0

I'm inclined towards using vectors and smart pointers instead of arrays as it is safer. The problem is that when initialised as an array, my threads join the main thread successfully. However, this is not the case when initialised as a vector. This is expected as the array elements are accessed by reference.

1. As array

pthread_t threads[numCPU]; // Initialised as an array

// Creating threads
for(long unsigned int i=0; i<numCPU; i++){
        rc = pthread_create(&threads[i], &attr, myfunc, (void *) args[i] );
        if (rc) {
            printUserMessage(std::string("Error; return code from pthread_create is : ") + std::to_string(rc));
        } else {
            printUserMessage(std::string("Created Thread " + std::to_string(i)));
        }
}

// Joining thread
for(long unsigned int i=0; i<numCPU;i++){
      rc = pthread_join(threads[i], NULL);

        if (rc) {
            printUserMessage(std::string("Error; return code from pthread_create is : ") + std::to_string(rc));
        } else {
            printUserMessage(std::string("Joined Thread " + std::to_string(i)));
}

The signatures of pthread_create and pthread_join would be respected when:

std::vector<pthread_t *> threads(numCPU);

A vector of pointers doesn't make sense and I may have to manage memory. Is there a safer way to create threads?

newkid
  • 1,368
  • 1
  • 11
  • 27
  • Any reason why you aren't use `std::thread`? – NathanOliver May 28 '20 at 15:37
  • @NathanOliver https://stackoverflow.com/questions/13134186/c11-stdthreads-vs-posix-threads – newkid May 28 '20 at 15:42
  • That really doesn't answer the question. That answer is 8 years old and `std::thread` is quite ubiquitous today. – NathanOliver May 28 '20 at 15:43
  • @NathanOliver isn't std::thread a c++ API which ultimately compiles to posix threads? I need a bit more control over the thread and mutex. That is the reason I'm still using pthreads. – newkid May 28 '20 at 15:50
  • Yes it is, but it is RAII and much easier (IMHO) to use. You can actually use the type system and get rid of all the casts to and from `void*`'s. – NathanOliver May 28 '20 at 15:52
  • @newkid `isn't std::thread a c++ API which ultimately compiles to posix threads?` Only on POSIX systems. On non-POSIX systems, it will be implemented using something else. `I need a bit more control` Can you be more specific? What is missing from `std::thread` that you need? – eerorika May 28 '20 at 15:52
  • @eerorika: My libraries are compiled into python extensions and I've also implemented fortran api's. The product is currently deployed on posix supported systems (macOS, centOS and RHEL). When debugging from python and latching on with a debugger, I've had trouble tracing back with APIs so I've not tried std::thread. Could I expect issues there? – newkid May 28 '20 at 16:09

1 Answers1

1

I'm inclined towards using vectors and smart pointers instead of arrays as it is safer.

Vectors and smart pointers aren't safer than arrays.

Using vector instead of an array would be useful if you want the number of threads to be determined at runtime, without a constant upper limit. Otherwise it won't be useful.

It is unclear why you would create a vector of pointers.

eerorika
  • 232,697
  • 12
  • 197
  • 326