The Concept of Volatile Variables in Multithreading

 
S
E
3
9
1
0
W
e
e
k
 
6
,
 
C
l
a
s
s
 
1
 
Week 6, Class 2 (Wednesday)
Quiz – to be graded still
Week 7, Class 2 (Wednesday)
Half-Exam 2
B
r
i
n
g
 
C
a
l
c
u
l
a
t
o
r
!
!
Qt and Java: Multithreading
 
SE-2811
Slide design: Dr. Mark L. Hornick
Content: Dr. Hornick
Errors: Dr. Yoder
 
1
 
M
u
l
t
i
-
c
o
r
e
 
p
r
o
g
r
a
m
m
i
n
g
 
 
SE-2811
Dr.Yoder
 
2
 
V
o
l
a
t
i
l
e
 
http://www.brandsoasis.com/bo3/en/volatile
http://link-
designs.deviantart.com/art/Warning-
Extremely-Volatile-403315705
http://www.emcdda.europa.eu/publications/dr
ug-profiles/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
https://eagleman6788.wordpress.com/tag/exp
losive-anger/
 
 
SE-2811
Slide design: Dr. Mark L. Hornick
Content: Dr. Hornick
Errors: Dr. Yoder
 
3
 
 
 
 
V
o
l
a
t
i
l
e
 
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
 
4
 
B
a
s
e
d
 
o
n
 
t
h
e
s
e
 
d
e
f
i
n
i
t
i
o
n
s
,
w
h
i
c
h
 
d
o
 
y
o
u
 
t
h
i
n
k
 
i
s
 
a
 
c
l
o
s
e
r
f
i
t
 
f
o
r
 
v
o
l
a
t
i
l
e
?
 
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
 
5
 
V
o
l
a
t
i
l
e
 
(
i
n
 
J
a
v
a
)
 
A variable which is not cached. Must always
go to main memory to read or write the value
 
Why might these variables be called volatile?
 
 
Why might we want a volatile variable?
 
SE-2811
Dr.Yoder
 
6
 
V
o
l
a
t
i
l
e
 
(
i
n
 
C
)
 
https://en.wikipedia.org/wiki/Volatile_(comput
er_programming)#Optimization_comparison_
in_C
 
SE-2811
Dr.Yoder
 
7
 
T
a
s
k
 
S
t
a
t
e
s
 
(
m
o
r
e
 
e
s
s
e
n
t
i
a
l
r
a
m
b
l
i
n
g
)
 
 
 
 
 
 
 
 
Wiki:
Process (computing)
See also Laplante and Ovaske 4E p. 97
 
SE-2811
Dr.Yoder
 
8
 
T
h
r
e
a
d
i
n
g
 
 
p
t
h
r
e
a
d
s
 
(
r
e
v
i
e
w
)
 
9
 
T
h
r
e
a
d
i
n
g
 
 
p
t
h
r
e
a
d
s
 
(
u
p
d
a
t
e
d
w
i
t
h
 
l
i
n
k
s
)
 
SE-3910  - Dr. Josiah Yoder
Slide style: Dr. Hornick
Much Material: Dr. Schilling
 
10
 
S
e
e
 
J
a
v
a
 
c
o
d
i
n
g
 
e
x
a
m
p
l
e
 
N
o
t
i
f
y
W
a
i
t
E
x
a
m
p
l
e
Caveat: “POSIX threads can wait at condition variables
of a greater generality than available in Java, but the
corresponding queues may be leaky.”
http://wimhesselink.nl/pub/whh241b.pdf
 
E
x
:
 
E
d
i
t
 
t
o
 
i
n
c
l
u
d
e
 
n
o
t
i
f
y
/
w
a
i
t
t
o
 
i
n
t
e
r
l
e
a
v
e
 
t
h
e
s
e
 
t
h
r
e
a
d
s
.
 
Consider this code-
snippet
Queue q = …
synchronized(q) {
   while(true) {
     img = getImage();
     q.offer(img);
   }
}
 
// on another thread
 
synchronized(q) {
   while(true) {
      img = q.poll();
      showImage();
   }
}
 
SE-2811
Dr.Yoder
 
11
 
T
h
r
e
a
d
i
n
g
 
 
q
t
h
r
e
a
d
s
 
(
w
i
t
h
c
o
r
r
e
c
t
i
o
n
s
)
 
12
 
U
s
e
f
u
l
 
i
f
 
y
o
u
 
a
r
e
 
i
n
t
o
 
Q
t
s
l
o
t
s
/
s
i
g
n
a
l
s
 
=
=
 
e
v
e
n
t
s
 
“A QThread should be used much like a regular
thread instance: prepare an object (QObject) class
with all your desired functionality in it. Then create
a new QThread instance, push the QObject onto it
using moveToThread(QThread*) of the QObject
instance and call start() on the QThread instance.
That’s all.
https://mayaposch.wordpress.com/2011/11/01/how
-to-really-truly-use-qthreads-the-full-explanation/
I
 
h
a
v
e
 
s
u
c
c
e
s
s
f
u
l
l
y
 
u
s
e
d
 
t
h
i
s
 
a
p
p
r
o
a
c
h
.
 
SE-2811
Dr.Yoder
 
13
 
Q
t
s
 
c
o
n
n
e
c
t
 
m
e
t
h
o
d
 
http://doc.qt.io/qt-5/qobject.html#connect
http://doc.qt.io/qt-5/qt.html#ConnectionType-
enum
 
SE-2811
Dr.Yoder
 
14
 
Q
t
 
C
o
n
n
e
c
t
i
o
n
 
t
y
p
e
s
 
SE-2811
Dr.Yoder
 
15
 
http://doc.qt.io/qt-5/qt.html#ConnectionType-enum
 
C
o
d
i
n
g
 
e
x
a
m
p
l
e
.
E
x
e
r
c
i
s
e
:
 
Both the Java and Qt solutions will behave
poorly if I start multiple threads.
P
r
e
d
i
c
t
 
h
o
w
 
e
a
c
h
 
s
o
l
u
t
i
o
n
 
w
i
l
l
 
b
e
h
a
v
e
 
i
f
 
I
:
Click start
Click start
// what happens? (For Java? For Qt?)
Click stop
// what happens? (For Java? For Qt?)
 
SE-2811
Dr.Yoder
 
16
 
R
e
f
e
r
e
n
c
e
s
 
EB: Derek Malloy, 
Exploring Beaglebone,
Wiley, 2015
 
SE-2811
Slide design: Dr. Mark L. Hornick
Content: Dr. Hornick
Errors: Dr. Yoder
 
17
Slide Note

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

http://stackoverflow.com/questions/9358821/should-i-extend-arraylist-to-add-attributes-that-isnt-null

SE3910

Dr. Josiah Yoder

Embed
Share

In computer programming, volatile variables play a crucial role in multithreading scenarios. They ensure that changes made by one thread are visible to other threads by bypassing caching mechanisms. This article explores the significance of volatile variables and their use cases in Java and C, shedding light on thread synchronization and memory consistency issues.

  • Multithreading
  • Volatile Variables
  • Java
  • C
  • Synchronization

Uploaded on Sep 22, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. SE3910 Week 6, Class 1 Week 6, Class 2 (Wednesday) Quiz to be graded still Week 7, Class 2 (Wednesday) Half-Exam 2 Bring Calculator!! Qt and Java: Multithreading SE-2811 1 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder

  2. Multi-core programming SE-2811 Dr.Yoder 2

  3. Volatile SE-2811 3 http://www.brandsoasis.com/bo3/en/volatile Content: Dr. Hornick Errors: Dr. Yoder Slide design: Dr. Mark L. Hornick

  4. 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- SE-2811 Dr.Yoder 4

  5. 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 5

  6. Volatile (in Java) A variable which is not cached. Must always go to main memory to read or write the value Why might these variables be called volatile? Why might we want a volatile variable? SE-2811 Dr.Yoder 6

  7. Volatile (in C) https://en.wikipedia.org/wiki/Volatile_(comput er_programming)#Optimization_comparison_ in_C SE-2811 Dr.Yoder 7

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

  9. Threading pthreads (review) Java java.lang.Thread No external jar needed link with -pthread Thread t = new Thread(r) t.start(); interface Runnable { void run(); } t.join(); Object o; synchronized(o) { } /* Garbage coll. */ Pthreads #include <pthread.h> pthread_create(t,r,sr,a) Parameter: void* (*sr) (void *) pthread_join(*t, &p) pthread_mutex_init(m,null) pthread_mutex_lock( ) pthread_mutex_destroy( ) 9

  10. Threading pthreads (updated with links) Java Object o; o.notify(); Pthreads phread_cond_t c = PTHREAD_COND_INITIALIZER; pthread_cond_broadcast(c); pthread_cond_wait(c,m); phtread_cond_signal(c); phtread_cond_broadcast(c); o.wait(); o.notify(); o.notifyAll(); See Java coding example NotifyWaitExample Caveat: POSIX threads can wait at condition variables of a greater generality than available in Java, but the corresponding queues may be leaky. http://wimhesselink.nl/pub/whh241b.pdf SE-3910 - Dr. Josiah Yoder Slide style: Dr. Hornick Much Material: Dr. Schilling 10

  11. Ex: Edit to include notify/wait to interleave these threads. Consider this code- snippet Queue q = synchronized(q) { while(true) { img = getImage(); q.offer(img); } } // on another thread synchronized(q) { while(true) { img = q.poll(); showImage(); } } SE-2811 Dr.Yoder 11

  12. Threading qthreads (with corrections) Java qthreads java.lang.Thread #include <QThread> No external jar needed (moc and friends take care of this) Thread t = new Thread(r) t.start(); interface Runnable { void run(); } QMainWindow)) t.join(); that checks if all threads are done. Object o; QMutex synchronized(o) { } /* Garbage coll. */ ???? QThread *t = new QThread; moveToThread(t); // note here QObject (e.g. QWidget (e.g. connect the QThread::finish() signal to a slot Avoid sharing memory entirely see code example 12

  13. Useful if you are into Qt slots/signals == events A QThread should be used much like a regular thread instance: prepare an object (QObject) class with all your desired functionality in it. Then create a new QThread instance, push the QObject onto it using moveToThread(QThread*) of the QObject instance and call start() on the QThread instance. That s all. https://mayaposch.wordpress.com/2011/11/01/how -to-really-truly-use-qthreads-the-full-explanation/ I have successfully used this approach. SE-2811 Dr.Yoder 13

  14. Qts connect method http://doc.qt.io/qt-5/qobject.html#connect http://doc.qt.io/qt-5/qt.html#ConnectionType- enum SE-2811 Dr.Yoder 14

  15. Qt Connection types http://doc.qt.io/qt-5/qt.html#ConnectionType-enum Constant Value Description (Default) If the receiver lives inthe thread that emits the signal, Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used. The connection type is determined when the signal is emitted. Qt::AutoConnection 0 Qt::Direct Connection Qt::Queued Connection Qt::Blocking QueuedConnection3 1 The slot is invoked immediately when the signal is emitted. The slot is executed in the signalling thread. 2 The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed inhe t receiver's thread. Same as Qt::QueuedConnection, except that the signalling thread blocks until the slot returns. This connection must not be used if the receiver lives in the signalling thread, or else the application will deadlock. This is a flag that can be combined with any one of the above connection types, using a bitwise OR. When Qt::UniqueConnection is set,QObject::connect() will fail if the connection already exists (i.e. if the same signal is already connected to the same slot for the same pair of objects). This flag was introduced in Qt 4.6. SE-2811 Dr.Yoder Qt::Unique Connection 0x80 15

  16. Coding example. Exercise: Both the Java and Qt solutions will behave poorly if I start multiple threads. Predict how each solution will behave if I: Click start Click start // what happens? (For Java? For Qt?) Click stop // what happens? (For Java? For Qt?) SE-2811 Dr.Yoder 16

  17. References EB: Derek Malloy, Exploring Beaglebone, Wiley, 2015 SE-2811 17 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#