簡體   English   中英

N螺紋帶針線的屏障

[英]barrier for N threads with a semphore

我寫了以下簡短的應用程序來解決障礙問題。 此應用程序應確保運行相同線程方法的三個相同線程都將在一個公共代碼段中全部“滿足”。 我運行它,看起來還可以。 我的問題是:

1)正確嗎?

2)有沒有一種首選的高效方法可以為N個線程實現它?

這是代碼:

static sem_t t_1_sem;

static sem_t t_2_sem;

static sem_t t_3_sem;



struct my_thread_info {
    int num;
};

void *thread(void *vargp)

{

        struct my_thread_info *info = (struct my_thread_info*)vargp;
        static int counter=0;
        counter++;

        if (info->num == 1) {
                printf("info->num=%d\n", info->num);
                if (counter<3)
                    sem_wait(&t_1_sem);   // down
                else {
                    sem_post(&t_2_sem);   // up
                    sem_post(&t_3_sem);   // up                 
                }

        } else 
            if (info->num == 2) {
                printf("info->num=%d\n", info->num);
             if (counter<3)             
                    sem_wait(&t_2_sem);             
                else {
                    printf("info->num=%d\n", info->num);
                    sem_post(&t_1_sem);
                    sem_post(&t_3_sem);    //up             
            }
            } 
            else  
            if (info->num == 3) {
                printf("info->num=%d\n", info->num);
             if (counter<3)             
                    sem_wait(&t_3_sem);             
                else {
                    sem_post(&t_1_sem);
                    sem_post(&t_2_sem);    //up             
            }
        }
        printf("meeting occured!\n");

}

int main()
{
    pthread_t tid0, tid1, tid2;

    struct my_thread_info info1, info2, info3;
    info1.num = 1;

    sem_init(&t_1_sem, 0, 0);
    sem_init(&t_2_sem, 0, 0);
    sem_init(&t_3_sem, 0, 0);

    pthread_create(&tid0, NULL, thread, &info1);
    info2.num = 2;

    pthread_create(&tid1, NULL, thread, &info2);

    info3.num = 3;
    pthread_create(&tid2, NULL, thread, &info3);


    pthread_join(tid0, NULL);
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);   
    pause();
    return 0;

}

問候凱文

首先,您的counter變量是不受保護的-在進行counter++時,您可能處於競爭狀態。 使用互斥鎖;

至於做N個線程-使用IMO可以接受使用線程/信號量的數組,我有使用該設計的代碼,並且效果很好。

1.不,這是不正確的。 counter++;那樣遞增counter++; 會導致比賽情況,並且無法正確增加計數器。 您想用一個關鍵部分來包圍它。

2。

static pthread_mutex_t cs_mutex = PTHREAD_MUTEX_INITIALIZER;
static sem_t t_sem;

void *thread(void *vargp)
{
    static int counter=0;
    pthread_mutex_lock( &cs_mutex );
    counter++;
    pthread_mutex_unlock( &cs_mutex );

    if( counter == NO_THREADS )
        sem_post( &t_sem );

    sem_wait( &t_sem );
    sem_post( &t_sem );
}
int main( int argc, char *argv[] ){

    pthread_t tids[NO_THREADS];
    sem_init( &t_sem, 0, 0 );

    for( i=0; i<NO_THREADS; i++ ){
        pthread_create(&tids[i], NULL, thread, NULL);
    }

    for( i=0; i<NO_THREADS; i++ ){
        pthread_join(&tids[i], NULL);
    }
    pause();
    return 0;
}

暫無
暫無

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

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