QT Multithreading Synchronization Summary

Source: Internet
Author: User
Tags mutex

1, Qmutex

 qmutex mutex; void func () {Mutex.lock (), ... mutex.unlock ();}  2, Qmutex teamed qmutexlocker in complex functions or exception handling, the lock () and unlock () operation of the mutex will be very complex, the entry point to lock (), at all the jumping point to unlock (), think of the egg hurts! Forgetting unlock () will be a tough thing to do, so QT introduced Qmutexlocker to avoid lock () and unlock () operations.  qmutex mutex; void Complexfunc () {Qmutexlocker locker (&mutex);//Set up Qmutexlocker objects where functions are needed, And put the mutex pointer in the back, nothing to do, and wait until the Exit function, the Qmutexlocker object local variables will be destroyed, the destruction is unlocked. It's convenient. Of course, you can also call Locker.unlock () to unlock the mutex, and then call Locker.relock () and then lock the mutex. You can also call Locker.mutex () to get a pointer to the mutex that was introduced when the locker was established. Isn't it convenient? ..........        ......}  3, Qreadwritelock This allows multiple processes to read at the same time, but only one write. And writing cannot be done at the same time.  qreadwritelock Lock; void Write () {lock.lockforwrite ();//Lock for writing ... lock.unlock ();//Unlock} void Read () {lock.lockforread ();//For reading and locking ... lock.unlock ();//Unlocking} 4, Qreadwritelock teamed up with Qreadlocker and qwritelocker  this somewhat similar to Qmutex and Qmutexlocker. It is also to avoid complicated lockforread ()/lockforwrite () and unlock () operations, and if the process is complicated, it is very cautious to the egg ache. But Qreadlocker and Qwritelocker are not the same after joining forces.Qreadwritelock Lock; void Write () {Qreadlocker locker (&lock); ...}  void Read () {Qwritelocker locker (&lock);....} The member functions of   qreadlocker and Qwritelocker are exactly the same, and the locker of the exit function is automatically destroyed and the lock is automatically unlocked. You can also call Locker.unlock () to unlock the lock, and then call Locker.relock () and lock it again. You can also call Locker.readwritelock () to get a pointer to the lock that was introduced when the locker was established.  5, qsemaphore  its member function is qsemaphore (int n = 0)//When an object is created, it can give it n resources ~qsemaphore () void acquire (int n = 1)//This operation reduces n at a time Resources, if the existing resources are less than n will block int available () const  //Returns the number of Qsemaphore resources currently available void release (int n = 1)//This operation increases the N resource bool Tryacq at a time. Uire (int n = 1)//is similar to acquire, but does not block when a request for less than N resources will immediately return bool Tryacquire (int n, int timeout)   below for producer-consumer example Description   c onst int datasize = 100000;//number of data to produce  const int buffersize = 8192;//cache size for data  char buffer[buffersize];  To define two semaphores, one to represent free space, one to represent the used space  qsemaphore freebytes (buffersize), or the size of the free space initialization is equal to the cache size  qsemaphore Usedbytes; class producer:public qthread { public:  &NBSp  void run ();  };  void Producer::run ()  {     qsrand (Qtime (0,0,0). Secsto ( Qtime::currenttime ()));     for (int i = 0; i < datasize; ++i) {         freeby Tes.acquire ();//apply for a free space, no on blocking          buffer[i% buffersize] = "ACGT" [(int) Qrand ()% 4];         usedbytes.release ();//has been used to increase the space of a, there are goods!      } }  class consumer:public qthread { public:     void Run ();  };  void Consumer::run ()  {     for (int i = 0; i < datasize; ++i) {         usedbytes.acquire ();         fprintf (stderr, "%c", buffer[i% buffersize]);          freebytes.release ();     }     fprintf (stderr, "\ n"); nbsp;}  int Main (int argc, char *argv[])  {     qcoreapplication app (argc, ARGV);     producer producer;     consumer consumer;     producer.start (); nbsp    consumer.start ();     producer.wait ();     consumer.wait ();    & Nbsp;return 0; } 6, qwaitcondition qwaitcondition the greatest benefit, I think, is the ability to wake up one or more other threads in a thread, if, of course, Other threads are waiting for some qwaitcondition, otherwise it won't work and you won't be waking up. Qwaitcondition must be used with Qmutex or qreadwritelock.

Qt Multithreading Synchronization summary

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.