Praveen Sundar
Praveen Sundar

Reputation: 89

How to make a thread wait for other threads to finish

I have a program where I created two threads. In one thread I assigned a value to integers a and b. In the second thread, I want to access a and b, to change their values.

#include <stdio.h>
#include <pthread.h>

struct data {
    int a;
    int b;
};

struct data temp;

void *assign(void *temp)
{
    struct data *new;

    new = (struct data *) temp;
    new->a = 2;
    new->b = 2;
    printf("You are now in thread1..\n The value of a and b is: %d, %d", new->a + 1, new->b + 1);
    printf("\n");
    pthread_exit(NULL);
}

void *add(void *temp1)
{
    struct data *new1;
    new1 = (struct data *) temp1;
    printf("You are now in thread 2\nValue of a and b is: %d, %d\n", new1->a - 1, new1->b - 1);
    pthread_exit(NULL);
}

int main()
{
    pthread_t threads[2];
    pthread_attr_t attr;
    void *status;
    int rc, t;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    pthread_create(&threads[0], NULL, assign, (void *) &temp);
    pthread_create(&threads[1], NULL, add, (void *) &temp);
    pthread_attr_destroy(&attr);
    for (t = 0; t < 2; t++) {
        rc = pthread_join(threads[t], &status);
        if (rc) {
            printf("ERROR; return code from pthread_join() is %d\n", rc);
            exit(-1);
        }
        printf("Main: completed join with thread %ld having a status of %ld\n", t, (long) status);
    }
    pthread_exit(NULL);
    return 0;
}

But the above program executes the two threads simultaneously. Sometimes I get

thread1..
The value of a and b is: 3, 3
thread 2
Value of a and b is: 1, 1

and sometimes I get

thread 2
Value of a and b is: -1, -1
You are now in thread1..
The value of a and b is: 3, 3

I want to make thread-2(add) to wait for thread-1(assign) to finish and exit. How can I implement it?

Upvotes: 1

Views: 17287

Answers (3)

user2201468
user2201468

Reputation: 9

If you use multiple threads, I mean more than two threads you have to pthread_join on every other thread and is not efficient solution. In my opition you should used pthrea_mutex_lock(&lock) and pthread_mutex_unlock(&lock) just after entering and exiting from the thread function. For an instance:

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void *assign(void *temp)
{
    pthread_mutex_lock(&lock)
    struct data *new;

    new = (struct data *) temp;
    new->a = 2;
    new->b = 2;
    printf("You are now in thread1..\n The value of a and b is: %d, %d", new->a + 1, new->b + 1);
    printf("\n");
    pthread_mutex_unlock(&lock)
    pthread_exit(NULL);
}

void *add(void *temp1)
{
    pthread_mutex_lock(&lock)
    struct data *new1;
    new1 = (struct data *) temp1;
    printf("You are now in thread 2\nValue of a and b is: %d, %d\n", new1->a - 1, new1->b - 1);
    pthread_mutex_unlock(&lock)
    pthread_exit(NULL);
}

Upvotes: 0

kelvin wu
kelvin wu

Reputation: 23

I suggest you to use a semaphore.

  1. Define a global semaphore with value 1.

  2. Before creating two threads, you do a P operation, and the value of the semaphore will be 0.

  3. In thread 1 after you have assigned the value of a and b, you do a V operation. The value of semaphore will be 1.

  4. In thread 2 before you do the print, add an operation V. If thread 1 hasn't finished the assignment, thread 2 will go to sleep until thread 1 have finished.

That's my opinion about this question.

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249582

If one thread must wait for the other to finish, I see three options:

  1. Make the second thread do pthread_join() on the first one.
  2. Use a condition variable to signal the second thread when the first one is done.
  3. Stop using threads, as it's pointless to have one whose only job is to wait for another one. Just put the logic sequentially in a single thread.

Upvotes: 11

Related Questions