// buffer.c
// Bounded-buffer Producer/Consumer problem with semaphores
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void* func(void* arg)
{
// detach the current thread
// from the calling thread
pthread_detach(pthread_self());
printf("Inside the thread\n");
// exit the current thread
pthread_exit(NULL);
}
void fun()
{
pthread_t ptid;
// Creating a new thread
pthread_create(&ptid, NULL, &func, NULL);
printf("This line may be printed" " before thread terminates\n");
// The following line terminates
// the thread manually
// pthread_cancel(ptid);
// Compare the two threads created
if(pthread_equal(ptid, pthread_self()))
printf("Threads are equal\n"); else
printf("Threads are not equal\n");
// Waiting for the created thread to terminate
pthread_join(ptid, NULL);
printf("This line will be printed" " after thread ends\n");
pthread_exit(NULL);
}
// Driver code
int main()
{
fun();
return 0;
}
Ly8gYnVmZmVyLmMKLy8gQm91bmRlZC1idWZmZXIgUHJvZHVjZXIvQ29uc3VtZXIgcHJvYmxlbSB3aXRoIHNlbWFwaG9yZXMKI2luY2x1ZGUgPHB0aHJlYWQuaD4gCiNpbmNsdWRlIDxzdGRpby5oPiAKI2luY2x1ZGUgPHN0ZGxpYi5oPiAKICAKdm9pZCogZnVuYyh2b2lkKiBhcmcpIAp7IAogICAgLy8gZGV0YWNoIHRoZSBjdXJyZW50IHRocmVhZCAKICAgIC8vIGZyb20gdGhlIGNhbGxpbmcgdGhyZWFkIAogICAgcHRocmVhZF9kZXRhY2gocHRocmVhZF9zZWxmKCkpOyAKICAKICAgIHByaW50ZigiSW5zaWRlIHRoZSB0aHJlYWRcbiIpOyAKICAKICAgIC8vIGV4aXQgdGhlIGN1cnJlbnQgdGhyZWFkIAogICAgcHRocmVhZF9leGl0KE5VTEwpOyAKfSAKICAKdm9pZCBmdW4oKSAKeyAKICAgIHB0aHJlYWRfdCBwdGlkOyAKICAKICAgIC8vIENyZWF0aW5nIGEgbmV3IHRocmVhZCAKICAgIHB0aHJlYWRfY3JlYXRlKCZwdGlkLCBOVUxMLCAmZnVuYywgTlVMTCk7IAogICAgcHJpbnRmKCJUaGlz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