Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Given the following program, what, if anything should be done? #include #include #include #include using namespace std; mutex mtx 1 , mtx 2 ; void
Given the following program, what, if anything should be done?
#include
#include
#include
#include
using namespace std;
mutex mtxmtx;
void john
for auto i:
mtxlock;
thisthread::sleepforchrono::milliseconds;
mtxlock;
cout "John has the locks" endl;
mtxunlock;
mtxunlock;
void jane
for auto i:
mtxlock;
thisthread::sleepforchrono::milliseconds;
mtxlock;
cout "Jane has the locks" endl;
mtxunlock;
mtxunlock;
int main
auto t threadjohn;
auto t threadjane;
tjoin;
tjoin;
Group of answer choices
void jane
for auto i:
mtxlock;
cout "Jane has the lock" endl;
mtxunlock;
NEVER USE TWO MUTEXES together, so remove mtx
void jane
for auto i:
lock mtx mtx;
cout "Jane has the locks" endl;
Because lock automatically unlocks when it goes out of scope.
void jane
for auto i:
mtxlockmtx;
cout "Jane has the locks" endl;
mtxunlock;
mtxunlock;
Because mtx will not lock until it locks mtx
void jane
for auto i:
lock mtx mtx;
cout "Jane has the locks" endl;
mtxunlock;
mtxunlock;
Because lock does not acquire the locks until it can get both.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started