Http://mobile.51cto.com/symbian-272643_1.htm
Here we use the void stop () function to redefine it and let it return in a Boolean form. Actually, it is useless... just for the demo effect ~~
- bool Thread::stop()
- {
- m_stop = true;
- return m_stop;
- }
Now the problem arises. If mutex is used in the Stop () function for mutex operations, but where is the unlock () operation written? The unlock () operation has to be returned, so that the unlock () operation will never be executed...
QtThe qmutexlocker class can simplify the processing of mutex. It accepts a qmutex object as a parameter in the constructor and locks it to unlock the mutex In the destructor.
In this way, you can re-compile the stop () function as follows:
- bool Thread::stop()
- {
- QMutexLocker locker(&mutex);
- m_stop = true;
- return m_stop;
- }
Qreadwritelocker, qreadlocker, qwritelocker
The following is a section of the qreadwritelocker class object. The read/write Lock operation is relatively simple. I will not explain it here. Let's take a look.
- MyData data;
- QReadWriteLock lock;
- void ReaderThread::run()
- {
- ...
- lock.lockForRead();
- access_data_without_modifying_it(&data);
- lock.unlock();
- ...
- }
- void WriterThread::run()
- {
- ...
- lock.lockForWrite();
- modify_data(&data);
- lock.unlock();
- ...
- }
Qsemphore
QtThe semaphores in are provided by the qsemaphore class. semaphores can be understood as an extension of the mutex function. mutex can only be locked once and semaphores can be obtained multiple times, it can be used to protect a certain amount of the same resources.
The acquire (n) function is used to obtain n resources. When there are not enough resources, the caller will be blocked until there are enough available resources. The release (n) function is used to release N resources.
The qsemaphore class also provides a tryacquire (n) function, which returns immediately if there are not enough resources.
A typical semaphore application is in twoThreadA certain amount of data (datasize) is transmitted between the twoThreadUse a shared loop cache of a certain size (buffersize.
- const int DataSize = 100000;
- const int BufferSize = 4096;
- char buffer[BufferSize];
ProducerThreadWrite Data to the cache until it reaches the end point, and then start again at the start point to overwrite existing data. ConsumerThreadRead the data generated by the former.
In a producer or consumer instanceSynchronizationThere are two requirements. If the producer generates data too quickly, it will overwrite the data that the consumer has not read. If the consumer reads the data too quickly, it will overwrite the producer and read some junk data.
An effective method to solve this problem is to use two semaphores:
- QSemaphore freeSpace(BufferSize);
- QSemaphore usedSpace(0);
Freespace semaphores allow the producer to populate the cache part of the data. Usedspace semaphore controls the areas that consumers can read. These two semaphores are complementary. The freespace semaphore is initialized to buffersize (4096), indicating that at the beginning of the program, buffersize buffer units can be filled, and the semaphore usedspace is initialized to 0, indicates that no data is available in the buffer at the beginning of the Program for reading.