Created
January 23, 2012 00:52
-
-
Save pfactum/1659702 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 <stdlib.h> | |
#include <pthread.h> | |
#include <unistd.h> | |
void *worker(void *data); | |
struct worker_data_t | |
{ | |
pthread_t thread_id; | |
pthread_mutex_t mutex; | |
pthread_cond_t cond; | |
long id, data; | |
int ready; | |
}; | |
struct worker_data_t *workers_data; | |
int main(int argc, char **argv) | |
{ | |
int cpus = sysconf(_SC_NPROCESSORS_ONLN); | |
if (cpus < 1) | |
cpus = 1; | |
workers_data = malloc(cpus * sizeof(struct worker_data_t)); | |
int ret_val[cpus]; | |
for (int i = 0; i < cpus; i++) | |
{ | |
pthread_mutex_init(&workers_data[i].mutex, NULL); | |
pthread_cond_init(&workers_data[i].cond, NULL); | |
workers_data[i].id = i; | |
workers_data[i].ready = 0; | |
} | |
for (int i = 0; i < cpus; i++) | |
ret_val[i] = pthread_create(&workers_data[i].thread_id, NULL, worker, &workers_data[i]); | |
pthread_mutex_lock(&workers_data[0].mutex); | |
workers_data[0].ready = 1; | |
pthread_cond_signal(&workers_data[0].cond); | |
pthread_mutex_unlock(&workers_data[0].mutex); | |
pthread_mutex_lock(&workers_data[0].mutex); | |
while (&workers_data[0].ready != 0) | |
pthread_cond_wait(&workers_data[0].cond, &workers_data[0].mutex); | |
pthread_mutex_unlock(&workers_data[0].mutex); | |
pthread_mutex_lock(&workers_data[0].mutex); | |
workers_data[0].ready = 1; | |
pthread_cond_signal(&workers_data[0].cond); | |
pthread_mutex_unlock(&workers_data[0].mutex); | |
pthread_mutex_lock(&workers_data[0].mutex); | |
while (&workers_data[0].ready != 0) | |
pthread_cond_wait(&workers_data[0].cond, &workers_data[0].mutex); | |
pthread_mutex_unlock(&workers_data[0].mutex); | |
/*pthread_mutex_lock(&workers_data[0].mutex); | |
pthread_cond_signal(&workers_data[0].cond); | |
pthread_mutex_unlock(&workers_data[0].mutex); | |
pthread_mutex_lock(&workers_data[0].mutex); | |
pthread_cond_signal(&workers_data[0].cond); | |
pthread_mutex_unlock(&workers_data[0].mutex);*/ | |
for (int i = 0; i < cpus; i++) | |
pthread_join(workers_data[i].thread_id, NULL); | |
return 0; | |
} | |
void *worker(void *data) | |
{ | |
struct worker_data_t d = *((struct worker_data_t*)(data)); | |
printf("Thread %d started\n", d.id); | |
for (;;) | |
{ | |
pthread_mutex_lock(&workers_data[d.id].mutex); | |
while (workers_data[d.id].ready == 0) | |
pthread_cond_wait(&workers_data[d.id].cond, &workers_data[d.id].mutex); | |
printf("catch!\n"); | |
workers_data[d.id].ready = 0; | |
pthread_mutex_unlock(&workers_data[d.id].mutex); | |
pthread_mutex_lock(&workers_data[d.id].mutex); | |
pthread_cond_signal(&workers_data[d.id].cond); | |
pthread_mutex_unlock(&workers_data[d.id].mutex); | |
/*pthread_mutex_lock(&workers_data[d.id].mutex); | |
pthread_cond_wait(&workers_data[d.id].cond, &workers_data[d.id].mutex); | |
printf("catch!\n"); | |
pthread_mutex_unlock(&workers_data[d.id].mutex);*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment