6 #include <pthread-l4.h>
9 #include <l4/sys/kdebug.h>
12 static int globalcounter;
15 static const unsigned print_iterations = 100;
16 static const unsigned inc_iterations = 10000;
19 void *thread(void *data)
25 for (unsigned cnt = 0; cnt < print_iterations; ++cnt) {
27 for (unsigned i = 0; i < inc_iterations; ++i) {
28 pthread_mutex_lock(&mtx);
30 pthread_mutex_unlock(&mtx);
33 pthread_mutex_lock(&mtx);
34 printf("\033[31mThread: %d\n", globalcounter);
35 pthread_mutex_unlock(&mtx);
41 static int diff_ms(struct timeval *t1, struct timeval *t2)
43 return (((t1->tv_sec - t2->tv_sec) * 1000000) +
44 (t1->tv_usec - t2->tv_usec))/1000;
48 int main(int argc, char **argv)
50 (void)argc; (void)argv;
54 struct timeval start, stop;
56 gettimeofday(&start, NULL);
58 pthread_mutex_init(&mtx, 0);
60 int res = pthread_create(&pt, NULL, thread, NULL);
66 for (unsigned cnt = 0; cnt < print_iterations; ++cnt) {
68 for (unsigned i = 0; i < inc_iterations; ++i) {
69 pthread_mutex_lock(&mtx);
71 pthread_mutex_unlock(&mtx);
74 pthread_mutex_lock(&mtx);
75 printf("\033[32mMain: %d\n", globalcounter);
76 pthread_mutex_unlock(&mtx);
79 pthread_join(pt, NULL);
81 gettimeofday(&stop, NULL);
83 unsigned long ms = diff_ms(&stop, &start);
84 printf("Start %ld.%ld --- Stop %ld.%ld --- Diff %ld.%03ld\n",
85 start.tv_sec, start.tv_usec, stop.tv_sec, stop.tv_usec,
86 ms / 1000, ms % 1000);
88 //enter_kdebug("before return");