You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 18:09:25