Thread Summary 3-Barrier

Thread Summary 3-Barrier

0. Barrier

A barrier is a synchronization mechanism for users to coordinate the parallel work of multiple threads. The barrier allows all cooperating threads to reach a certain point and then continue execution from that point.

1. Related functions

1.1. Initialization

#include<pthread.h>
int pthread_barrier_init(pthread_barrier_t *restrict barrier,
                        const pthread_barrierattr_t *restrict attr, 
                        unisgned int count);
int pthread_barrier_destory(pthread_barrier_t *barrier);

The count parameter sets the number of threads that must reach the barrier before allowing all threads to continue execution.

1.2. wait function

int pthread_barrier_wait(pthread_barrier_t *barrier);

pthread_barrier_waitThe thread calling the function will go to sleep when the barrier count does not reach count. If the thread is the last to call pthread_barrier_wait, the barrier count is satisfied and all threads will be awakened.

For an arbitrary thread, the pthread_barrier_waitfunction returns PTHREAD_BARRIER_SERIAL_THREAD. The return value seen by the remaining threads is 0. This allows one thread to act as the main thread, and it can work on the results of the work completed by all other threads.

2. Routine

Routine 1: The teacher assigns a task to each of 10 students. The difficulty of the task is different, so the time-consuming is also different. After each student completes his task, output "task[i] accomplished in m minutes!". When all the students have completed their tasks, the teacher announced "mission accomplished!".

#include<pthread.h>
#define NTHR 10
void* task(void *arg);

pthread_barrier_t barrier;

int main()
{
    int err, i;
    pthread_t tid;

    pthread_barrier_init(&barrier, NULL, NTHR + 1);
    for (i = 0; i < NTHR; i++) {
        err = pthread_create(&tid, NULL, task, (void*)(i));
        if (err != 0) {
            printf("create thread error!\n");
            return -1;
        }
    }
    pthread_barrier_wait(&barrier);
    printf("mission accomplished!\n");
    return 0;
}

void* task(void *arg)
{
    int i, t;

    i = (int)arg;
    t = rand() % 10 + 1;
    // 睡几秒钟,假装我们在执行任务
    sleep(t); 
    printf("task %d accomplished in %d minutes\n", i, t);
    pthread_barrier_wait(&barrier);
    return ((void*)0);
}

See barrier-ex01.c for the complete example

Routine 2: Sort 10 million random numbers.

In this example, we created 100 threads, and each thread performs heap sorting on 100,000 numbers. After sorting is completed, the main thread is merged.

int main()
{
    int err, i;
    pthread_t tid;
    printf("INT_MAX = %d\n", INT_MAX);

    srand(time(NULL));  
    for (i = 0; i < NUMNUM; i++) {
        // sleep_ms(1);
        nums[i] = rand() % INT_MAX;
    }

    // 初始化barrier
    pthread_barrier_init(&barrier, NULL, NTHR + 1);
    // 创建100个排序线程
    for (i = 0; i < NTHR; i++) {
        err = pthread_create(&tid, NULL, sort, (void*)(i * TNUM));
        if (err != 0) {
            printf("create thread error!\n");
            return -1;
        }
    }
    
    pthread_barrier_wait(&barrier);
    //合并排序结果
    merge();
    printf("-------------------------------------\n");
    for (i = 0; i < NUMNUM; i++) {
        // nums[i] = random();
        printf("%10d \n", nums[i]);
        if ((i + 1) % TNUM == 0) {
            printf("-------------------------------------\n");
        }
    }
    return 0;
}

For the complete example, see barrier-ex02.c

3. Reference materials

[1.] Chapter 11 of "Advanced Programming in Unix Environment"

Guess you like

Origin blog.csdn.net/ww1473345713/article/details/95035156