You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Your Windows build number: (Type ver at a Windows Command Prompt)
Windows: 10.0.18363.720
Distro: Ubuntu-18.04
What you're doing and what's happening: (Copy&paste the full set of specific command-line steps necessary to reproduce the behavior, and their output. Include screen shots if that helps demonstrate the problem.)
Here is a simple case: mutex_priority.cpp
#include<array>
#include<cstdio>
#include<thread>
#include<pthread.h>// A snippet from Physx3.4 which implements mutex for unixclassMutexUnixImpl
{
public:MutexUnixImpl()
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
pthread_mutex_init(&_lock, &attr);
pthread_mutexattr_destroy(&attr);
}
~MutexUnixImpl()
{
pthread_mutex_destroy(&_lock);
}
voidlock()
{
pthread_mutex_lock(&_lock);
}
voidunlock()
{
pthread_mutex_unlock(&_lock);
}
private:pthread_mutex_t _lock;
size_t _owner = 0;
};
// record&print worker thread's executionsstructCounter
{
// use 3 workersconstexprstaticint WORKER_COUNT = 3;
// _count_array[N] records the N'th thread's exec count
std::array<int, WORKER_COUNT> _count_array;
MutexUnixImpl _mtx;
Counter()
{
for (int i = 0; i < WORKER_COUNT; ++i)
{
_count_array[i] = 0;
}
}
intinc(size_t i)
{
if (i < WORKER_COUNT)
{
return ++_count_array[i];
}
return -1;
}
voidPrint()
{
for (int i = 0; i < WORKER_COUNT; ++i)
{
printf("counter: %d -> %d\n", i, _count_array[i]);
}
}
voidWorker(int id)
{
while (true)
{
_mtx.lock();
int c = inc(id);
printf("[%d] working, count=%d\n", id, c);
_mtx.unlock();
}
}
voidStartAllWorkers()
{
for (int i = 0; i < WORKER_COUNT; ++i)
{
std::thread(&Counter::Worker, this, i).detach();
}
}
};
intmain(int argc, char **argv)
{
Counter counter;
counter.StartAllWorkers();
// wait 3 secs// then use another thread to print counterstd::this_thread::sleep_for(std::chrono::seconds(3));
std::thread([&counter]()
{
// stop all workers and print counter for 2 secs
counter._mtx.lock();
counter.Print();
std::this_thread::sleep_for(std::chrono::seconds(2));
counter._mtx.unlock();
}).detach();
// pendingstd::this_thread::sleep_for(std::chrono::seconds(300));
return0;
}
Thank-you for the detailed repro. It is always appreciated. The thread starvation didn't get better on WSL1 between 18363 and 21370. It is better on WSL2, natch.
This issue has been automatically closed since it has not had any activity for the past year. If you're still experiencing this issue please re-file this as a new issue or feature request.
Please fill out the below information:
ver
at a Windows Command Prompt)Windows:
10.0.18363.720
Distro:
Ubuntu-18.04
Here is a simple case:
mutex_priority.cpp
compile:
g++ -pthread -std=c++11 mutex_priority.cpp
run:
./a.out
Work threads ran very well at first.
After 3 seconds, only one worker thread printed the count and the other workers never awoke as if they were starving.
Commenting the line
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
fixed it.I've checked #1006 which was tagged
fixedin1809
and got a bit puzzled.Could someone help?
thx
The text was updated successfully, but these errors were encountered: