基本上我需要主線程繼續根據一些全局變量的值進行一些操作,這些全局變量可以由輔助線程編輯(在某些選定的時間間隔)。是這樣的:OpenMP將一個函數的執行分配給一個線程?
vector<int> mySharedVar;
void secondaryThreadFunction() {
Do some operations
And update mySharedVar if necessarily
}
int main() {
int count = 0;
while(true) {
count++;
if (count%100) { //> Each 100 iterations run a parallel thraed
//> RUN secondaryThreadFunction in a separateThread
}
this is the main thread that based its operation on mySharedVar
}
}
這是OpenMP的命令與secondaryThreadFunction();
運行單個並行線程?
是否有比這任何其他更好的辦法:
#pragma omp parallel num_threads(2)
{
int i = omp_get_thread_num();
if (i == 0){
mainThread();
}
if (i == 1 || omp_get_num_threads() != 2){
secondaryThreadFunction();
}
}
我想是OpenM P部分是少一點hacky。 – Mysticial 2012-07-18 09:47:35
@Mysticial:嗨,大聲笑,我從你的答案採取該示例:D http://stackoverflow.com/questions/7876156/openmp-run-two-functions-in-parallel-each-by-half-of-thread-無論如何,你可以給我看一個樣本嗎? :D – dynamic 2012-07-18 09:49:53
哈哈,這個問題似乎有一個很好的例子:http://stackoverflow.com/questions/2770911/how-does-the-sections-directive-in-openmp-distribute-work – Mysticial 2012-07-18 09:52:10