75th Lesson mutual exclusion between multiple threads (on)

Source: Internet
Author: User

1. Production consumer issues

(1) Description of the problem

① has n producers producing products at the same time and storing them in warehouses

② has m consumers who need to remove the product from the warehouse.

(2) Rules

When the warehouse is not full , any producer can deposit the product.

When the warehouse is not empty , any consumer can take out the product.

2. Examples of life

There may be dependencies between ① threads in addition to the timing , and there may be dependencies when reading and writing shared resources .

Use of ② toilets

③ "Crossroads"

3. Mutual exclusion between multiple threads

(1) critical resource (Critical Resource): A resource that allows only one thread to access (read/write) at a time. (Note that this is not a concept with the critical section under Windows, the critical resource is the resource to be shared read and write.)

(2) Mutual Exclusion (competition) between threads: Multiple threads need access to critical resources at the same time , which creates competition.

(3) TheQmutex class is a thread lock that guarantees mutual exclusion between threads . The use of the thread lock can guarantee the security of critical resources.

4. key member functions in Qmutex

(1) void lock () function

① when the lock is idle , gets the lock and resumes execution.

② when a lock is occupied by another thread , it blocks and waits for the lock to be released.

(2) void unlock () function:

Release Lock (acquisition and release of the same lock must appear paired in the same thread )

② if the call to unlock () is idle , that is, unlock () is called before Lock () is called, then the behavior of the program is undefined .

"Programming experiments" to solve production consumer problems

#include <QCoreApplication>#include<QThread>#include<QMutex>#include<QDebug>#defineMaxstorage 10//Maximum Stock volumeStaticQmutex G_mutex;//read/write Inventory lockStaticQmutex G_debug;//Qdebug LockStaticQString G_store;//producersclassProducer: Publicqthread{protected:    voidrun () {intCount =0;  while(true) {G_mutex.Lock(); //production data, adding data to the warehouse when not fully closed            if(G_store.count () <=maxstorage) {G_store.append (Qstring::number (Count++) %Ten)); G_debug.Lock(); Qdebug ()<< ObjectName () <<":"+G_store;            G_debug.unlock ();            } g_mutex.unlock (); Msleep (1); }    }};//ConsumerclassCustomer: Publicqthread{protected:    voidrun () { while(true) {G_mutex.Lock(); //Warehouse            if(G_store.count () >0)            {                //The first 1 characters in a stringG_store.remove (0,1); G_debug.Lock(); Qdebug ()<< ObjectName () <<":"+G_store;            G_debug.unlock ();            } g_mutex.unlock (); Msleep (1); }    }};intMainintargcChar*argv[])    {Qcoreapplication A (argc, argv);    Producer p;    Customer C; P.setobjectname ("Producer"); C.setobjectname ("Customer");    P.start ();    C.start (); returna.exec ();}

5. Summary

(1) critical resource allows only one thread to access (read/write) at a time

(2) thread Lock (Qmutex) for protecting critical resources

(3) A thread can access a critical resource only after it acquires a lock

(4) when the lock is fetched by another thread , the current thread enters the wait state.

(5) The acquisition and release of the thread lock must occur in pairs in the same thread.

75th Lesson mutual exclusion between multiple threads (on)

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.