#include "stdio.h"
#include "pthread.h"

pthread_t thread1, thread2;
pthread_mutex_t lock1, lock2, lock3;
pthread_cond_t cond;



void f4()
{
	int i=0; 
	while(i++<10) 
	{
		while( i> 5) 
		{
			break; 
			i*=2;
		} 
		i++;
	}
	pthread_mutex_lock(&lock2);

	pthread_mutex_unlock(&lock2);
}

void f3() {
	printf("xxxxxx"); 
	if((int)(&lock1) % 3 == 1)
	{ 
		int i=10; 
		while(i--> 0) 
			f4(); 
	} /* 
	{
		for(int i=10; i> 0; i--)
			f4(); 
	} */
}

void* f1 ( void* arg ) {

	pthread_mutex_lock(&lock1);

	pthread_mutex_lock(&lock3); 
	pthread_cond_wait(&cond, &lock3); //pthread_cond_wait(&cond, &lock1); 
	pthread_mutex_unlock(&lock3);
	
	printf("Test thread 1\n");
	
	if((int)(&lock1) % 7 == 1)
	{
		goto _exit_;
	}
	
	if((int)(&lock2) % 3 == 1) 
	{ printf("test\n");
		f3();
	}

_exit_:
	pthread_mutex_unlock(&lock1);
	
	return NULL;
}
void* f2 ( void* arg ) {

	pthread_mutex_lock(&lock2);

	printf("Test thread 2\n");

	pthread_mutex_lock(&lock1);

	
	pthread_mutex_unlock(&lock1);
	pthread_mutex_unlock(&lock2);
	
	return NULL;
}

int main ( void ) 
{

	pthread_create(&thread1, NULL, f1, NULL);
	pthread_create(&thread2, NULL, f2, NULL);

	pthread_join(thread1, NULL);
	pthread_join(thread2, NULL);

	printf("Finished.\n");
	return 0;
}
