MultiThreads of Qt. Teleca Chengdu 26 July 2010 Author: Tom Chen. Agenda. Overview All of Threads classes Example Recommended Reading.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
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.
All of Threading classes
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().
class MyThread : public QThread
// connect QTcpSocket\'s signals somewhere meaningful
QThreadStorage is a template class that provides per-thread data storage.
Note that due to compiler limitations, QThreadStorage can only store pointers.
QThreadStorage<QCache<QString, SomeClass> *> caches;
void cacheObject(const QString &key, SomeClass *object)
caches.setLocalData(new QCache<QString, SomeClass>);
void removeFromCache(const QString &key)
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
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.
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
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.