简体   繁体   English

如何让一个线程等待其他线程完成

[英]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 .在一个线程中,我为整数ab分配了一个值。 In the second thread, I want to access a and b , to change their values.在第二个线程中,我想访问ab ,以更改它们的值。

#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.我想让线程 2(add) 等待线程 1(assign) 完成并退出。 How can I implement it?我该如何实施?

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.让第二个线程在第一个线程上执行pthread_join()
  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.只需将逻辑按顺序放在单个线程中。

I suggest you to use a semaphore.我建议你使用信号量。

  1. Define a global semaphore with value 1.定义一个值为 1 的全局信号量。

  2. Before creating two threads, you do a P operation, and the value of the semaphore will be 0.在创建两个线程之前,你做了一个P操作,信号量的值为0。

  3. In thread 1 after you have assigned the value of a and b, you do a V operation.在分配 a 和 b 的值后,在线程 1 中执行 V 操作。 The value of semaphore will be 1.信号量的值为 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.在进行打印之前,在线程 2 中添加一个操作 V。如果线程 1 还没有完成分配,线程 2 将进入休眠状态,直到线程 1 完成。

That's my opinion about this question.这就是我对这个问题的看法。

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.如果您使用多个线程,我的意思是您必须在每个其他线程上 pthread_join 两个以上的线程,这不是有效的解决方案。 In my opition you should used pthrea_mutex_lock(&lock) and pthread_mutex_unlock(&lock) just after entering and exiting from the thread function.在我的选择中,您应该在进入和退出线程函数后使用 pthrea_mutex_lock(&lock) 和 pthread_mutex_unlock(&lock) 。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM