In c, how to make a thread wait for other threads to finish

Praveen Sundar

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?

John Zwinck

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

make main thread wait for other threads to finish

How to wait for all threads to finish, using ExecutorService?

how to make main thread wait for executor service threads to complete

How to wait for thread pool to finish all tasks?

How to wait for the function finish (Threads in Thread)

How can I wait for a thread to finish with .NET?

Make the main thread wait until all threads finish

How to force main thread to wait for all its child threads finish in Haskell

Python: How to NOT wait for a thread to finish to carry on?

Let current thread finish using the function before letting other threads call it C#

Java consumer thread to wait for all producer threads to finish

How to wait till all threads finish their work?

Gradle Sync: Wait for the other thread to finish acquiring the distribution never ends

How NOT to wait for a thread to finish in Python

Main thread wait for other threads

How do I wait for all threads in an Indy thread pool to finish

Multi-threaded matrix multiplication in the main thread doesn't wait until the other threads finish their work?

Make 1 Thread wait for an array of Threads to complete

how to make thread2 wait for thread1 in c using posix-threads

How to wait for a set of threads to finished or other event in C++

How to wait for multiple threads to finish (with c++11 threads)?

How to wait for a reflection thread to finish

How do I make the current thread wait for another thread to finish before proceeding?

Why does main thread wait for background threads to finish

How do I use threads to make an action wait for another action to finish before moving on?

Java threading - How to properly wait for threads to finish?

MariaDB make SELECT wait for other transactions to finish

How to make destructor wait until other thread's job complete?

Wait for threads to finish before creating a new thread