2. Task Pack Multithreading Parallel Computing

Source: Internet
Author: User
Tags mutex

1#include <future>//process Communication for future results2#include <iostream>3#include <thread>4#include <string>5#include <chrono>//Time6#include <mutex>//Mutex Amount7 using namespacestd;8 9 //Create Mutex AmountTen Mutex m; One  A //Global Communication Variables -promise<string>Val; -  the voidMain () - { -     //accessing external variables [=] -Auto fun = [=] (intIndex)int +     { -         //Locking +Lock_guard<mutex>lckg (m); A  at         //Show thread ID -cout <<"Thread Number:"<< this_thread::get_id () <<"  "<< Index <<Endl; -         //wait 10 seconds -This_thread::sleep_for (Chrono::seconds (1)); -  -         //Calculation in         returnIndex *1024x768; -     }; to  +     //get the return value, create a task package -packaged_task<int(int) >pt1 (fun); thepackaged_task<int(int) >pt2 (fun); *  $Thread T1 ([&]() Panax Notoginseng     { -PT1 ( at);  the     }); +Thread T2 ([&]() A     { thePT2 ( -); +     }); -  $     //open Thread, get results, get only once $     intRes1 = Pt1.get_future ().Get(); -     intRes2 = Pt2.get_future ().Get(); -     intLast = Res1 +Res2; thecout << Last <<Endl; - Wuyi t1.joinable (); the t2.joinable (); - T1.join (); Wu T2.join (); -System"Pause"); About}

2. Task Pack Multithreading Parallel Computing

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.