60 likes | 175 Views
Using C++11 to teach Concurrency and Parallelism Concepts . Dhananjai M. Rao ( raodm@miamioh.edu ) CSE Department, Oxford, OHIO. Motivation. Students consistently report steep, multifaceted learning curve and challenges due to: Introduction of new programming language
E N D
Using C++11 to teach Concurrency and Parallelism Concepts Dhananjai M. Rao (raodm@miamioh.edu) CSE Department, Oxford, OHIO
Motivation • Students consistently report steep, multifaceted learning curve and challenges due to: • Introduction of new programming language • Prerequisite courses are predominantly taught in Java • Students struggle with C-language • Initiation to Linux • Course introduces Linux and predominantly uses a Command Line Interface (CLI) based laboratory environment • A Significant change from a Graphical User Interface (GUI) driven Windows™ platform • Introduction and extensive use of PDC concepts • A paradigmatic change from the traditional serial programming that the students are conversant with.
Proposed using C++ rather than C • In Fall 2012, C++11 was used instead of C to ameliorate the steep learning curves faced by students • Continue to build upon student’s object-oriented skills • Use STL data structures to ease programming • Encouraged use of standard algorithms • Supports automatic multi-threading via compiler flags • Permit the use of C++ lambdas (makes programming convenient and concise) • Enables seamless use of system calls exposed only in C language • C++11 provides many modern design patterns for managing concurrency and parallelism • Threading constructs are very streamlined and straightforward • Simplified, stream-based I/O • For console I/O, files, sockets, and IPC-pipes
Sample C++ Code Fragments void producer(intnum) { for(int i = 0; (i < num); i++){ std::unique_lock<std::mutex> lock(queueMutex); data_cond.wait(lock, []{returnqueue.size() < MaxQSize; }); queue.push(rand() % 10000); data_cond.notify_one(); } } usingnamespacestd; intmain() { promise<int> prom; async(launch::async, thread1, 99999, std::ref(prom)); future<int> result = std::async(launch::async, thread2, 50000, std::ref(prom)); // Do some foreground work here! cout<< "Result = ” << result.get() << std::endl; return0; } intnum = 0; // A mutex to synchronize // access to num std::mutex gate; voidthreadMain() { // Automatic lock & unlock std::lock_guard<std::mutex> guard(gate); for(inti=0; (i<1000); i++) { num++; } }
Conclusions • Pedagogical experiences indicate C++11 was effective in teaching concurrency and parallelism • It alleviated the steep learning curve experienced by students • Permitted coverage of pointers and explicit dynamic memory management later in the course when students were already comfortable with C++. • Helped to focus on core concepts rather than routine problem solving aspects • Effective for covering modern design patterns related to parallelism and concurrency • Many of the terms and concepts are portable to other popular programming languages • Eases use of OS system calls and other C-language API • Student experience and feedback was very positive • Course evaluations were very positive: 3.38 (SD: 0.84) out of 4.0 • Department plans to use C++11 in Fall 2013 • If experiences and student feedback remain positive, then C++11 will be permanently adopted