Optimization of pthread_mutex_lock in the application code

65 views
Skip to first unread message

Niresh

unread,
Sep 3, 2020, 3:25:48 AM9/3/20
to BeagleBoard
Hi,

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

thr_id1 = pthread_create ( &p_thread1, NULL, (void *)execution_task1, NULL );
thr_id2 = pthread_create ( &p_thread2, NULL, (void *)execution_task2, NULL );  

void execution_task1()
{
    
     for(int i = 0;i < 100;i++) 
     {
          pthread_mutex_lock(&lock);  
                shmPtr->status = 1;
pthread_mutex_unlock(&lock);
sleep(1/2);
     }
}

Niresh

unread,
Sep 3, 2020, 3:46:35 AM9/3/20
to BeagleBoard

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).

thr_id1 = pthread_create ( &p_thread1, NULL, (void *)execution_task1, NULL );
thr_id2 = pthread_create ( &p_thread2, NULL, (void *)execution_task2, NULL );  

void execution_task1()
{
     for(int i = 0;i < 100;i++) 
     {
          //100 lines of application code running here
          pthread_mutex_lock(&lock);  
                shmPtr->status = 1;  //shared memory variable 
         pthread_mutex_unlock(&lock);
         sleep(1/2);
     }
}

void execution_task2()
{
           //100 lines of application code running here
          pthread_mutex_lock(&lock);  
                shmPtr->status = 0;  //shared memory variable
         pthread_mutex_unlock(&lock);
         sleep(1/2);
}

Regards,
NK

Micka

unread,
Sep 3, 2020, 3:46:43 AM9/3/20
to beagl...@googlegroups.com
Why would you optimize the mutex ?

sleep(1/2); ==> doesn't work , because 1/2 is cast to 0.

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/fe4074cd-ff97-4e7c-8d68-02ba9a0d5678n%40googlegroups.com.

Micka

unread,
Sep 3, 2020, 3:49:09 AM9/3/20
to beagl...@googlegroups.com
It's not the best place to discuss this topic here .....

have a look here:



--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.

Niresh

unread,
Sep 3, 2020, 3:52:28 AM9/3/20
to beagl...@googlegroups.com
Thank you, Like you said sleep(1/2) won't work, then how to give a sleep of 500ms.

Regards,
NK

You received this message because you are subscribed to a topic in the Google Groups "BeagleBoard" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beagleboard/-9GlXf8Y9ns/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/CAF%2BMRt%3DAQktRK_NgTmxFCvR3NVyudd9NBgwWTt7h-5Ub7ftk7w%40mail.gmail.com.

Micka

unread,
Sep 3, 2020, 4:22:48 AM9/3/20
to beagl...@googlegroups.com
Usleep function.... Google! Best place to find your answer!

Dennis Lee Bieber

unread,
Sep 3, 2020, 1:52:27 PM9/3/20
to Beagleboard
On Thu, 3 Sep 2020 00:46:16 -0700 (PDT), in
gmane.comp.hardware.beagleboard.user Niresh
<nireshsharanya-Re5...@public.gmane.org> wrote:


>
>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...



--
Dennis L Bieber

Reply all
Reply to author
Forward
0 new messages