[問題] thread 使用請益
開發平台(Platform): (Ex: Win10, Linux, ...)
Linux Centos 7
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
G++ 4.8.5
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
純 C++11
使用 #include <thread>
問題(Question):
想要使用 Thread 加速現有資料計算
不知道該如何開始 ..
程式碼(Code):(請善用置底文網頁, 記得排版)
現有程式碼大至如下
int max_score = 0
for( int a = 0 ; a < 10 ; ++a ) {
for( int b = 0 ; b < 10 ; ++b ) {
int score = algorithm(a,b);
if ( score > max_score ) { max_score = score; }
}
}
簡單來說 algorithm 會帶入 a,b 全部組合
也就是 100 種排列,經過演算法計算後得到 score
我只需要計算最大 score 值
上述程式運作良好
但實際資料大概上億種組合
每次運算約 0.01 秒,耗費時間仍可觀
看過一些文章都是平行做兩件不同 function
我要計算同個 function 而且需要比較結果
找不到類似的 example (包含限制 thread 數量等)
目前機台只有四核心可用
想要透過 thread 加速,不知道該從何入手
希望板友能提供一些簡易說明
(有看過需要使用到 lock / unlcok 或是 mutex .. 因為要避免 race condition)
感謝 :)
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.36.0.3
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1495813613.A.1BD.html
→
05/27 00:00, , 1F
05/27 00:00, 1F
→
05/27 00:07, , 2F
05/27 00:07, 2F
→
05/27 00:08, , 3F
05/27 00:08, 3F
→
05/27 00:09, , 4F
05/27 00:09, 4F
→
05/27 00:12, , 5F
05/27 00:12, 5F
→
05/27 00:14, , 6F
05/27 00:14, 6F
→
05/27 00:15, , 7F
05/27 00:15, 7F
→
05/27 00:16, , 8F
05/27 00:16, 8F
→
05/27 01:18, , 9F
05/27 01:18, 9F
→
05/27 01:24, , 10F
05/27 01:24, 10F
→
06/06 23:22, , 11F
06/06 23:22, 11F
→
06/06 23:23, , 12F
06/06 23:23, 12F
→
06/06 23:23, , 13F
06/06 23:23, 13F
※ 編輯: DRLai (1.163.229.152), 06/06/2017 23:24:16
討論串 (同標題文章)