Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "Confidential © 2008 Teleca AB MultiThreads of Qt Teleca Chengdu 26 July 2010 Author: Tom Chen."— Presentation transcript:

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

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

3 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

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

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

6 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); }

7 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

8 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

9 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();... }

10 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

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

12 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

13 Confidential © 2008 Teleca AB The End Thanks

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

Similar presentations

Ads by Google