Samiur Rahman
Samiur Rahman

Reputation: 63

How do i properly call a function while creating pthread?

I am creating a pthread in the main function and calling another function called "PrintHello". the "PrintHello" function should print some messages. My threads are being created but i am guessing my function "PrintHello" isn't being called properly as the messages are not printing.

I have put another print command in "PrintHello" function and that is printing. That means the function is being called. but i can't figure out why the messages aren't printing.

char *messages[NUM_THREADS];
struct thread_data
{
    int thread_id;
    int sum;
    char *message;
    };
struct thread_data thread_data_array[NUM_THREADS];

void *PrintHello(void *threadarg)
{
    printf("I am in PrintHello");
    int taskid , sum;
    char *hello_msg;
    struct thread_data *my_data;
    Sleep(1);
    my_data = (struct thread_data *) threadarg;
    taskid = my_data ->thread_id;
    sum = my_data ->sum;

    hello_msg = my_data ->message;
    printf("Thread %d: %s  Sum=%d\n", taskid , hello_msg , sum) ;

    pthread_exit(NULL);
}
int main(int argc , char *argv[])
{
    pthread_t threads[NUM_THREADS];
    int *taskids[NUM_THREADS];
    int rc, t, sum;
    sum=0;
    messages[0] = "English: Hello World!";
    ..............
    messages[7] = "Latin: Orbis , te saluto!";
for(t=0;t<NUM_THREADS;t++)
    {
        sum = sum + t;
        thread_data_array[t].thread_id = t;
        thread_data_array[t].sum = sum;
        thread_data_array[t].message = messages[t];
        printf("Creating thread %d\n", t);

        rc = pthread_create(&threads[t], NULL , PrintHello , (void *) &thread_data_array[t]);
        //rc = pthread_create(&threads[t], NULL , PrintHello , NULL);
        if (rc)
            {
                printf("ERROR; return code from pthread_create() is %d \n", rc);
                exit(-1);
            }
    }
        pthread_exit(NULL);
}

The code should print out the hello messages.

Upvotes: 0

Views: 585

Answers (2)

Samiur Rahman
Samiur Rahman

Reputation: 63

Sorry for the late reply. I should have mentioned that i am using Windows. Anyway, i found out the problem. It is happening because of the "sleep" argument. For windows apparently it is different. so i changed my sleep argument to Sleep(1000), which apparently means 1 second in windows and that solved my problem. Thank you for all the replies.

Upvotes: 0

Tofu
Tofu

Reputation: 3613

I assume what's happening is your application is exiting before your threads have the chance to actually execute fully. Since you are storing each thread handle in the array of pthread_t threads[NUM_THREADS]; what you will need to do is upon creation a call to pthread_join should be made which will allow the caller thread to be blocked until the thread has executed and returned. You can call pthread_join right after you call pthread_create or loop through all your handles and call pthread_join on each index in your array. If a call to pthread_join is made after the creation of each thread individually then a new thread will not spawn until the previous finishes. If you wish to execute them simultaneously then a loop after all the threads have been created would be the better option.

int main(int argc , char *argv[])
{
    pthread_t threads[NUM_THREADS];
    int *taskids[NUM_THREADS];
    int rc, t, sum;
    sum=0;
    messages[0] = "English: Hello World!";
    ..............
    messages[7] = "Latin: Orbis , te saluto!";
    for(t=0;t<NUM_THREADS;t++)
    {
        sum = sum + t;
        thread_data_array[t].thread_id = t;
        thread_data_array[t].sum = sum;
        thread_data_array[t].message = messages[t];
        printf("Creating thread %d\n", t);

        rc = pthread_create(&threads[t], NULL , PrintHello , (void *) &thread_data_array[t]);
        //rc = pthread_create(&threads[t], NULL , PrintHello , NULL);
        if (rc)
        {
            printf("ERROR; return code from pthread_create() is %d \n", rc);
            exit(-1);
        }
    }
    for(int index = 0; index < NUM_THREADS; ++index){
        pthread_join(threads[index],NULL);//Wait for execution of each thread
    }
}

You also don't need to call pthread_exit in your main. Typically that should be called in the thread you wish to terminate early where the value passed into pthread_exit can be retrieved from the second argument of pthread_join

Upvotes: 1

Related Questions