Skip to content

Instantly share code, notes, and snippets.

Created August 29, 2012 18:24
Show Gist options
  • Select an option

  • Save anonymous/3516639 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/3516639 to your computer and use it in GitHub Desktop.
benchthread.c
#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