+ All Categories
Home > Documents > Confidential © 2008 Teleca AB MultiThreads of Qt Teleca Chengdu 26 July 2010 Author: Tom Chen.

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

Date post: 26-Mar-2015
Category:
Upload: melanie-moore
View: 219 times
Download: 0 times
Share this document with a friend
Popular Tags:
13
Confidential © 2008 Teleca AB MultiThreads of Qt Teleca Chengdu 26 July 2010 Author: Tom Chen
Transcript
Page 1: Confidential © 2008 Teleca AB MultiThreads of Qt Teleca Chengdu 26 July 2010 Author: Tom Chen.

Con

fide

ntia

200

8 T

elec

a A

B

MultiThreads of Qt

Teleca Chengdu

26 July 2010

Author: Tom Chen

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

Con

fide

ntia

200

8 T

elec

a A

B

2

Overview

All of Threads classes

Example

Recommended Reading

Agenda

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

Con

fide

ntia

200

8 T

elec

a A

B

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

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

Con

fide

ntia

200

8 T

elec

a A

B

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.

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

Con

fide

ntia

200

8 T

elec

a A

B

5

QThreadThe 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().

class MyThread : public QThread { public: void run(); };

void MyThread::run() { QTcpSocket socket; // connect QTcpSocket's signals somewhere meaningful ... socket.connectToHost(hostName, portNumber); exec(); }

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

Con

fide

ntia

200

8 T

elec

a A

B

6

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

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

Con

fide

ntia

200

8 T

elec

a A

B

7

QMutexThe 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

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

Con

fide

ntia

200

8 T

elec

a A

B

8

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

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

Con

fide

ntia

200

8 T

elec

a A

B

9

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

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

Con

fide

ntia

200

8 T

elec

a A

B

10

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

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

Con

fide

ntia

200

8 T

elec

a A

B

11

Example

•Mandelbrot Example

•Semaphores Example

•Wait Conditions Example

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

Con

fide

ntia

200

8 T

elec

a A

B

12

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

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

Con

fide

ntia

200

8 T

elec

a A

B

The End Thanks


Recommended