broken, and what was changed in the fixed deadlock code to remove the deadlock. Thank you in

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

I have uploaded two types of code, a deadlock (fixed) code, and a deadlock (broken) code. I wanted to know why the broken deadlock code is broken, and what was changed in the fixed deadlock code to remove the deadlock. Thank you in advance. 

#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
mutex mutex1, mutex2;
void ThreadA()
{
// Solves deadlock problem Why?
mutex1.lock();
cout << "Thread A" << endl;
mutex2.lock();
mutex2.unlock();
mutex1.unlock();
}
void ThreadB()
{
// Solves deadlock problem - Why?
mutex1.lock();
cout << "Thread B" << endl;
mutex2.lock();
mutex1.unlock();
mutex2.unlock();
}
void ExecuteThreads ()
{
thread t1(ThreadA);
thread t2(ThreadB);
cout << "Deadlocked code (fixed)...!" << "\n";
cout << "Program will end...!" << "\n";
// Starting the threads.
t1.join();
t2.join();
cout << "Finished" << endl;
}
int main()
{
cout << "This program was written by James L. Bowman III.\n\n";
Execute Threads ();
Transcribed Image Text:#include <iostream> #include <thread> #include <mutex> using namespace std; mutex mutex1, mutex2; void ThreadA() { // Solves deadlock problem Why? mutex1.lock(); cout << "Thread A" << endl; mutex2.lock(); mutex2.unlock(); mutex1.unlock(); } void ThreadB() { // Solves deadlock problem - Why? mutex1.lock(); cout << "Thread B" << endl; mutex2.lock(); mutex1.unlock(); mutex2.unlock(); } void ExecuteThreads () { thread t1(ThreadA); thread t2(ThreadB); cout << "Deadlocked code (fixed)...!" << "\n"; cout << "Program will end...!" << "\n"; // Starting the threads. t1.join(); t2.join(); cout << "Finished" << endl; } int main() { cout << "This program was written by James L. Bowman III.\n\n"; Execute Threads ();
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
mutex mutex1, mutex2;
void ThreadA()
{
// Creates deadlock problem - Why?
mutex2.lock();
cout << "\nThread A\n";
mutex1.lock();
mutex2.unlock();
mutex1.unlock();
void ThreadB()
{
// Creates deadlock problem Why?
mutex1.lock();
cout << endl << "\nThread B\n";
mutex2.lock();
mutex1.unlock();
mutex2.unlock();
}
void ExecuteThreads ()
{
thread t1(ThreadA);
thread t2(ThreadB);
cout << "Deadlocked code (broken)...!" << "\n";
cout << "Program never ends...!" << "\n";
// Starting the threads.
t1.join();
t2.join();
// Threads never allow this line to be executed.
cout << "Finished" << endl;
int main()
{
cout << "This program was written by James L. Bowman III.\n\n";
ExecuteThreads();
Transcribed Image Text:#include <iostream> #include <thread> #include <mutex> using namespace std; mutex mutex1, mutex2; void ThreadA() { // Creates deadlock problem - Why? mutex2.lock(); cout << "\nThread A\n"; mutex1.lock(); mutex2.unlock(); mutex1.unlock(); void ThreadB() { // Creates deadlock problem Why? mutex1.lock(); cout << endl << "\nThread B\n"; mutex2.lock(); mutex1.unlock(); mutex2.unlock(); } void ExecuteThreads () { thread t1(ThreadA); thread t2(ThreadB); cout << "Deadlocked code (broken)...!" << "\n"; cout << "Program never ends...!" << "\n"; // Starting the threads. t1.join(); t2.join(); // Threads never allow this line to be executed. cout << "Finished" << endl; int main() { cout << "This program was written by James L. Bowman III.\n\n"; ExecuteThreads();
Expert Solution
Step 1

Computer Engineering homework question answer, step 1, image 1

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY