标签:
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; }
标签:
原文地址:http://www.cnblogs.com/gearslogy/p/5879789.html