C ++ 11 Thread and c11thread

Source: Internet
Author: User

C ++ 11 Thread and c11thread
Thread

 

Start a thread

 

Using c ++ 11 to open a thread is relatively simple, as shown below:

 

#include<iostream>#include<thread>using namespace std;void hello(){    cout<<"hello kitty"<<endl;}int main(){    std::thread t(hello);     t.join();     return 0;}


Output result:

 

 

 

 


 

You can also use function objects.

 

#include<iostream>#include<thread>using namespace std;class Say_hello{public:    void operator()(){cout<<"hello";}};int main(){    Say_hello hello;    std::thread t(hello);    t.join();    return 0;}

Output result:

 

 

 

Functions with Parameters

 

When the function itself has parameters, the form parameters can be directly written after the function parameters that enable the thread, for example:

Thread t (function, t1, t2 ,...)

For example, the following two examples

 

#include<iostream>#include<thread>using namespace std;class Say_hello{public:    enum {times = 2};    void operator()(int n = times)    {        for(int i=0;i!=n;i++)            cout<<"hello"<<endl;    }};int main(){    Say_hello hello;    std::thread t(hello);    t.join();    return 0;}


The output result is:


 

 

 

When parameters are included, the output result is:

int main(){    Say_hello hello;    std::thread t(hello,5);    t.join();    return 0;}


 

 

 

Lock implementation

 

 

Multithreading to ensure thread security, when accessing shared data, avoid simultaneous access by several threads to avoid race condition)

The following is a simple example of using locks for synchronization.

 

 

 

There are 10 tickets in total, and four threads are opened to sell tickets.

 

#include<iostream>#include<thread>#include<mutex>using namespace std;int tickets_count = 10;mutex a_mutex;void sale(){    lock_guard<mutex> guard(a_mutex);    //a_mutex.lock();    while(tickets_count>0)    {       cout<<tickets_count--<<endl;    }     //a_mutex.unlock();}int main(){    std::thread t1(sale);    std::thread t2(sale);    std::thread t3(sale);    std::thread t4(sale);    t1.join();    t2.join();    t3.join();    t4.join();    return 0;}

If no lock is applied, it may cause repetition and output confusion (std: out is NOT thread-safe)

Implement thread security by locking the critical section.

 

Output result:



 

 

 


 

 


11 Qiu Fu's preschool children's Science Education Online Assignment 1

I. multiple choice questions (20 questions in total, 40 points in total .) Score: 40 V 1. Which of the following is the evaluation of children's scientific inquiry ability :()
A. absorb the spirit
B. Persistence
C. Independence
D. Operations on materials and use of tools
Correct answer: D full score: 2 score: 2
2. Help preschool children learn to classify objects according to two sets of standards. What age should they be placed?
A. 2 ~ 3 years old
B. 3 ~ 4 years old
C. 4 ~ 5 years old
D. 5 ~ 6 years old
Correct answer: D full score: 2 score: 2
3. The material premise for children to learn science is
A. Exploration attitude
B. Exploration objects
C. exploration process
D. Exploration Results
Correct answer: B full score: 2 score: 2
4. The motivation for preschool children to learn science is ().
A. Exploration attitude
B. exploration process
C. Target audience
D. Exploration Results
Correct answer: A full score: 2 score: 2
5. before observation, an observation checklist is prepared based on the observed objectives. The investigator checks items in the table one by one based on the observed events or behaviors, which of the following statements is marked and evaluated on the matching entries?
A. Check and observe
B. Behavior Check
C. observation and evaluation
D. Behavior Evaluation
Correct answer: B full score: 2 score: 2
Www.aopengzuoye.com/thread-47289-1-1.html
 
How to Learn C/c ++ multi-thread programming? Who can give me a C/C ++ library document? Now

For beginners, you can play with the pthread library.
It is very convenient to use the pthread library in codeblocks To Write multi-threaded applications.
Now the c ++ 11 language supports the multi-threaded model.

If you are interested in C/C ++, welcome to Baidu codeblocks.

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.