CMPSC 32 Lecture Notes - Lecture 17: Linearizability, Memory Management, Merge Sort

14 views5 pages
8 Jan 2020
School
Course
Professor

Document Summary

// main. cpp void callback1(string val, int num) { cout << "----" << endl; cout << "thread with str val: " << val << endl; cout << "num: " << num << endl; cout << "thread id: " << this_thread::get_id() << endl; sleep(5); cout << "----" << endl; int main() { thread t1(callback1, "one", 1); cout << "t1 id: " << t1. get_id() << endl; t1. join(); Bank(); double getbalance(); void deposit(double amount); private: double totalbalance; std::mutex m; Bank::bank() { totalbalance = 0; void bank::deposit(double amount) { m. lock(); totalbalance += amount; m. lock(); totalbalance += amount; m. unlock(); double bank::getbalance() { return totalbalance; #include "bank. h" using namespace std; void run(bank* b) { for (int i = 0; i < 10000; i++) b->deposit(10); int main() { Bank* bank = new bank(); thread t1(run,bank); thread t2(run,bank); t1. join(); t2. join(); cout << "----" << endl; cout << bank->getbalance() << endl: format. Similar to midterms, but designed to take two hours (~ twice as long as the midterm)

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers