#include "thread.c"

/* what are we doing */
char *things = "context switches";			
/* how many have we done */
unsigned long things_done=0;

/* threads */
pthread_t thread_id;
void *thread(void*);

/* globals */
unsigned long *context_switched = &things_done;

static pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;

void *thread1(void *arg) {
  while (1) {
    pthread_mutex_lock(&mutex1);
    (*context_switched)++;
    pthread_mutex_unlock(&mutex2);
  }
}

void *thread2(void *arg) {
  while (1) {
    pthread_mutex_lock(&mutex2);
    (*context_switched)++;
    pthread_mutex_unlock(&mutex1);
  }
}

void do_test(void) {

  pthread_t th1, th2;

  pthread_mutex_lock( &mutex1 );
  pthread_mutex_lock( &mutex2 );

  pthread_create(&th1, NULL, thread1, (void*)NULL );
  pthread_create(&th2, NULL, thread2, (void*)NULL );

  pthread_mutex_unlock( &mutex1 );

  /* we don't need to do anything else */ 
  sleep(10);

}

