Change lock mechanism to std::lock_guard

This commit is contained in:
Lisa Julia Nebel 2024-07-15 14:16:18 +02:00
parent 4d0179cffc
commit c73a51650f
2 changed files with 4 additions and 8 deletions

View File

@ -51,9 +51,8 @@ public:
{
assert(0 <= runner->workerThreadIndex() && runner->workerThreadIndex() < runner->numWorkerThreads());
std::this_thread::sleep_for(std::chrono::milliseconds(mseconds_));
outputMutex.lock();
std::lock_guard<std::mutex> guard(outputMutex);
std::cout << "Sleep tasklet " << n_ << " of " << mseconds_ << " ms completed by worker thread " << runner->workerThreadIndex() << std::endl;
outputMutex.unlock();
}
private:
@ -67,9 +66,8 @@ void sleepAndPrintFunction()
{
int ms = 100;
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
outputMutex.lock();
std::lock_guard<std::mutex> guard(outputMutex);
std::cout << "Sleep completed by worker thread " << runner->workerThreadIndex() << std::endl;
outputMutex.unlock();
}
int SleepTasklet::numInstantiated_ = 0;

View File

@ -58,9 +58,8 @@ public:
assert(0 <= runner->workerThreadIndex() && runner->workerThreadIndex() < runner->numWorkerThreads());
std::cout << "Sleep tasklet " << id_ << " of " << mseconds_ << " ms starting sleep on worker thread " << runner->workerThreadIndex() << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(mseconds_));
outputMutex.lock();
std::lock_guard<std::mutex> guard(outputMutex);
std::cout << "Sleep tasklet " << id_ << " of " << mseconds_ << " ms completed by worker thread " << runner->workerThreadIndex() << std::endl;
outputMutex.unlock();
}
private:
@ -77,9 +76,8 @@ public:
void run() override
{
std::this_thread::sleep_for(std::chrono::milliseconds(mseconds_));
outputMutex.lock();
std::lock_guard<std::mutex> guard(outputMutex);
std::cout << "Failing sleep tasklet of " << mseconds_ << " ms failing now, on work thread " << runner->workerThreadIndex() << std::endl;
outputMutex.unlock();
throw std::logic_error("Intentional failure for testing");
}