#include #include #include pthread_spinlock_t lock; void *thread (int p) { while (1) { printf("Thread %d ready\n", p); pthread_spin_lock(&lock); printf("Thread %d inside C.S.\n", p); usleep(1000000 * p); printf("Thread %d leaving C.S.\n", p); pthread_spin_unlock(&lock); usleep(1000000); } } int main () { pthread_t thr_id; pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE); pthread_create(&thr_id, NULL, thread, (void *) 1); pthread_create(&thr_id, NULL, thread, (void *) 2); pthread_create(&thr_id, NULL, thread, (void *) 3); pthread_create(&thr_id, NULL, thread, (void *) 4); pthread_create(&thr_id, NULL, thread, (void *) 5); pthread_create(&thr_id, NULL, thread, (void *) 6); usleep(50000000); //simulation time return 0; }