我试图用pthreads做一个练习,做一个相当简单的搜索,我把一个文本文件分成5列,5个线程每个执行搜索。在此之后,将进行合并,以确定输入是否与文本文件中的任何单个条目匹配。然而,当我运行它时,它似乎挂起了。我想我有一个各种各样的竞争条件,但是用DDD调试还没有产生任何结果。
我认为我正在正确地创建和连接pthread,但我不确定我是否正确地使用了互斥锁。
search函数在给定列中创建一个唯一值的链表,然后创建一个位掩码,用于保存找到该值的索引。无论如何,如果我运行一个标准的线性搜索,我得到7更多的结果为我的测试数据,如果我做它线程,我认为这意味着某些东西是失败的搜索或合并线程。将我的数据集的批大小从10%块减少到一次1或2个值会增加匹配的数量,但我仍然会错过正确的总数。
如果有人知道为什么程序在完成之前挂起(可能是由于搜索或合并线程),我将非常感谢帮助。
为了简洁起见,我省略了大约一半的代码,但如果有帮助的话,我可以发布更多的代码。void * search(void* pthread_arg){
search_thread_param * myParams = (search_thread_param*) pthread_arg;
int k=0; // how many searches have been done so far
int m;
while (k<NUM_PACKETS){
pthread_mutex_lock(&mutex);
if(n !=0){
pthread_cond_wait(&search_cv, &mutex); // mege is in progress, go to sleep
}
pthread_mutex_unlock(&mutex); // release th lock so the other search threads can acquire it
for (m =0; m < BATCH; m++)
myParams->buffer[m] = mySearchFunction(myParams->packet_field[k+m], myParams->rule_field, unique_no[myParams->thread_id]); // given a value, return its BV
k=k+m;
pthread_mutex_lock(&mutex);
if (n==NUM_FIELDS-1) // this search thread is the only active one
{
n++;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&merge_cv); // wake up merge thread
}
else{ // not the last one goes to sleep
n++;
pthread_mutex_unlock(&mutex);
}
}
return NULL;
}
void merge_function(unsigned long b0,unsigned long b1,unsigned long b2,unsigned long b3,unsigned long b4){
unsigned long final_BV;
final_BV = b0 & b1 & b2 & b3 & b4;
if( final_BV )
int counter = 0;
while(final_BV){
counter++;
final_BV = final_BV >> 1; //bit shift
}
if( counter)
printf("matched rule %in", counter);
}
void * merge (void* pthread_arg){
merge_thread_param * myParams = (merge_thread_param*) pthread_arg;
int k=0; // how many merges have been done so far
int m;
pthread_mutex_lock(&mutex);
if (n<NUM_FIELDS) // the first batch is not ready
pthread_cond_wait(&merge_cv, &mutex); // go to sleep
while (k<NUM_PACKETS){
for (m =0; m < BATCH; m++){
merge_function(myParams->buffer[0][m],myParams->buffer[1][m],myParams->buffer[2][m],myParams->buffer[3][m],myParams->buffer[4][m]);
}
printf("Merge thread: finish merging one batchn");
k=k+m;
n = 0; //ready to wake up all search threads
if(k ==NUM_PACKETS){ // the program is over
pthread_mutex_unlock(&mutex);
printf("Merge thread: I am done, found %i results, compared to %i linear search n = %in",res,lin,n);
return NULL;
}
else {
pthread_cond_broadcast(&search_cv);
pthread_mutex_unlock(&mutex);
pthread_cond_wait(&merge_cv, &mutex); // go to sleep
}
}
return NULL;
}
int main () {
///////setup info omitted for brevity
// create search threads
for (i=0; i< NUM_FIELDS; i++)
if(pthread_create(&s_thread[i], &attr, search, (void*) &stp[i])!=0){printf("Creating Thread failed!n");}
// create merge thread
if(pthread_create(&m_thread, &attr, merge, (void*) &mtp)!=0){printf("Creating Thread failed!n");}
// join the threads
for (i=0; i<NUM_FIELDS; i++)
if (pthread_join(s_thread[i], NULL)==0){printf("joined thread %in",i);}
else{printf("Joining thread %i failed!n",i);}
if (pthread_join(m_thread, NULL)!=0){printf("joined merge thread");}
else{printf("Joining thread failed!n");}
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&search_cv);
pthread_cond_destroy(&merge_cv);
//free dat memory
printf("finished joining all the threads");
fflush(stdout);
return 0;
}
原来问题是决定是否在合并线程中设置pthread_cond_wait的检查出错,导致合并线程在搜索线程之前执行。这意味着合并线程在搜索线程完成生成结果之前就完成了。然后,搜索线程向一个不存在的线程发出信号,并进入睡眠状态。它从未连接,因为它从未完成。好了,就这样了