使用C语言pthreads多线程计算词频总和,需用mutex解决计数问题
多线程单词频率统计(使用互斥锁避免计数错误)
我正在运行三个线程,尝试计算每个单词的总出现频率,需要使用mutex来避免计数错误。
运行输出结果
57848 index is 0 37389 index is 1 8447 index is 2 10016 index is 3 2756 index is 4
统计结果
- he -> 57848
- she -> 37389
- they -> 8447
- him -> 10016
- me -> 2756
相关代码片段
void threadfunc(char *path, char *filetowrite, long specialfreq[], int num_threads){ DIR* dir = opendir(path); if(dir == NULL){ return; } struct dirent* entity; entity = readdir(dir); for (int i = 0; i < num_threads; i++) { if (pthread_join(thread_id[i], NULL) != 0) { perror("Failed to join thread"); } } }
内容的提问来源于stack exchange,提问作者user19603927
相关产品推荐
相关产品推荐

