It's not the best place to discuss this topic here ..... have a look here:
https://stackoverflow.com/questions/6460542/performance-of-pthread-mutex-lock-unlock On Thu, Sep 3, 2020 at 9:46 AM Niresh <[email protected]> wrote: > > 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 > On Thursday, September 3, 2020 at 12:55:48 PM UTC+5:30 Niresh wrote: > >> 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); >> } >> } >> > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/beagleboard/1d7fc600-e5da-4fbc-b26a-fa1f0c3a3514n%40googlegroups.com > <https://groups.google.com/d/msgid/beagleboard/1d7fc600-e5da-4fbc-b26a-fa1f0c3a3514n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/CAF%2BMRt%3DAQktRK_NgTmxFCvR3NVyudd9NBgwWTt7h-5Ub7ftk7w%40mail.gmail.com.
