Fix ClientProcess start race condition

This commit is contained in:
Jack Andersen 2016-04-08 16:47:58 -10:00
parent 6cfb952ecf
commit 5fc5442286
2 changed files with 11 additions and 0 deletions

View File

@ -16,6 +16,7 @@ class ClientProcess
{ {
std::mutex m_mutex; std::mutex m_mutex;
std::condition_variable m_cv; std::condition_variable m_cv;
std::condition_variable m_initCv;
int m_verbosity; int m_verbosity;
public: public:
@ -71,6 +72,7 @@ private:
ClientProcess& m_proc; ClientProcess& m_proc;
std::thread m_thr; std::thread m_thr;
BlenderToken m_blendTok; BlenderToken m_blendTok;
bool m_didInit = false;
Worker(ClientProcess& proc); Worker(ClientProcess& proc);
void proc(); void proc();
}; };

View File

@ -63,6 +63,11 @@ void ClientProcess::Worker::proc()
while (m_proc.m_running) while (m_proc.m_running)
{ {
std::unique_lock<std::mutex> lk(m_proc.m_mutex); std::unique_lock<std::mutex> lk(m_proc.m_mutex);
if (!m_didInit)
{
m_proc.m_initCv.notify_one();
m_didInit = true;
}
while (m_proc.m_pendingQueue.size()) while (m_proc.m_pendingQueue.size())
{ {
std::unique_ptr<Transaction> trans = std::move(m_proc.m_pendingQueue.front()); std::unique_ptr<Transaction> trans = std::move(m_proc.m_pendingQueue.front());
@ -89,7 +94,11 @@ ClientProcess::ClientProcess(int verbosityLevel)
#endif #endif
m_workers.reserve(cpuCount); m_workers.reserve(cpuCount);
for (int i=0 ; i<cpuCount ; ++i) for (int i=0 ; i<cpuCount ; ++i)
{
std::unique_lock<std::mutex> lk(m_mutex);
m_workers.emplace_back(*this); m_workers.emplace_back(*this);
m_initCv.wait(lk);
}
} }
const ClientProcess::BufferTransaction* const ClientProcess::BufferTransaction*