Question
I have 15hrs to submit this programming project plz help! The project is in four parts. Each part has a few questions that you have
I have 15hrs to submit this programming project plz help!
The project is in four parts. Each part has a few questions that you have to answer and an implementation you have to code. Write the answer in the _place indicated in this file_.
## Part 1 Read the code in example1.cpp. It creates four threads: two to run the function doit and the other two to run a function object. The four threads run concurrently with two threads run doit() incrementing/decrementing the global variable y and two threads run function objects incrementing/decrementing the local variable x. From the output you can see that there is data corruption since both x and y should be 0. To fix this problem uncomment the line with #define SYNC . ### Question We learned that a mutex m is locked with a wait(m), (in C++ it is m.lock() ) and unlocked with signal(m) (in C++ it is m.unlock() ). How is this done in example.cpp? What is a lock_guard ? Write your answer here by editing this file (no more that a few lines)
#### Answer
Write your answer here
## Part 2
### Question
What is the difference between std::mutex and std::shared_mutex ? Write your answer below
#### Answer
Write your answer here ### Implementation
Implement the readers writers solution we discussed in class. Use *main.cpp* in the folder _readersWriters_ as a start. Do not *remove* any code just *add* synchronization code to fix the access problem. As you might have guessed you need to use std::shared_mutex here.
## Part 3
### Question
Describe condition variables in C++ and how they used (a few lines)
#### Answer
Write your answer here
### Implementation
Read the file _main.cpp_ in the folder *waitforothers. We have multiple threads running the **same* code in function void thread(std::string s) . Each thread runs through two parts: the first it prints "phase 1" and in the second it prints its name followed by 'done'. Without synchronization these two phases can be interleaved among all threads as you can see by running this program. Use condition variables and mutex to synchronize theses threads as follows: no thread can start phase two until all threads have completed phase 1. In terms of output, since we run 10 threads you must guarantee that there are 10 'phase 1' printouts before any 'done' printout. The order in which threads print 'phase one' or 'done' does not matter as long as all print 'phase 1' before *any* of them prints 'done'.
## Part 4
You need to develop a multithreaded application that interacts with the user even if it is performing another task in the background. For example, the app could be downloading a file from the internet and still allows you to play the guess the number game. Read the file *main.cpp* in the folder _async_. The function download "simulates" a task that takes time (e.g. downloading a large file) by sleeping for a certain time. After that it returns a value. In the main you need to start the function _download_ asynchronously and while it is performing its task you play the guess the number game until _download_ finishes.
Using c++ async and futures to implement the above behavior.
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