Today Return Quiz First release of final project template Multithreading Options Qt socket buffers...

23
Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication in Qt SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1 SE3910 Week 8, Class 2

description

I was asked this question in an interview today…. (continued) "When we create a thread with pthread_create() (POSIX Threads), the thread starts on its own. Why do we need to explicitly call start() in Java. What is the reason that Java doesnt start the thread when we create an instance of it." I was blank and interviewer was short of time and eventually he couldnt explain the reason to me. a-threads-vs-pthreads SE Dr. Josiah Yoder Slide style: Dr. Hornick Much Material: Dr. Schilling 3

Transcript of Today Return Quiz First release of final project template Multithreading Options Qt socket buffers...

Page 1: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

Today Return Quiz First release of final project template Multithreading Options

Qt socket buffers between threads Tomorrow: Quiz

Interthread communication in Qt

SE-2811Slide design: Dr. Mark L. Hornick

Content: Dr. HornickErrors: Dr. Yoder

1

SE3910Week 8, Class 2

Page 2: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

Options for multithreading (review) pthreads

POSIX (Linux/Unix standard) threads Can use in C/C++

qthreads Qt Threads Object-Oriented Uses pthreads under the hood, when on POSIX [1]

Derek Malloy’s GPIO class Uses pthreads under the hood, but is single-

threaded Boost (Please discuss this with me BEFORE

HAND if you want to use it.)[1] http://stackoverflow.com/questions/4140189/qthreads-vs-pthreads

2

Page 3: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

I was asked this question in an interview today…. (continued)

"When we create a thread with pthread_create() (POSIX Threads), the thread starts on its own. Why do we need to explicitly call start() in Java. What is the reason that Java doesnt start the thread when we create an instance of it."I was blank and interviewer was short of time and eventually he couldnt explain the reason to me.http://stackoverflow.com/questions/5269535/java-threads-vs-pthreads

SE-3910 - Dr. Josiah YoderSlide style: Dr. Hornick

Much Material: Dr. Schilling3

Page 4: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

Task States (more essential rambling)

Wiki:Process (computing)See also Laplante and Ovaske 4E p. 97SE-2811

Dr.Yoder 4

Page 5: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

Threading – pthreads (review) Java Pthreadsjava.lang.Thread #include <pthread.h>No external jar needed link with -pthreadThread t = new Thread(r)t.start();

pthread_create(t,r,sr,a)

interface Runnable {void run(); }

Parameter:void* (*sr) (void *)

t.join(); pthread_join(*t, &p)Object o; pthread_mutex_init(m,null)synchronized(o) {…} … /* Garbage coll. */

pthread_mutex_lock(…)…pthread_mutex_destroy(…)

5

Page 6: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

Threading – pthreads (updated with links)Java PthreadsObject o;o.notify();

phread_cond_t c = PTHREAD_COND_INITIALIZER;pthread_cond_broadcast(c);

o.wait(); pthread_cond_wait(c,m);o.notify(); phtread_cond_signal(c);o.notifyAll(); phtread_cond_broadcast(c);

SE-3910 - Dr. Josiah YoderSlide style: Dr. Hornick

Much Material: Dr. Schilling6

• 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

Page 7: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

Ex: Edit to include notify/wait to interleave these threads.Consider this code-snippetQueue q = ……synchronized(q) { while(true) { img = getImage(); q.offer(img); }}

// on another thread

synchronized(q) { while(true) { img = q.poll(); showImage(); }}

SE-2811Dr.Yoder 7

Page 8: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

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();

QThread *t = new QThread;moveToThread(t); // note here

interface Runnable {void run(); }

QObject (e.g. QWidget (e.g. QMainWindow))

t.join(); connect the QThread::finish() signal to a slot that checks if all threads are done.

Object o; QMutexsynchronized(o) {…} … /* Garbage coll. */

Avoid sharing memory entirely…… see code example…???? 8

Page 9: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

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

Page 11: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

Qt Connection types

Constant Value Description

Qt::AutoConnection 0(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::DirectConnection 1 The slot is invoked immediately when the signal is emitted. The

slot is executed in the signalling thread.

Qt::QueuedConnection 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.

Qt::BlockingQueuedConnection 3

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.

Qt::UniqueConnection 0x80

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

http://doc.qt.io/qt-5/qt.html#ConnectionType-enum

Page 12: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

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

Page 13: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

A bit more on the Rate Monotonic Analysis

SE-2811Dr.Yoder 13

Page 14: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

Possibly Fun Very simple C++ wrapper for pthreads

http://www.cs.utexas.edu/users/lavender/courses/cs345/lectures/CS345-Lecture-09.pdf

notify/wait – example comparison with Java and pthreads http://wimhesselink.nl/pub/whh241b.pdf

Compares create for Java, pthreads, and Win32 threads http://cs.millersville.edu/~csweb/lib/userfiles/9Thre

adsII.pdfSE-3910 - Dr. Josiah Yoder

Slide style: Dr. HornickMuch Material: Dr. Schilling

14

Page 15: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

Certainly fun (Dr. Taylor’s Reviews) http://msoe.us/taylor/tutorial/ce2810/functionp

ointers Function Pointers

http://msoe.us/taylor/tutorial/ce2810/csimilar C/C++/Java

http://msoe.us/taylor/tutorial/ce2810/ooc Object-Oriented C

SE-3910 - Dr. Josiah YoderSlide style: Dr. Hornick

Much Material: Dr. Schilling15

Page 16: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

Other references http://granite.sru.edu/~whit/cpsc464/Notes/fig

s/02-14.jpg Simple pthread chart

From http://granite.sru.edu/~whit/cpsc464/Notes/ch2.html

https://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Fapis%2Fusers_75.htm IBM example of using pthread_cond_init

SE-3910 - Dr. Josiah YoderSlide style: Dr. Hornick

Much Material: Dr. Schilling16

Page 17: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

Standards http://pubs.opengroup.org/onlinepubs/790879

9/xsh/pthread.h.html pthead.h

http://pubs.opengroup.org/onlinepubs/7908799/xsh/systypes.h.html systypes.h – for xxxx_t

SE-3910 - Dr. Josiah YoderSlide style: Dr. Hornick

Much Material: Dr. Schilling17

Page 18: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

Muddiest Point

Wait for the slides, or follow this link to answer both questions at once: http://bit.ly/1Mow5a3

SE-2811Dr.Yoder 18

Page 19: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

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

Page 20: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

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

Page 21: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

References

EBB: Derek Malloy, Exploring Beaglebone, Wiley, 2015RTS: Laplante and Ovaska, Real-Time Systems Design and Analysis by, Fourth Edition, Wiley, 2012

SE-2811Slide design: Dr. Mark L. Hornick

Content: Dr. HornickErrors: Dr. Yoder

21

Page 22: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

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

Page 23: Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.

Task States

Wiki:Process (computing)See also Laplante and Ovaske 4E p. 97SE-2811

Dr.Yoder 23