使用 pthread_join 的 pthreads 的同步问题

Synchronization problem with pthreads using pthread_join

我有以下循环,

for (i = 1; i < nDiag; ++i)
{
    elem = findelemnum();
    taskarr[3]= elem;
    if(threadnum > elem) limit = elem;
    else limit=threadnum;

    if (i <= lenA) 
    {
        si = i;
        sj = 1;
    } 
    else 
    {
        si = lenA;
        sj = i - lenA + 1;
    }
    taskarr[0] = si, taskarr[1] = sj;  

    for (j = 0; j < limit; ++j) 
    {
        taskarr[2] = j;
        wakeup = 0;
        pthread_create(&threads[j], NULL, mytask, taskarr);
        while(!wakeup){
        }
    }
    for (int j=0; j < limit ;j++){
        pthread_join(threads[j],NULL);  
    } 
}

我想同步我的线程,以便所有线程先完成内部循环,然后开始新循环 (i++)。出于这个原因,我使用了 pthread_join 函数。虽然一个新的 for i 循环在其之前的结束之前开始。我做错了什么?

如果您想等待特定条件发生,请使用 class 函数 pthread_cond_*

如果您只是想等待线程完成它们的工作,请删除 while(!wakeup){}。这是有效的,因为

int pthread_join(pthread_t thread, void **value_ptr);

The pthread_join() function suspends execution of the calling thread until the target thread terminates, unless the target thread has already terminated.

所以在下面的代码片段中,您首先创建 limit 个线程,然后它们立即启动 运行。然后等待每个线程结束,即挂起调用线程,直到 limit 个线程终止。

for (j = 0; j < limit; ++j) 
{
    taskarr[2] = j;
    wakeup = 0;
    pthread_create(&threads[j], NULL, mytask, taskarr);
}

for (int j=0; j < limit ;j++){
    pthread_join(threads[j],NULL);  
} 

这是一个小演示

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

#define NTHREAD 4
#define ITERATIONS 5

typedef struct _pcontext
{
    int id;
    int iteration;
}pcontext_t;

void* work(void* arg)
{
    pcontext_t* ctx = (pcontext_t*)arg;
    
    sleep(2); 
    
    printf("Thread ID: %d\n", ctx->id);
    printf("Iteration nr: %d\n", ctx->iteration);
    printf("Done.\n\n");
    
    return NULL;
}

int main()
{
    pthread_t thread[NTHREAD];
    pcontext_t ctx[NTHREAD];
    int err;
    int i,j;
    
    for(j = 0; j < ITERATIONS; j++)
    {
        for(i = 0; i < NTHREAD; i++)
        {
            ctx[i].id = i;
            ctx[i].iteration = j;
            err = pthread_create(&thread[i], NULL, work, (void*)(&(ctx[i])));
            if (err)
            {
                printf("An error occured: %d", err);
                return err;
            }
        }
        
        printf("Waiting for the threads to end...\n");

        for(i = 0; i < NTHREAD; i++)
        {
          pthread_join(thread[i], (void**)NULL);    
        }
        
        printf("Threads ended.\n");
        printf("Iteration %d ended.\n\n", j);
    } 
    
    return 0;
}

输出:

gcc -Wall -Wextra -pedantic -pthread -o main main.c 
Waiting for the threads to end...
Thread ID: 1
Iteration nr: 0
Done.

Thread ID: 0
Iteration nr: 0
Done.

Thread ID: 2
Iteration nr: 0
Done.

Thread ID: 3
Iteration nr: 0
Done.

Threads ended.
Iteration 0 ended.

Waiting for the threads to end...
Thread ID: 0
Iteration nr: 1
Done.

Thread ID: 3
Iteration nr: 1
Done.

Thread ID: 2
Iteration nr: 1
Done.

Thread ID: 1
Iteration nr: 1
Done.

Threads ended.
Iteration 1 ended.