pthread_barrier_t 사용법 (pthread barrier)
2021. 9. 27. 07:48ㆍLinux
- 목차
반응형
// VERY VERY SIMPLE EXAMPLE BELOW !!
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#define RANGE_WORK_NUM (5)
#define RANGE_SLEEP (100000)
#define NUM_THREADS (4)
typedef struct {
unsigned int id;
pthread_barrier_t *bar;
} worker_param_t;
static void* worker(void *carpark_in) {
worker_param_t *worker_param = (worker_param_t *)carpark_in;
unsigned int seed, heart_beat = 0;
unsigned int i = 0;
printf("[%02u]+worker\r\n", worker_param->id);
for (;i < rand_r(&seed) % RANGE_WORK_NUM; i++) {
usleep(rand_r(&seed) % RANGE_SLEEP);
printf("[%02u] do work... = %u\r\n", worker_param->id, heart_beat++);
}
pthread_barrier_wait(worker_param->bar);
printf("[%02u]-worker\r\n", worker_param->id);
return (NULL);
}
int main(int argc, char *argv[]) {
unsigned int i;
worker_param_t worker_param[NUM_THREADS];
pthread_t workers[NUM_THREADS];
pthread_barrier_t bar;
pthread_barrier_init(&bar, NULL, NUM_THREADS);
for (i = 0; i < NUM_THREADS; i++) {
worker_param[i].id = i;
worker_param[i].bar = &bar;
pthread_create(&workers[i], NULL, worker, (void *)&worker_param[i]);
}
printf("start join\r\n");
for (i = 0; i < NUM_THREADS; i++) {
pthread_join(workers[i], NULL);
}
printf("---\r\n");
/*Changed from resturn(0) */
return (EXIT_SUCCESS);
}
반응형
'Linux' 카테고리의 다른 글
input driver (0) | 2021.09.27 |
---|---|
System call (0) | 2021.09.27 |
Ioremap 코드 분석 (0) | 2021.09.27 |
Linux의 kmalloc과 vmalloc에 대해서 (0) | 2021.09.27 |
vmalloc (0) | 2021.09.21 |