Created
October 13, 2016 04:55
-
-
Save kumagi/9f5b7ea65905f1ace5a84329bc76b37a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <pthread.h> | |
#include <sys/eventfd.h> | |
#include <unistd.h> | |
struct workset { | |
int tid_; | |
int efd_; | |
}; | |
void *worker(void *arg) { | |
pthread_t tid; | |
workset* ids = (workset*)arg; | |
eventfd_read(ids->efd_, NULL); | |
printf("worker(%d) finish\n", ids->tid_); | |
} | |
int main() { | |
pthread_t tid[10]; | |
workset ids[10]; | |
int efd; | |
efd = eventfd(0, EFD_SEMAPHORE); | |
printf("main: efd=%d\n", efd); | |
for(int i = 0; i < 10; i++) { | |
ids[i].tid_ = i; | |
ids[i].efd_ = efd; | |
pthread_create(&tid[i], NULL, worker, &ids[i]); | |
printf("main: spawn worker(%d)\n", i); | |
sleep(1); | |
} | |
sleep(1); | |
eventfd_write(efd, 5); | |
sleep(1); | |
eventfd_write(efd, 5); | |
for(int i = 0; i < 10; i++) | |
pthread_join(tid[i], NULL); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main: efd=3 | |
main: spawn worker(0) | |
main: spawn worker(1) | |
main: spawn worker(2) | |
main: spawn worker(3) | |
main: spawn worker(4) | |
main: spawn worker(5) | |
main: spawn worker(6) | |
main: spawn worker(7) | |
main: spawn worker(8) | |
main: spawn worker(9) | |
worker(5) finish | |
worker(0) finish | |
worker(8) finish | |
worker(1) finish | |
worker(7) finish | |
worker(6) finish | |
worker(9) finish | |
worker(2) finish | |
worker(3) finish | |
worker(4) finish |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment