Question: Its a C++ programming project: this is the ReadMe.cpp (questions): here's the following code: // readersWriters.cpp : This file contains the 'main' function. Program execution
Its a C++ programming project:
this is the ReadMe.cpp (questions):

here's the following code:
// readersWriters.cpp : This file contains the 'main' function. Program execution begins and ends there. //
#include
std::random_device e; std::uniform_int_distribution dist(1, 9); std::vector
//Size of the vector v constexpr auto DATA_SIZE = 100; constexpr auto NUM_TRIALS = 10;
/* Readers writer setup. The common data is the * vector of integers v. The writers write a random * value from 1 to 9 followed by its negative such * that the total sum is 0. for example * [2,-2,2,-2,2,-2....] * The readers check that the sum in the vector is zero * if not it will print the sum (which means data is corrupted) */
/* You MUST NOT remove any of the code below. ADD to it sync * primites so that it works. Basically using c++ to implement the * solution we saw in class (it is in the lecture notes) */
class Reader { public:
void operator() () { int sum = 0;
for (auto x : v) { sum += x; std::this_thread::sleep_for(std::chrono::milliseconds(10)); }
if (sum != 0) std::cout
class Writer {
public: Writer() { } void operator() () { int value = dist(e);
for (auto& x : v) { x = value; value = -value; std::this_thread::sleep_for(std::chrono::milliseconds(10)); }
} };
int main() { int value = 1; for (int i = 0; i mythreads; for (int i = 0; i
}
Here's the following Answers (Instructions on solving) of the following 2 questions:
instructions of question part 1:

Instructions of question Part 2:

## 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 std:: mutex *** and std::shared_mutex "? What is the difference between 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. Answer A lock guard is an object that manages a mutex object by keeping it always locked. On construction, the mutex object is locked by the calling thread, and on destruction, the mutex is unlocked. It is the simplest lock, and is specially useful as an object with automatic duration that lasts until the end of its context. In this way, it guarantees the mutex object is properly unlocked in case an exception is thrown. Though that the lock_guard object does not manage the lifetime of the mutex object in any way: the duration of the mutex object shall extend at least until the destruction of the lock_guard that locks it. Answer The shared_mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads. In contrast to other mutex types which facilitate exclusive access, a shared_mutex has two levels of access: shared - several threads can share ownership of the same mutex, exclusive - only one thread can own the mutex. Shared mutexes are usually used in situations when multiple readers can access the same resource at the same time without causing data races, but only one writer can do so
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
