截至目前,我的每個函數都有兩個線程。 Ax和Sword是Matrix對象。在C++中被問及多線程乘法運算11
thread thrd1(Add, std::ref(Axe), std::ref(Sword), std::ref(Axe));
thread thrd2(Multiply, std::ref(Axe), std::ref(Sword), std::ref(Axe));
我是線程新手,不太瞭解它。我必須將線程添加到我的乘法函數中嗎?現在它僅僅是
//Multiply the matrices
void Multiply(Matrix &a, Matrix &b, Matrix &c){
for (auto i=0; i<c.dx; ++i) {
for (auto j=0; j<c.dy; ++j) {
for (auto k=0; k<a.dy; ++k) {
c.p[i][j] += a.p[i][k] * b.p[k][j];
}
}
}
}
,但我覺得,如果我需要別的東西補充,由於他們在時間上沒有減少,同時通過設置的OpenMP線程數。誰能幫我嗎?
那麼我將如何去「設置」一些線程的使用,說我需要只使用4線程,谷歌搜索顯示openMP作爲解決方案 –
你「通過實例化許多'std :: thread'對象來設置「多個線程」。爲了獲得大量的線程,你可以使用'std :: thread :: hardware_concurrency'。明確地陳述你的要求是非常好的 - 這是爲了學習c + 11線程設施而進行某種練習,還是你真的需要這個功能?我個人對穩定和快速數字計算的看法是:不要自己寫。 –