簡體   English   中英

使用pthread_join的pthread的同步問題

[英]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循環在其先前的結尾之前開始。 我做錯了什么?

如果要等待特定情況發生,請使用函數類pthread_cond_*

如果只想等待線程執行操作,則刪除while(!wakeup){} 這行得通,因為

int pthread_join(pthread_t線程,void ** value_ptr);

pthread_join()函數會暫停調用線程的執行,直到目標線程終止為止,除非目標線程已經終止。

因此,在以下代碼段中,您首先創建了限制線程,它們立即開始運行。 然后,您等待每個線程結束,即暫停調用線程,直到限制線程終止。

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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM