#include <pthread.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>

pthread_t pt[2];
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

extern "C" void* start (void *extra __attribute__((__unused__))) {
    { int r = pthread_mutex_lock(&mutex);    assert(r==0); }
    printf("T%lx got lock\n", pthread_self());
    sleep(1);
    printf("T%lx releasing lock\n", pthread_self());
    { int r = pthread_mutex_unlock(&mutex); assert(r==0); }
    return 0;
}

void create_pthread(void) {
    for (int i=0; i<2; i++) {
	int r = pthread_create(&pt[i], 0, start, NULL);
	assert(r==0);
    }
}

void join_pthread (void) {
    for (int i=0; i<2; i++) {
	int r = pthread_join(pt[i], NULL);
	assert(r==0);
    }
}

void foo (void) {
}

int cilk_main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) {
    create_pthread();
    cilk_spawn foo();
    cilk_sync;
    join_pthread();
    return 0;
}
