#include <assert.h>
#include <cosmo.h>
#include <pthread.h>
#define ITERATIONS 10000000l
long counter;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void hit(void) {
pthread_mutex_lock(&lock);
++counter;
pthread_mutex_unlock(&lock);
}
long count(void) {
return counter;
}
void *worker(void *arg) {
for (long i = 0; i < ITERATIONS; ++i)
hit();
return 0;
}
int main(void) {
int threads = cosmo_cpu_count();
pthread_t th[threads];
for (long i = 0; i < threads; ++i)
pthread_create(&th[i], 0, worker, 0);
for (long i = 0; i < threads; ++i)
pthread_join(th[i], 0);
assert(count() == threads * ITERATIONS);
}