Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Discussion 5 Sample: #include #include #include using namespace std; mutex Out; void println(string s){ Out.lock(); cout Out.unlock(); } class Library{ private: int maxCustomers; int numberOfCustomers;

image text in transcribed

Discussion 5 Sample:

#include

#include

#include

using namespace std;

mutex Out;

void println(string s){

Out.lock();

cout

Out.unlock();

}

class Library{

private:

int maxCustomers;

int numberOfCustomers;

int numberOK;

int numberExpired;

int numberOfBooks;

mutex shared_var;

public:

Library(int);

void close();

int findBooks(int);

bool checkOut(int, bool, int);

};

Library::Library(int mc){

maxCustomers = mc;

numberOfCustomers = 0;

numberOK = 0;

numberExpired = 0;

numberOfBooks = 0;

}

void Library::close(){

cout

cout

}

int Library::findBooks(int id){

return (id * id + id + 1);

}

bool Library::checkOut(int id, bool expired, int books){

shared_var.lock();

numberOfCustomers++;

if(expired){

numberExpired++;

}else{

numberOK++;

numberOfBooks += books;

}

shared_var.unlock();

return(!expired);

}

class Student{

private:

int id;

bool cardExpired;

Library *library;

public:

Student();

Student(int, bool, Library*);

void operator() ();

void run();

};

Student::Student(){

id = 0;

cardExpired = false;

library = NULL;

}

Student::Student(int i, bool x, Library* lib){

id = i;

cardExpired = x;

library = lib;

}

void Student::operator() (){

run();

}

void Student::run(){

int books;

books = library->findBooks(id);

if(library->checkOut(id, cardExpired, books)){

string sOut = "Student " + to_string(id) +

"leaves library with " + to_string(books) + "books";

println(sOut);

}

}

int main(int argc, const char *argv[]){

Library*library = new Library(3);

thread studentA{

Student(0, false, library)

};

thread studentB{

Student(1, true, library)

};

thread studentC{

Student(2, false, library)

};

println("All studens are visiting the library.");

studentA.join();

studentB.join();

studentC.join();

library->close();

return 0;

}

Change the program in Discussion Question 5 as follows: a. Use pthreads instead of C++ version 11 threads. b. Instead of using three threads (for Students A, B and C), make the program work with n threads. Use a symbolic constant value: NSTUDENTS to define this value. You must use arrays to keep track of the different threads. Odd id values are to be associated with expired library cards. Change the program to ensure that all students have found their books (returned from findBooks) before allowing any students (threads) to continue with checking out their books (invoking checkOut). Add an output message (all books found) that is to be issued just once when all students have returned from findBooks. For full credit for this part, solve it without using the pthread_barrier-functions. Turn in a listing of your code and output that shows it works correctly with NSTUDENTS equal to 10. On your listing, highlight the changes you made to the program in Discussion Question 5. All threads must exit successfully and the messages issued by the Library close routine must be displayed. Output with 5 Students with solution to parts a - c: Student O found 1 All students are visiting the library. Student 4 found 21 Student 2 found 7 Student 3 found 13 Student i found 3 all books found Student O leaves library with 1 books Student 4 leaves library with 21 books Student 2 leaves library with 7 books Number of customers: 5 Number of books checked out: 29 Change the program in Discussion Question 5 as follows: a. Use pthreads instead of C++ version 11 threads. b. Instead of using three threads (for Students A, B and C), make the program work with n threads. Use a symbolic constant value: NSTUDENTS to define this value. You must use arrays to keep track of the different threads. Odd id values are to be associated with expired library cards. Change the program to ensure that all students have found their books (returned from findBooks) before allowing any students (threads) to continue with checking out their books (invoking checkOut). Add an output message (all books found) that is to be issued just once when all students have returned from findBooks. For full credit for this part, solve it without using the pthread_barrier-functions. Turn in a listing of your code and output that shows it works correctly with NSTUDENTS equal to 10. On your listing, highlight the changes you made to the program in Discussion Question 5. All threads must exit successfully and the messages issued by the Library close routine must be displayed. Output with 5 Students with solution to parts a - c: Student O found 1 All students are visiting the library. Student 4 found 21 Student 2 found 7 Student 3 found 13 Student i found 3 all books found Student O leaves library with 1 books Student 4 leaves library with 21 books Student 2 leaves library with 7 books Number of customers: 5 Number of books checked out: 29

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Project Management A Systems Approach to Planning Scheduling and Controlling

Authors: Harold Kerzner

10th Edition

978-047027870, 978-0-470-5038, 470278706, 978-0470278703

Students also viewed these Databases questions