130 likes | 248 Views
MultiThreads of Qt. Teleca Chengdu 26 July 2010 Author: Tom Chen. Agenda. Overview All of Threads classes Example Recommended Reading.
E N D
MultiThreads of Qt • Teleca Chengdu • 26 July 2010 • Author: Tom Chen
Agenda • Overview • All of Threads classes • Example • Recommended Reading
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 Overview
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. All of Threading classes
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(). QThread class MyThread : public QThread { public: void run(); }; void MyThread::run() { QTcpSocket socket; // connect QTcpSocket's signals somewhere meaningful ... socket.connectToHost(hostName, portNumber); exec(); }
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 QThreadStorage<QCache<QString, SomeClass> *> caches; void cacheObject(const QString &key, SomeClass *object) { if (!caches.hasLocalData()) caches.setLocalData(new QCache<QString, SomeClass>); caches.localData()->insert(key, object); } void removeFromCache(const QString &key) { if (!caches.hasLocalData()) return; caches.localData()->remove(key); }
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 consistently QMutex
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. 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. QMutexLocker
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 infrequently QReadWriteLock lock; void ReaderThread::run() { ... lock.lockForRead(); read_file(); lock.unlock(); ... } void WriterThread::run() { ... lock.lockForWrite(); write_file(); lock.unlock(); ... } QReadWriteLock
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(): 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. QSemaphore
Example • Mandelbrot Example • Semaphores Example • Wait Conditions Example
Recommended Reading • Threads Primer: A Guide to Multithreaded Programming • Thread Time: The Multithreaded Programming Guide • Pthreads Programming: A POSIX Standard for Better Multiprocessing • Win32 Multithreaded Programming