Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today Quiz not yet graded (Sorry!) More on Multithreading Happens-Before in Java Other langauges with Happens-Before Happens-Before in C++ SE-2811 Slide.

Similar presentations


Presentation on theme: "Today Quiz not yet graded (Sorry!) More on Multithreading Happens-Before in Java Other langauges with Happens-Before Happens-Before in C++ SE-2811 Slide."— Presentation transcript:

1 Today Quiz not yet graded (Sorry!) More on Multithreading Happens-Before in Java Other langauges with Happens-Before Happens-Before in C++ SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1 SE3910 Week 9, Class 2

2 Volatile http://www.brandsoasis.com/bo3/en/volatile http://link- designs.deviantart.com/art/Warning- Extremely-Volatile-403315705 http://link- designs.deviantart.com/art/Warning- Extremely-Volatile-403315705 http://www.emcdda.europa.eu/publications/dr ug-profiles/volatile http://www.emcdda.europa.eu/publications/dr ug-profiles/volatile http://www.stormthetest.com/word-of-the-day- volatile/ http://www.stormthetest.com/word-of-the-day- volatile/ http://world-worst- disasters.blogspot.com/2010/11/volcano- death-toll-rises-past-320.html http://world-worst- disasters.blogspot.com/2010/11/volcano- death-toll-rises-past-320.html https://eagleman6788.wordpress.com/tag/exp losive-anger/ https://eagleman6788.wordpress.com/tag/exp losive-anger/ SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 2

3 Volatile readily vaporizable at a relatively low temperature flying or having the power to fly lighthearted, lively tending to erupt into violence : explosive unable to hold the attention fixed because of an inherent lightness or fickleness of disposition difficult to capture or hold permanently http://www.stormthetest.com/word-of-the-day- volatile/ SE-2811 Dr.Yoder 3

4 Based on these definitions, which do you think is a closer fit for volatile? A variable which is cached – at any time, value could be incorrect because other threads change it. A variable which is not cached. Must always go to main memory to read value, because other threads may change it at any time. Note: I am NOT asking which is volatile in Java or other languages. SE-2811 Dr.Yoder 4

5 Volatile (in Java) A variable which is not cached. Must always go to main memory to read or write the value SE-2811 Dr.Yoder 5

6 Sequential Consistency A program is sequentially consistent if every read is immediately visible on every other thread, in the program order. The “hop-back-and-forth between threads” model If language were sequentially consistent by default, could not reorder instructions or use cache (Research area: Or would it?) https://docs.oracle.com/javase/specs/jls/se8/ht ml/jls-17.html#jls-17.4 SE-2811 Dr.Yoder 6

7 Some definitions “Two accesses to (reads of or writes to) the same variable are said to be conflicting if at least one of the accesses is a write.” (§17.4.1)§17.4.1 “When a program contains two conflicting accesses that are not ordered by a happens- before relationship, it is said to contain a data race.” (§17.4.5)§17.4.5 SE-2811 Dr.Yoder 7

8 Data-race free programs (updated) If a program has no data races, then we can treat that program as if it behaved according to our simple “switch-back-and-forth- between-threads” model – it is sequentially consistent This is nice, but there can still be problems E.g. single-locked singleton It doesn’t guarantee that operations will happen or fail as a unit - atomically SE-2811 Dr.Yoder 8

9 A set of operations that happen all at once; they cannot be interrupted if(theInstance == null) { theInstance = new MySingleton(); } Atomic (review?) SE-2811 Dr.Yoder 9 Example: Should be atomic

10 Volatile caveat (review) Although writes to references are protected by volatile, writes to objects are not. If you want a volatile object, you have to declare all its fields volatile (not recommended) Even then… You can’t do this for objects in the standard library Your class may still have parts that need to be atomic but are not If you are calling a method that is not designed to be used without synchronization, you should synchronize around it. SE-2811 Dr.Yoder 10

11 Ex. Happens-before explanation if(loggers.get(path)==null){ synchronized (loggers){ if(loggers.get(path) == null){ Logger logger = new EventLogger(path); loggers.put(path, logger); } SE-2811 Dr.Yoder 11 Write whether this code contains any data races. Explain your answer. Assume loggers is thread safe.

12 An alternative: java.util.concurrent (review) Lock-free multi-threaded data structures: ConcurrentHashMap Like HashMap, only “concurrent” ConcurrentSkipListMap Like TreeMap, only “concurrent” Discussed in Dean & Dean SE1011 book recommended by a student for studying data structures SE-2811 Dr. Yoder 12

13 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) LLVM intermediate representation http://preshing.com/20130702/the-happens- before-relation/ SE-2811 Dr.Yoder 13

14 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) http://preshing.com/20130702/the-happens- before-relation/ http://preshing.com/20130702/the-happens- before-relation/ http://en.cppreference.com/w/cpp/atomic/me mory_order http://en.cppreference.com/w/cpp/atomic/me mory_order http://en.cppreference.com/w/cpp/language/e val_order http://en.cppreference.com/w/cpp/language/e val_order SE-2811 Dr.Yoder 14

15 Example #include std::atomic 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))) ; 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 15 http://en.cppreference.com/w/cpp/atomic/memory_order

16 Muddiest Point Wait for the slides, or follow this link to answer both questions at once: http://bit.ly/1Mow5a3 SE-2811 Dr.Yoder 16

17 SE-2811 Dr. Josiah Yoder 17 http://bit.ly/1Mow5a3

18 SE-2811 Dr. Josiah Yoder 18 http://bit.ly/1Mow5a3

19 References EBB: Derek Malloy, Exploring Beaglebone, Wiley, 2015 RTS: Laplante and Ovaska, Real-Time Systems Design and Analysis by, Fourth Edition, Wiley, 2012 SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 19

20 Someday soon Late next week? Real-Time Operating Systems What is a RTOS? How does it relate to the rest? OS roles Interrupts and the OS Definitions, Flowchart, Timing diagram Detailed steps Scheduling Task states & pre-runtime vs runtime SE-2811 Dr.Yoder 20

21 Task States Wiki:Process (computing) See also Laplante and Ovaske 4E p. 97 SE-2811 Dr.Yoder 21

22 Some happens-before relationships Every action in a thread Exit synch. section Write to volatile field Any action http://docs.oracle.com/j avase/8/docs/api/java/ut il/concurrent/package- summary.html#Memory Visibility actions later in program order in same thread Enter synch. section locked on same object (later in time) Read of volatile field (later in time) Any action which happens before an action which … happens before this action (chaining) SE-2811 Dr. Yoder 22


Download ppt "Today Quiz not yet graded (Sorry!) More on Multithreading Happens-Before in Java Other langauges with Happens-Before Happens-Before in C++ SE-2811 Slide."

Similar presentations


Ads by Google