/* Written By: Prawar Poudel 13 Feb 2018 This is written to demonstrate simple creation and waiting for pthread to terminate */ #include #include #include #define NUM_THREADS 100 int mutexProtectedGlobalVariable; int unprotectedProtectedGlobalVariable; pthread_mutex_t myMutex; //this function will update the value without any protection void *unprotectedThreadFunc(void* argument) { int i; for( i=0;i<10000;i++) unprotectedProtectedGlobalVariable++; } //this function will update the value without any protection void *protectedThreadFunc(void* argument) { int i; pthread_mutex_lock (&myMutex); for( i=0;i<10000;i++) mutexProtectedGlobalVariable++; pthread_mutex_unlock (&myMutex); } int main() { mutexProtectedGlobalVariable = 0; unprotectedProtectedGlobalVariable = 0; int i; //you can create these dynamically also pthread_t myThreads[NUM_THREADS]; int status = 0; printf("Calling unprotected set of threads\n"); //first set of five threads will call a function that will update the variable unprotected for(i=0;i