/*C++11 原子類型測試 問題:如果有多個原子需要操作,如何保障並行的序列化*/ #include <thread>#include <stdlib.h>#include <atomic> #include <iostream>#include <time.h>#include <vector>#include "mytimer.h"using namespace std;//用原子資料類型作為共用資源的資料類型atomic_long total(0);atomic_int i(0);//long total = 0;TimeVal a, b;void click(){//由於有兩個原子類型,必須保證更新的順序性 while (i++ < 10000000) //原子操作,保障了順序操作 total += 1; ///*錯誤while(i < 1000000) //一次讀取{i++; //上次讀取i到這裡未防止再次讀取i的值total++;}*/}//如果用單線程計算的話long commonclick(){long total = 0;for(int i = 0; i < 10000000; i++)total++;return total;}int main(int argc, char* argv[]){int N = 16;double time0, time1;if (argc == 2)N = atoi(argv[1]);//主線程測試get_current_time( a );long val = commonclick();get_current_time( b );cout << "Main thread" << endl;cout<<"result:"<<val<<endl;time0 = getDeltaTime(b, a) * 1e3; cout<<"duration:"<< time0 <<"ms"<<endl;cout << "Single thread" << endl;//單線程測試get_current_time( a );thread t1(commonclick);t1.join();get_current_time( b ); cout<<"duration:"<< getDeltaTime(b, a) * 1e3 <<"ms"<<endl;cout << "Multi thread " << N << endl; // 計時開始 get_current_time( a ); // 建立N個線程類比點擊統計 vector<thread> threads(N); for(int i=0; i<N;++i) { threads[i] = thread(click); } for(int i=0; i<N;++i) threads[i].join(); // 計時結束 get_current_time( b ); // 輸出結果 cout<<"result:"<<total<<endl;time1 = getDeltaTime(b, a) * 1e3; cout<<"duration:"<< time1 <<"ms"<<endl;cout << "Delay for each thread: " << time1 / N - time0 << "ms, about " << time1/N / time0 << " time of main thread." << endl; return 0;}
mytimer.h檔案
#ifndef __MYGLIB_H__#define __MYGLIB_H__#include <windows.h>typedef LARGE_INTEGER TimeVal;LARGE_INTEGER freq;void get_current_time(TimeVal& s){QueryPerformanceFrequency(&freq);QueryPerformanceCounter(&s); }double getDeltaTime(TimeVal t2, TimeVal t1){double time = (double)(t2.QuadPart-t1.QuadPart)/(double)freq.QuadPart; return time;}#endif
編譯:
g++ -std=c++11 atomic_par.cpp
運行結果:
運行a 2
Main thread
result:10000000
duration:25.7743ms
Single thread
duration:29.4485ms
Multi thread 2
result:10000000
duration:372.861ms
Delay for each thread: 160.656ms, about 7.23319 time of main thread.
運行a 4
Main thread
result:10000000
duration:22.2332ms
Single thread
duration:23.3911ms
Multi thread 4
result:10000000
duration:472.065ms
Delay for each thread: 95.783ms, about 5.3081 time of main thread.
運行a 8
Main thread
result:10000000
duration:22.2252ms
Single thread
duration:22.7899ms
Multi thread 8
result:10000000
duration:468.485ms
Delay for each thread: 36.3355ms, about 2.63488 time of main thread.
運行a 16
Main thread
result:10000000
duration:22.399ms
Single thread
duration:23.7603ms
Multi thread 16
result:10000000
duration:489.68ms
Delay for each thread: 8.20599ms, about 1.36636 time of main thread.
從最後結果看,每個線程由於任務少了,所用時間短了,但總的系統用於線程調度的時間多了。