Created
October 9, 2011 06:27
-
-
Save m-mizutani/1273378 to your computer and use it in GitHub Desktop.
pthread_cond_signal benchmark
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 <pthread.h> | |
#include <sys/time.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
long long TEST_NUMBER ; | |
typedef struct { | |
pthread_mutex_t mutex; | |
pthread_cond_t cond; | |
long long queue_count; | |
long long recv_count; | |
long long cond_wait; | |
long long cond_pass; | |
} bridge_t ; | |
void * sender (void * obj) { | |
long long i; | |
bridge_t * b = (bridge_t *)obj; | |
for (i = 0; i < TEST_NUMBER; i++) { | |
pthread_mutex_lock (&b->mutex); | |
b->queue_count++; | |
pthread_cond_signal (&b->cond); | |
pthread_mutex_unlock (&b->mutex); | |
} | |
return NULL; | |
} | |
void * recver (void * obj) { | |
bridge_t * b = (bridge_t *)obj; | |
int q = 0; | |
while (1) { | |
pthread_mutex_lock (&b->mutex); | |
if (b->queue_count == 0) { | |
pthread_cond_wait (&b->cond, &b->mutex); | |
b->cond_wait++; | |
} | |
else { | |
b->cond_pass++; | |
} | |
q = b->queue_count; | |
b->queue_count = 0; | |
pthread_mutex_unlock (&b->mutex); | |
b->recv_count += q; | |
if (b->recv_count >= TEST_NUMBER) | |
break; | |
} | |
return NULL; | |
} | |
int main (int argc, char *argv[]) { | |
bridge_t b; | |
pthread_t th_s, th_r; | |
struct timeval ts_start, ts_end, ts; | |
char *e; | |
if (argc != 2) { | |
printf ("give me test loop number!\n"); | |
exit (EXIT_FAILURE); | |
} | |
TEST_NUMBER = strtoll (argv[1], &e, 0); | |
if (*e != '\0') { | |
printf ("invalid number!\n"); | |
exit (EXIT_FAILURE); | |
} | |
pthread_mutex_init (&(b.mutex), NULL); | |
pthread_cond_init (&(b.cond), NULL); | |
b.queue_count = 0; | |
b.recv_count = 0; | |
b.cond_wait = 0; | |
b.cond_pass = 0; | |
gettimeofday (&ts_start, NULL); | |
pthread_create (&th_s, NULL, sender, &b); | |
pthread_create (&th_r, NULL, recver, &b); | |
pthread_join (th_s, NULL); | |
pthread_join (th_r, NULL); | |
gettimeofday (&ts_end, NULL); | |
timersub (&ts_end, &ts_start, &ts); | |
double duration = (double)ts.tv_sec + ((double)ts.tv_usec) / 1000000; | |
double mps = (double)TEST_NUMBER / duration; | |
printf ("%lld, %10.6f, %lld, %lld\n", TEST_NUMBER, mps, b.cond_wait, b.cond_pass); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment