[Concurrent programming] Use the thread-safe queue and the notify of the condition variable to schedule the step-by task

Source: Internet
Author: User

1 //Method 1: Build n thread directly to run2 foreach(i, size)3 {4Thread TRD (&instance::d owork, &inst);5 Lsttrd.push_back (TRD);6 }7 8 foreach(LSTTRD, join);9 Ten  One //Method 2: Use notify to handle the step-by task A  - classThreadsafe_queue - { thequeue<int>Q; - Mutex m; - condition_variable cond; -  Public: +     voidPushinti) -     { +Lock_guard<mutex>LK (m); A Q.push_back (i); at Cond.notify_one (); -     } -      -     voidWait_and_pop (int&i) -     { -Unique_lock<mutex>LK (m); inCond.wait (LK, [ This]{return!q.empty ();} ); -i =Q.front (); to Q.pop (); +     } - } the  * classInstance $ {Panax Notoginseng     //stores the original n number of data for processing -List<gdslayer>Lstinfo; the     ... +     //Store the final data (in fact, there should be intermediate process data for each step) AList<result>Lstresult; the      +     //Each step completes the index of the completed data into the corresponding queue, while the next thread waits for the queue to have elements -     //so for the data index = 0 will be: a->b->c ... $ threadsafe_queue merge_q; $ threadsafe_queue match_q; - threadsafe_queue gdspingen_q; - threadsafe_queue obsgen_q; the threadsafe_queue lefpin_q; -  Public:Wuyi     voidMerge_thread () the     { -         //If an error occurs, then whether or not to conform to the size can also exit Wu         //do you want to allocate all the possible operands in advance, lest if you first operate 0, the result notify to 3, then the Operation 3, although the original data has, but the stored procedure data has not -         //at the same time, is log information processing feasible? Is the operation of the intermediate check information safe?  About         //However, each thread is responsible for only one step, processing one data before processing the next (is that so?). There is no question of cross-over, supposedly. $         foreach(i, Lstinfo.size ()) -         { -             BOOLB_state =Domerge (Lstinfo[i]); -             if(b_state) A Merge_q.push_back (i); +         } the     } -      $     voidMatch_thread () the     { the         foreach(i, Lstinfo.size ()) the         { the             intKey =-1; - Merge_q.wait_and_pop (key); in             BOOLB_state =Domatch (lstinfo[key]); the             if(b_state) the             { About Match_q.push_back (key); the             } the         } the     } +      -     voidGdspingen_thread () the     {}Bayi      the     voidObsgen_thread () the     {} -      -     voidLefpin_thread () the     {} the}

Method 3: Use Async

Method 4: Use Package_task
Method 5: Use Promise

Method 6: Use the thread pool

[Concurrent programming] Use the thread-safe queue and the notify of the condition variable to schedule the step-by task

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.