4

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?

4
  • pthread_t is some unspecified type. On my Debian/Linux system, it is a long... And malloc could fail. Did you test for that? At last, you should clear the ptr array ... But pthread_create could fail, and you should test that. Commented Nov 5, 2014 at 9:49
  • 1
    Your malloc solution seems correct. The problem is probably elsewhere. Commented Nov 5, 2014 at 9:51
  • 2
    Please edit your question to improve it with real code. Commented Nov 5, 2014 at 9:52
  • 1
    "will not work". Please go ahead and describe the problem. Commented Nov 20, 2016 at 6:30

2 Answers 2

5

yes, it should work. what is your some space?

You did not put complete code, but just a suggestion, did you initialize num ?

Check the below code. I believe it's working.

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void* func(void* id)
{
        int *c;
        c = (int*)id;
        if(c)
                printf("%d\n", *c);
}

int main(int argc, char *argv[])
{
        int i = 0;
        int *index = NULL;
        int num;
        pthread_t *ptr;
        if (argv[1])
                num = atoi(argv[1]);

        index = calloc (num, sizeof (int));
        for(i = 0; i < num; i++)
        {
                index[i] = i;
        }

        ptr = malloc(sizeof(pthread_t)*num);
        for(i = 0; i < num; i++)
        {
                pthread_create(&ptr[i], NULL, func, (void*)&index[i]);
        }
        for(i = 0; i < num; i++)
                pthread_join(ptr[i], NULL);

        return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

It's same as you dynamically allocate integer array ...

pthread_t  *id_array;

id_array = (pthread_t*)malloc(sizeof(pthread_t)*n); 

where n = array size ...

it will work for you..

2 Comments

He need to initialize not allocate memory
Also, you should not cast malloc()'s return in C. Check this post for the reasons: stackoverflow.com/q/605845/4726668 . This question is tagged with C, not C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.