首页 > 代码库 > 学习激动人心的C++ 11
学习激动人心的C++ 11
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();}
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;}
3->
4->容器类
vector <int> test;test.push_back(1);test.push_back(2);for(auto &e : test ){ cout << e <<endl;}
学习激动人心的C++ 11
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。