码迷,mamicode.com
首页 > 编程语言 > 详细

学习激动人心的C++ 11

时间:2016-09-17 23:21:17      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

1->创建7个Thread,跑个非常大的循环.观察CPU

技术分享
void func(string &name)
{
    for(auto i=0;i<0xFFFFFFFF;i++)
    {
        //cout << name << this_thread::get_id() << "running \n";
    }
}
int main()
{
    string thread_01_name("AThread");
    string thread_02_name("BThread");
    string thread_03_name("CThread");
    string thread_04_name("DThread");
    string thread_05_name("EThread");
    string thread_06_name("FThread");
    string thread_07_name("HThread");



    thread thread_01(func,ref(thread_01_name));
    thread thread_02(func,ref(thread_02_name));
    thread thread_03(func,ref(thread_03_name));
    thread thread_04(func,ref(thread_04_name));
    thread thread_05(func,ref(thread_05_name));
    thread thread_06(func,ref(thread_06_name));
    thread thread_07(func,ref(thread_07_name));

    thread_01.join();
    thread_02.join();
    thread_03.join();
    thread_04.join();
    thread_05.join();
    thread_06.join();
    thread_07.join();

}
View Code

 

2->Mutex

技术分享
static int globNum = 0;
mutex g_lock;
void func()
{
    g_lock.lock();
    std::cout << " going to in thread ID " << std::this_thread::get_id() << std::endl;
    std::this_thread::sleep_for((std::chrono::seconds(1)));
    std::cout << " leaving thread ID  " << std::this_thread::get_id() << std::endl;
    globNum += 1;
    g_lock.unlock();
}
int main()
{

    std::thread t1(func);
    std::thread t2(func);
    std::thread t3(func);

    t1.join();
    t2.join();
    t3.join();

    cout << globNum << endl;
    return 0;
}
View Code

 

3->

 

 

 

4->容器类

vector <int> test;
test.push_back(1);
test.push_back(2);
for(auto &e : test )
{
    cout << e <<endl;
}

 

学习激动人心的C++ 11

标签:

原文地址:http://www.cnblogs.com/gearslogy/p/5879789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!