Introduction to QT thread synchronization instances (2)

Source: Internet
Author: User

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 ~~

     
     
    1. bool Thread::stop()  
    2. {  
    3.     m_stop = true;  
    4.     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:

     
     
    1. bool Thread::stop()  
    2. {  
    3.     QMutexLocker locker(&mutex);  
    4.     m_stop = true;  
    5.     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.

     
     
    1. MyData data;  
    2. QReadWriteLock lock;  
    3. void ReaderThread::run()  
    4. {  
    5.     ...  
    6.     lock.lockForRead();  
    7.     access_data_without_modifying_it(&data);  
    8.     lock.unlock();  
    9.     ...  
    10. }  
    11. void WriterThread::run()  
    12. {  
    13.     ...  
    14.     lock.lockForWrite();  
    15.     modify_data(&data);  
    16.     lock.unlock();  
    17.     ...  

    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.

     
     
    1. const int DataSize = 100000;  
    2. const int BufferSize = 4096;  
    3. 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:

     
     
    1. QSemaphore freeSpace(BufferSize);  
    2. 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.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.