我正在编写一个多线程的 Eratosthenes 筛,我必须使用 pthreads。我很确定这样做的方法是使用互斥锁和 cond_waits。我在程序开始时创建了 4 个线程,然后我必须强制它们等待,直到 Eratosthenes 的筛子找到一个素数。然后我必须解除阻塞线程,以便它们可以标记位数组中该素数的每次迭代。然后,他们必须再次阻塞并等待下一个素数,直到埃拉托色尼筛算法耗尽自己的新数字。
这是我的线程函数的代码:
while(!doneFlag){
printf("Thread wile loop\n");
pthread_mutex_lock(&lock);
pthread_cond_wait(&cond, &lock);
startingPosition = (double)(maxNum/numThreads) * i;
endingPosition = (double)(maxNum/numThreads) * (i+1)-1;
if(i == numThreads-1){
endingPosition = maxNum;
}
... Until the end of the function ...
pthread_mutex_unlock(&lock);
}
return (void*)0;
doneFlag 是我在埃拉托色尼筛算法完成所有数字时设置为 1 的标志。我希望带有 cond_wait() 函数的 while 循环会导致线程等待输入(只要还有输入要给出)
这是 main() 函数中的 Sieve 部分:
while(outerCounter < sqrt(maxNum)){
//searching numbers above the current for prime numbers
//printf("Sieve While\n");
for(innerCounter = outerCounter+1; innerCounter <= maxNum; innerCounter++){
//not composite
//printf("Sieve for\n");
if(composite[innerCounter] == 0){
printf("Prime found: %d\n", innerCounter);
pthread_mutex_lock(&lock);
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&lock);
outerCounter = innerCounter;
numPrimes++;
}
}
}
doneFlag = 1;
不知何故,复合数字没有被标记为复合数字(尽管有几个是)。我假设因为它与 main() 函数的竞争条件有关,它如何在线程仍在后台运行时不断找到更多素数,从而在线程仍在工作时更改素数。
我怎样才能解决这个问题?我的 locks/cond_wait 设置正确吗?我很难在网上找到这方面的资源。
谢谢!
编辑:我还想确保我的每个线程都可以同时运行该函数(该函数将数组中的元素标记为复合元素)。也许互斥锁在我的线程函数中不是一个好主意,因为我希望它们一起运行?(每个线程占用数组的不同段)