GregH
GregH

Reputation: 5459

c++ issue passing information to pthreads

The issue I am having is that printf debug statements in showData() will give me nonsensical numbers. i.e.: the thread_id is -1781505888. If I insert the printf statements in createThreads() right after I set the values of thread_id, startIndex, and stopIndex then the values will print correctly. Somehow data is being corrupted when i pass threadData as an argument to pthread_create or when I read data from threadArg in showData(). Also, the values of N and k can be assumed to be integers with the remainder of N/k being 0. All help is appreciated.

edit: Also if it provides any extra information- when I run this on the same input I will get different output each time I run it. sometimes nonsensical numbers, sometimes all values print as 0, and once in a while it seg faults.

void createThreads(int k){
struct threadData threadData;
int numThreads = k;
int i = 0;
int err = 0;

pthread_t *threads = static_cast<pthread_t*>(malloc(sizeof(pthread_t) * numThreads));
for(i = 0;i<numThreads;i++){
    struct threadData threadData;
    threadData.thread_id = i;
    threadData.startIndex = ((N/k)*i);
    if(i == numThreads -1){
        threadData.stopIndex = ((N/k)*(i+1))-1;
    }
    else{
        threadData.stopIndex = ((N/k)*(i+1));
    }



    err = pthread_create(&threads[i], NULL, showData, (void *)&threadData); 


    if(err != 0){
        printf("error creating thread\n");
    }
}
}

void *showData(void *threadArg){
    struct threadData *threadData;
    threadData = (struct threadData *) threadArg;

    printf("thread id : %d\n", threadData->thread_id);
    printf("start: %d\n", threadData->startIndex);
    printf("stop : %d\n", threadData->stopIndex);
}

Upvotes: 0

Views: 71

Answers (1)

Tony Delroy
Tony Delroy

Reputation: 106116

threadData is local to your for loop... it goes out of scope on each iteration, so a pointer to it is not valid in your showData() routine. You could instead allocate it dynamically and delete it at the end of showData.

You probably want to return the threads data to createThreads' caller too, so it can join the threads to await completion of the showData "work".

Example:

...
for(i = 0; i < numThreads; ++i)
{
    struct threadData* threadData = new struct threadData;
    threadData->thread_id = i;
    threadData->startIndex = ((N/k)*i);
    if(i == numThreads -1){
        threadData->stopIndex = ((N/k)*(i+1))-1;
    }
    else{
        threadData->stopIndex = ((N/k)*(i+1));
    }

    err = pthread_create(&threads[i], NULL, showData, (void*)threadData); 

    if(err != 0){
        printf("error creating thread\n");
        exit(1); // probably not worth trying to continue...
    }
    return threads;
}

void *showData(void *threadArg){
    struct threadData* threadData = (struct threadData*)threadArg;

    printf("thread id : %d\n", threadData->thread_id);
    printf("start: %d\n", threadData->startIndex);
    printf("stop : %d\n", threadData->stopIndex);

    delete threadData;
}

Upvotes: 2

Related Questions