The program is designed to create several threads in which each thread increases the total variable by 10000, using a for loop, which increases it by 1 at each iteration. Both a mutex lock lock and a lock lock (wait) are required. According to what I found out, the mutex version should be faster than direct locking. But what I implemented gave me the opposite answer ...
This is the implementation of each thread in the mutex version:
void *incr(void *tid) { int i; for(i = 0; i < 10000; i++) { pthread_mutex_lock(&the_mutex);
And this is the implementation in the spin lock version:
void *incr(void *tid) { int i; for(i = 0; i < 10000; i++) { enter_region((int)tid); //Grab the lock sharedVar++; //Increment the shared variable leave_region((int)tid); //Release the lock } pthread_exit(0); } void enter_region(int tid) { interested[tid] = true; //Show this thread is interested turn = tid; //Set flag while(turn == tid && other_interested(tid)); //Busy waiting } bool other_interested(int tid) //interested[] is initialized to all false { int i; for(i = 0; i < tNumber; i++) if(i != tid) if(interested[i] == true) //There are other threads that are interested return true; return false; } void leave_region(int tid) { interested[tid] = false; //Depart from critical region }
I also repeated the process of creating and starting threads hundreds of times to make sure that runtime can be distinguished. For example, if tNumber is 4, and I repeated the program 1000 times, the mutex will take 2.22 seconds, and the lock will take 1.35 seconds. The difference increases with the number. Why is this happening? Is my code wrong?
source share