Download presentation
Presentation is loading. Please wait.
1
Slide design: Dr. Mark L. Hornick
SE3910 4/24/2019 SE3910 Week 9, Class 3 Today Multithreading in Java and C++11 Monday: Class time -> Office Hours / Demo time (Perhaps in S-365) Full agenda: Return Exam Questions about lab due tomorrow in class? Threads: Locking on null object Threads: invokeLater Threads: The squares example Decorator Class Diagram More on Java IO Class diagrams Design Principles in the patterns we’ve seen so far Compare with alternatives Decorator vs. array approach suggested in class Non-decorator array – decorator can be added on without modifying the original hierarchy Decorator has “before-after” and possibly other combinatorial control that would be hard-coded in array [Show “screenshot” of discussion from class? Or just re-type?] Strategy vs. Decorator class diagrams side-by-side Structural difference (inheritance optional in Strategy pattern?) Decorator vs. “Strategy” array Perhaps next: Coding Starbuzz coffee (?) Add real patterns ArrayList – null-checking Java I/O: Students do coding examples SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder Dr. Josiah Yoder
2
SE3910 4/24/2019 Lab Announcements To link to OpenCV from Qt, please add these lines at the end of your .pro file: unix: CONFIG += link_pkgconfig unix: PKGCONFIG += opencv If you are getting many underflows, you may want to check that the CPU is set to its maximum frequency. On the BB, run the commands cpufreq-info cpufreq-set -f 1000MHz to get the frequency set correctly. (Thanks, Dr. Schilling.) SE-2811 Dr.Yoder Dr. Josiah Yoder
3
SE3910 4/24/2019 Lab Announcements When checking out a kit for an LCD, check out two kits. If you have any trouble getting it to boot to a Desktop, shutdown and use the other LCD. Or come to my office and borrow mine. When debugging on the desktop, you may want to use a script to automatically capture the output of your commands. e.g., containing: ./mylab8 arguments > output.txt 2> errror.txt Dr. Josiah Yoder
4
Total and Partial Order
SE2811 4/24/2019 Total and Partial Order Each thread has a Total Order Without synchronize or volatile, actions are concurrent ? In a partial order, some “happens-before” relationships are specified, but not all. In a total order, all “happens-before” relationships are specified. (So there is only one way through the program) For any given multithreaded program, different total orders are possible. But from a synchronization perspective, each thread has only one intra-thread total order. This is the “Program order” of that thread. SE-2811 Dr.Yoder Dr. Josiah Yoder
5
Total and Partial Order
SE2811 4/24/2019 Total and Partial Order Partial Order One Total Order In a partial order, some “happens-before” relationships are specified, but not all. In a total order, all “happens-before” relationships are specified. (So there is only one way through the program) SE-2811 Dr.Yoder Dr. Josiah Yoder
6
Total and Partial Order
SE2811 4/24/2019 Total and Partial Order Partial Order One Total Order In a partial order, some “happens-before” relationships are specified, but not all. In a total order, all “happens-before” relationships are specified. (So there is only one way through the program) SE-2811 Dr.Yoder Dr. Josiah Yoder
7
Total and Partial Order
SE2811 4/24/2019 Total and Partial Order Partial Order Another Total Order In a partial order, some “happens-before” relationships are specified, but not all. In a total order, all “happens-before” relationships are specified. (So there is only one way through the program) For any given multithreaded program, different total orders are possible when the program runs. These are called the “Execution order” But from a synchronization perspective, each thread has only one intra-thread total order. SE-2811 Dr.Yoder Dr. Josiah Yoder
8
Total and Partial Order
SE2811 4/24/2019 Total and Partial Order Partial Order Another Total Order In a partial order, some “happens-before” relationships are specified, but not all. In a total order, all “happens-before” relationships are specified. (So there is only one way through the program) For any given multithreaded program, different total orders are possible when the program runs. These are called the “Execution order” But from a synchronization perspective, each thread has only one intra-thread total order. SE-2811 Dr.Yoder Dr. Josiah Yoder
9
Languages using Happens-Before
SE3910 4/24/2019 Languages using Happens-Before Java C++11 Golang LLVM (Technically not a language, but a compiler infrastructure, but it does include an intermediate representation language) SE-2811 Dr.Yoder Dr. Josiah Yoder
10
Happens-Before in C++11 Some are transitive
SE3910 4/24/2019 Happens-Before in C++11 Some are transitive “If we ignore consume operations and the dependency-ordered-before relation in C++11, the remaining forms of happens-before end up being transitive.” (Preshing) Specifications are notoriously difficult to understand, even the most precise ones. This question clarifies exactly where the C++11 defines what happens-before means. The asker is confused because they say A is true if B is true instead of A is true if and only if B is true. Important to our current discussion: I don’t have access to the C++11 specification. It is not an open standard. You have to purchase a copy, which I have not done. (or at least it doesn’t pop up on Google) SE-2811 Dr.Yoder Dr. Josiah Yoder
11
Example http://en.cppreference.com/w/cpp/atomic/memory_order
SE3910 4/24/2019 Example #include <thread> #include <atomic> #include <cassert> #include <string> std::atomic<std::string*> ptr; int data; void producer() { std::string* p = new std::string("Hello"); data = 42; ptr.store(p, std::memory_order_release); } void consumer() { std::string* p2; while (!(p2 = ptr.load(std::memory_order_acquire))) /* do nothing */; assert(*p2 == "Hello"); // never fires assert(data == 42); // never fires } int main() std::thread t1(producer); std::thread t2(consumer); t1.join(); t2.join(); SE-2811 Dr.Yoder Dr. Josiah Yoder
12
Consider this code: synchronized(this) { im = getImage(); q.put(im); }
SE3910 4/24/2019 Consider this code: synchronized(this) { im = getImage(); q.put(im); } synchronized(this) { im = q.get(im); display(im); } SE-2811 Dr.Yoder Dr. Josiah Yoder
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.