mirror of
https://github.com/aportelli/LatAnalyze.git
synced 2024-11-10 00:45:36 +00:00
better interface for critical threaded sections
This commit is contained in:
parent
fe8c6c6630
commit
78351a9b76
@ -12,15 +12,15 @@ int main(void)
|
||||
{
|
||||
pool.addJob([i, &pool](void)
|
||||
{
|
||||
pool.critical([i](void)
|
||||
{
|
||||
unique_lock<mutex> lock(pool.getMutex());
|
||||
cout << "job " << i << " wait for " << i*100 << " ms" << endl;
|
||||
}
|
||||
});
|
||||
this_thread::sleep_for(chrono::milliseconds(i*100));
|
||||
pool.critical([i](void)
|
||||
{
|
||||
unique_lock<mutex> lock(pool.getMutex());
|
||||
cout << "job " << i << " done" << endl;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
pool.terminate();
|
||||
|
@ -92,6 +92,14 @@ void ThreadPool::addJob(Job newJob)
|
||||
condition_.notify_one();
|
||||
}
|
||||
|
||||
// critical section ////////////////////////////////////////////////////////////
|
||||
void ThreadPool::critical(Job fn)
|
||||
{
|
||||
unique_lock<mutex> lock(mutex_);
|
||||
|
||||
fn();
|
||||
}
|
||||
|
||||
// wait for completion /////////////////////////////////////////////////////////
|
||||
void ThreadPool::terminate(void)
|
||||
{
|
||||
|
@ -37,6 +37,8 @@ public:
|
||||
std::mutex & getMutex(void);
|
||||
// add jobs
|
||||
void addJob(Job newJob);
|
||||
// critical section
|
||||
void critical(Job fn);
|
||||
// wait for completion and terminate
|
||||
void terminate(void);
|
||||
private:
|
||||
|
Loading…
Reference in New Issue
Block a user