I have a pthread pointer, and I need to allocate enough space for the pointer to hold enough number of pthread. Then initialize them and pthread_create() to pass thread to some functions.
The problem is that if I just use malloc to allocate space to the pointer, then just pthread_create using pointer[index], then the thread will not be created properly. How do I fix this problem? I believe pthread_t is some type of struct, so I believe I need to initialize them before I do pthread. how do I do that? thank you.
I just tested with certain amount of pthread that they worked properly:
pthread_t t1, t2, t3 ...... tn;
then
pthread_create(&t1, NULL, function, (void *)argument)
But if I use pointer and malloc, they wont work.Thread will not be created.
pthread_t *ptr;
ptr = malloc(sizeof(pthread_t)*num);
then
pthread_create(&ptr[index], NULL, function, (void *)argument)
will not work. How do I initialize then ptr[index] in this case?
pthread_tis some unspecified type. On my Debian/Linux system, it is along... Andmalloccould fail. Did you test for that? At last, you should clear theptrarray ... Butpthread_createcould fail, and you should test that.