I am using beagle Bone Black with debian image. Could any please suggest how to optimize the application code using pthread_mutex_lock. Let me describe the solution I am looking at.
For example, I have two thread sharing a global shared memory variable as below
Sorry I did not finish the thing and got posted accidentally
For example, I have two thread sharing a global shared memory variable as below. The variable shmPtr->status is protected with mutex lock in both the function. Though there is a sleep(1/2) between mutex lock inside “for loop” in the task1 function , I could not access the shmPtr->status in task2 when required and have to wait until “for loop” exit in the task1 function, this takes around 50 seconds to take control of shmPtr->status by the task2 function.
I am wondering why the task1 function is not releasing the mutex lock even with an sleep(1/2).
Since "1/2" is integer division, the result is the same as sleep(0) --
which may return immediately and locking the mutex before the other thread
can even start.
Providing a minimum compilable example demonstrating the problem would
also be of assistance. As it is we are left with either trying to write a
working program for testing, or working theoretically...