Confidential © 2008 Teleca AB MultiThreads of Qt Teleca Chengdu 26 July 2010 Author: Tom Chen.

Slides:



Advertisements
Similar presentations

Advertisements

Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Chapter 6: Process Synchronization
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
CS427 Multicore Architecture and Parallel Computing
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Chapter 6 – Concurrent Programming Outline 6.1 Introduction 6.2Monitors 6.2.1Condition Variables 6.2.2Simple Resource Allocation with Monitors 6.2.3Monitor.
4.7.1 Thread Signal Delivery Two types of signals –Synchronous: Occur as a direct result of program execution Should be delivered to currently executing.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
PRASHANTHI NARAYAN NETTEM.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
10/04/2011CS4961 CS4961 Parallel Programming Lecture 12: Advanced Synchronization (Pthreads) Mary Hall October 4, 2011.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
The University of Adelaide, School of Computer Science
111 © 2002, Cisco Systems, Inc. All rights reserved.
1 Confidential Enterprise Solutions Group Process and Threads.
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Synchronizing Threads with Semaphores
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Threads. Readings r Silberschatz et al : Chapter 4.
Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Barriers and Condition Variables
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
SMP Basics KeyStone Training Multicore Applications Literature Number: SPRPxxx 1.
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
Chapter 4: Threads 羅習五. Chapter 4: Threads Motivation and Overview Multithreading Models Threading Issues Examples – Pthreads – Windows XP Threads – Linux.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
pThread synchronization
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads.
Threads Threads.
Multithreading Tutorial
Thread synchronization
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Finalization 17: Finalization
Multithreading Tutorial
Multithreading.
Shared Memory Programming
Chapter 7: Synchronization Examples
Multithreading Tutorial
Multithreading Tutorial
CSE 153 Design of Operating Systems Winter 19
CSE 451 Section 1/27/2000.
CS 144 Advanced C++ Programming May 7 Class Meeting
“The Little Book on Semaphores” Allen B. Downey
Presentation transcript:

Confidential © 2008 Teleca AB MultiThreads of Qt Teleca Chengdu 26 July 2010 Author: Tom Chen

Confidential © 2008 Teleca AB 2 Overview All of Threads classes Example Recommended Reading Agenda

Confidential © 2008 Teleca AB 3 Overview Qt provides thread support in the form of platform-independent threading classes, a thread-safe way of posting events, and signal- slot connections across threads. This makes it easy to develop portable multithreaded Qt applications and take advantage of multiprocessor machines. Multithreaded programming is also a useful paradigm for performing time-consuming operations without freezing the user interface of an application. Earlier versions of Qt offered an option to build the library without thread support. Since Qt 4.0, threads are always enabled

Confidential © 2008 Teleca AB 4 All of Threading classes QThread provides the means to start a new thread. QThreadStorage provides per-thread data storage. QMutex provides a mutual exclusion lock, or mutex. QMutexLocker is a convenience class that automatically locks and unlocks a QMutex. QReadWriteLock provides a lock that allows simultaneous read access. QReadLocker and QWriteLocker are convenience classes that automatically lock and unlock a QReadWriteLock. QSemaphore provides an integer semaphore (a generalization of a mutex). QWaitCondition provides a way for threads to go to sleep until woken up by another thread.

Confidential © 2008 Teleca AB 5 QThread The QThread class provides platform-independent threads. A QThread represents a separate thread of control within the program; it shares data with all the other threads within the process but executes independently in the way that a separate program does on a multitasking operating system. Instead of starting in main(), QThreads begin executing in run(). To create your own threads, subclass QThread and reimplement run().run class MyThread : public QThread { public: void run(); }; void MyThread::run() { QTcpSocket socket; // connect QTcpSocket's signals somewhere meaningful... socket.connectToHost(hostName, portNumber); exec(); }

Confidential © 2008 Teleca AB 6 QThreadStorage The QThreadStorage class provides per-thread data storage. QThreadStorage is a template class that provides per-thread data storage. Note that due to compiler limitations, QThreadStorage can only store pointers. QThreadStorage *> caches; void cacheObject(const QString &key, SomeClass *object) { if (!caches.hasLocalData()) caches.setLocalData(new QCache ); caches.localData()->insert(key, object); } void removeFromCache(const QString &key) { if (!caches.hasLocalData()) return; caches.localData()->remove(key); }

Confidential © 2008 Teleca AB 7 QMutex The QMutex class provides access serialization between threads. The purpose of a QMutex is to protect an object, data structure or section of code so that only one thread can access it at a time (this is similar to the Java synchronized keyword). It is usually best to use a mutex with a QMutexLocker since this makes it easy to ensure that locking and unlocking are performed consistentlyQMutexLocker

Confidential © 2008 Teleca AB 8 QMutexLocker The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes. The purpose of QMutexLocker is to simplify QMutex locking and unlocking. Locking and unlocking a QMutex in complex functions and statements or in exception handling code is error-prone and difficult to debug.QMutex QMutexLocker can be used in such situations to ensure that the state of the mutex is always well-defined. QMutexLocker should be created within a function where a QMutex needs to be locked. The mutex is locked when QMutexLocker is created, and unlocked when QMutexLocker is destroyed.QMutex

Confidential © 2008 Teleca AB 9 QReadWriteLock The QReadWriteLock class provides read-write locking. A read-write lock is a synchronization tool for protecting resources that can be accessed for reading and writing. This type of lock is useful if you want to allow multiple threads to have simultaneous read-only access, but as soon as one thread wants to write to the resource, all other threads must be blocked until the writing is complete. In many cases, QReadWriteLock is a direct competitor to QMutex. QReadWriteLock is a good choice if there are many concurrent reads and writing occurs infrequentlyQMutex QReadWriteLock lock; void ReaderThread::run() {... lock.lockForRead(); read_file(); lock.unlock();... } void WriterThread::run() {... lock.lockForWrite(); write_file(); lock.unlock();... }

Confidential © 2008 Teleca AB 10 QSemaphore The QSemaphore class provides a general counting semaphore. A semaphore is a generalization of a mutex. While a mutex can only be locked once, it's possible to acquire a semaphore multiple times. Semaphores are typically used to protect a certain number of identical resources. Semaphores support two fundamental operations, acquire() and release():acquirerelease acquire(n) tries to acquire n resources. If there aren't that many resources available, the call will block until this is the case. release(n) releases n resources. There's also a tryAcquire() function that returns immediately if it cannot acquire the resources, and an available() function that returns the number of available resources at any time.tryAcquireavailable

Confidential © 2008 Teleca AB 11 Example Mandelbrot Example Semaphores Example Wait Conditions Example

Confidential © 2008 Teleca AB 12 Recommended Reading Threads Primer: A Guide to Multithreaded ProgrammingThreads Primer: A Guide to Multithreaded Programming Thread Time: The Multithreaded Programming GuideThread Time: The Multithreaded Programming Guide Pthreads Programming: A POSIX Standard for Better MultiprocessingPthreads Programming: A POSIX Standard for Better Multiprocessing Win32 Multithreaded Programming

Confidential © 2008 Teleca AB The End Thanks