簡體   English   中英

使用互斥鎖在2個進程之間進行屏障同步

[英]Barrier Synchronization between 2 process using mutex

我需要使用互斥鎖(僅)在2個線程之間實現屏障同步。 屏障同步是2個線程將在預定義的步驟互相等待,然后再繼續。

我能夠使用seamaphore做到這一點,但是我如何僅使用互斥鎖來實現這一點 提示我需要2個互斥鎖而不是1個。

使用Seamaphore:

#include <pthread.h>
#include <semaphore.h>
using namespace std;

sem_t s1;
sem_t s2;


void* fun1(void* i)
{
    cout << "fun1 stage 1" << endl;
    cout << "fun1 stage 2" << endl;
    cout << "fun1 stage 3" << endl;
    sem_post (&s1);
    sem_wait (&s2);
    cout << "fun1 stage 4" << endl;
}

void* fun2(void* i)
{
    cout << "fun2 stage 1" << endl;
    cout << "fun2 stage 2" << endl;
//    sleep(5);
    sem_post (&s2);
    sem_wait (&s1);
    cout << "fun2 stage 3" << endl;
}

main()
{
    sem_init(&s1, 0, 0);
    sem_init(&s2, 0, 0);
    int value; 
    sem_getvalue(&s2, &value);
    cout << "s2 = " << value << endl;

    pthread_t iThreadId;

    cout << pthread_create(&iThreadId, NULL, &fun2, NULL) << endl;
//    cout << pthread_create(&iThreadId, NULL, &fun2, NULL) << endl;
    pthread_create(&iThreadId, NULL, &fun1, NULL);
    sleep(10);
}

將以上代碼編譯為“ g ++ barrier.cc -lpthread”

沒有雜物和鎖怎么辦? 僅使用原子操作

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

static sigset_t _fSigSet;
static volatile int _cMax=20, _cWait = 0;
static pthread_t    _aThread[1000];

void * thread(void *idIn)
{
int nSig, iThread, cWait, id = (int)idIn;

    printf("Start %d\n", id, cWait, _cMax);
    // do some fake weork
    nanosleep(&(struct timespec){0, 500000000}, NULL);
    // barrier
    cWait = __sync_add_and_fetch(&_cWait, 1);
    printf("Middle %d, %d/%d Waiting\n", id, cWait, _cMax);
    if (cWait < _cMax)
    {        
        // if we are not the last thread, sleep on signal
        sigwait(&_fSigSet, &nSig); // sleepytime
    }
    else
    {
        // if we are the last thread, don't sleep and wake everyone else up
        for (iThread = 0; iThread < _cMax; ++iThread)
            if (iThread != id)
                pthread_kill(_aThread[iThread], SIGUSR1);
    }

    // watch em wake up    
    cWait = __sync_add_and_fetch(&_cWait, -1);
    printf("End %d, %d/%d Active\n", id, cWait, _cMax);

    return 0;
}

int main(int argc, char** argv)
{
    pthread_attr_t attr;
    int i, err;

    sigemptyset(&_fSigSet);
    sigaddset(&_fSigSet, SIGUSR1);
    sigaddset(&_fSigSet, SIGSEGV);

    printf("Start\n");
    pthread_attr_init(&attr);
    if ((err = pthread_attr_setstacksize(&attr, 16384)) != 0)
    {
        printf("pthread_attr_setstacksize failed: err: %d %s\n", err, strerror(err));
        exit(0);
    }

    for (i = 0; i < _cMax; i++)
    {
        if ((err = pthread_create(&_aThread[i], &attr, thread, (void*)i)) != 0)
        {
            printf("pthread_create failed on thread %d, error code: %d %s\n", i, err, strerror(err));
            exit(0);
        }
    }

    for (i = 0; i < _cMax; ++i)
        pthread_join(_aThread[i], NULL);

    printf("\nDone.\n");
    return 0;
}

我不確定您是否需要兩個互斥鎖,其中一個互斥鎖和一個條件變量以及一個額外的標志可能就足夠了。 這個想法是您通過獲取互斥鎖進入關鍵部分,然后檢查是否是第一個線程,如果是,則等待條件。 如果您是第二個線程,那么您將喚醒等待的線程,並且兩個線程都將離開。

暫無
暫無

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

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