Created
August 29, 2012 18:24
-
-
Save anonymous/3516639 to your computer and use it in GitHub Desktop.
benchthread.c
This file contains hidden or 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 <stdlib.h> | |
| #include <stdio.h> | |
| #include <pthread.h> | |
| #include <semaphore.h> | |
| int nthreads; | |
| sem_t die; | |
| void * thread_func(void *arg) { | |
| if ((int)arg == nthreads-1) { | |
| sem_post(&die); | |
| } | |
| else { | |
| while (1) { | |
| sleep(0); // имитация бурной деятельности | |
| } | |
| } | |
| } | |
| int main(int argc, char *argv[]) { | |
| nthreads = atoi(argv[1]); | |
| pthread_t *threads = calloc(nthreads, sizeof(pthread_t)); | |
| int i; | |
| sem_init(&die, 0, 0); | |
| for (i = 0; i < nthreads; i++) { | |
| pthread_create(threads + i, NULL, thread_func, (void *)i); | |
| } | |
| sem_wait(&die); | |
| printf("Time to die\n"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment