int threads = 5;
pthread_t * thread = malloc(sizeof(pthread_t)*threads);
for (i = 0; i < threads; i++){
int ret = pthread_create(&thread[i], NULL, &foobar_function, NULL);}
I'm not in a position to run the code right now. But I saw this as part of an online example and was a little confused by the total lack of square brackets. I'm not great with C.
So does this work for creating an array of threads?
Yes.
thread is pointing at a block of memory allocated by malloc that is large enough to hold threads pthread_t objects.
An array of threads pthread_t objects can be represented in exactly this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With