pthread_mutex not updating fast enough, so one thread “hogs” the lock.

[亡魂溺海] 提交于 2019-12-25 04:53:07

问题


I have a c++ program where I'm creating multiple threads and having them access a shared array.

Everytime I want one thread to access the array, I call

pthread_mutex_lock(&mutex);

access the array and then call

pthread_mutex_unlock(&mutex);

All threads loop continuously until they've accessed the array a certain number of times. Thus they aren't just accessing the array once, but instead accessing it several times.

Now, when I execute my program just as it is, whichever thread gets the mutex first (usually the first thread created) executes until it completes, before allowing another thread to access the array.

If I add a simple sleep() behind

pthread_mutex_unlock(&mutex);

Then the threads will alternate accessing the array (which is what I want). I would rather not have to use sleep() however to accomplish this.

To the best of my knowledge I believe this is what's happening:

Thread A locks the mutex and begins accessing the array
Thread B tries to lock the mutex but finds its locked, therefore it waits
Thread A finishes with the array and unlocks the mutex
Thread A loops and relocks the mutex before Thread B realizes that Thread A unlocked the matrix

Thus Thread A continues to access the array until it's accessed it n times, then completes and Thread B accesses the array n times

Is there anyway to make the threads waiting (for the mutex to unlock) to update faster and grab the lock as soon as it's unlocked?
I would prefer the above output to be something more along the lines of:

Thread A locks the mutex and begins accessing the array
Thread B tries to lock the mutex but finds its locked, therefore it waits
Thread A finishes with the array and unlocks the mutex
Thread B sees the mutex is unlocked and locks it
Thread A loops and tries to lock the mutex, but finds its locked, therefore it waits
... etc.

回答1:


Instead of sleep() you can take a look at pthread_yield()/sched_yield(), which will result in Thread A yielding the CPU before acquiring the mutex again right after releasing it. Mutexes don't queue and don't guarantee fairness.

Alternatively, signal the other threads using a condition variable.



来源:https://stackoverflow.com/questions/16138072/pthread-mutex-not-updating-fast-enough-so-one-thread-hogs-the-lock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!