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

pthread_t tid[2];

pthread_mutex_t lock;

void* task1(void *arg)
{
    pthread_t ttid = pthread_self();
    printf("\n Thread %08x started\n", ttid );
    pthread_mutex_lock(&lock);
    printf("\n     Thread %08x performs critical section\n", ttid );
    sleep(1);
    printf("\n     Thread %08x is about to be done with its critical section\n", ttid );
    pthread_mutex_unlock(&lock);
    printf("\n Thread %08x finished\n", ttid);
    pthread_exit(0);
}

int main(void)
{
    pthread_mutex_init(&lock, NULL);

    pthread_create(&(tid[0]), NULL, &task1, NULL);
    pthread_create(&(tid[1]), NULL, &task1, NULL);

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);
    
    pthread_mutex_destroy(&lock);

    exit(0);
} 
