新人求助,想用多线程完成文件读取,但是遇到了错误一直没办法解决

猫鸠 猫鸠 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我想写一个用多线程来读取文件并且计算文件中的单词词频的程序。但是我一直得到No matching function for call to 'pthread_create'这个错误,上网搜索了很久也没办法解决。



#include <iostream>
#include <fstream>
#include <cstdlib>
#include <pthread.h>
#include <string.h>
#include <map>

using namespace std;

#define NUM_THREADS 5

void *countWordFrequency(void *str[100])
{
//将void类型转换成string类型
string s = (string)(char *)str;

int count = 0, c = 0, i, j = 0, k, space = 0;
char p[50][100], ptr1[50][100];

for (i = 0;i< s.length();i++)
{
if ((s[i] == ' ')||(s[i] == ',')||(s[i] == '.'))
{
space++;
}
}
for (i = 0, j = 0, k = 0;j < s.length();j++)
{
if ((s[j] == ' ')||(s[j] == 44)||(s[j] == 46))
{
p[i][k] = '\0';//把正在录入的设置成空格
i++;
k = 0;
}
else
p[i][k++] = s[j]; //str[j] probably is the array which store the words
//that are input into this vessel
//i probably the number of the paticular word
//j 是为了确认这个循环不超过单词的d总数量
}
k = 0; //
for (i = 0;i <= space;i++)
{
for (j = 0;j <= space;j++)
{
if (i == j)
{
strcpy(ptr1[k], p[i]);//ptr1[k] is the pointer to the destination
//array that the content is be copied.
//p[i] is the string to be copied.
//这个复制是复制一整个词而不是一个字母
k++;
count++;//i think the count is the number of the frequency words
break;
}
else
{
if (strcmp(ptr1[j], p[i]) != 0)//strcmp is the comparing function
//! = 0 means that they are paired
//successively
continue;//then continue means that if it satify the
//requirements then skip these sentence, restart the
//sentence from the beginning.
//so this continue means if ptr1 == p then step out
//this circle and restart
else
break;
}
}
}
for (i = 0;i < count;i++)
{
for (j = 0;j <= space;j++)
{
if (strcmp(ptr1[i], p[j]) == 0)
c++;
}
printf("%s -> %d times\n", ptr1[i], c);//ptr1[i] is the place to store the
//words of this code
c = 0;
}

pthread_exit(NULL);

return 0;
}

string readByLine()
{
char buffer[256];
fstream outFile;
outFile.open("book.txt",ios::in);

while(!outFile.eof())
{
outFile.getline(buffer,256,'\n');//getline(char *,int,char) 表示该行字符达到256个或遇到换行就结束
cout<<buffer<<endl;
}
outFile.close();

return 0;
}

int main ()
{
pthread_t threads[NUM_THREADS];
int indexes[NUM_THREADS];
int rc;
int i;

//read file
string r = readByLine();

for( i=0; i < NUM_THREADS; i++ ){
indexes[i] = i; //先保存i的值
// 传入的时候必须强制转换为void* 类型,即无类型指针
rc = pthread_create(&threads[i], NULL,
*countWordFrequency, (void *)&i);
if (rc){
cout << "Error: can create threads," << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);
}



我遇到的错误是在117行,No matching function for call to 'pthread_create'。我试了很久都没办法解决,求助!
...全文

相关推荐

MATLAB常见错误及解决办法

MATLAB常见错误翻译,及解决办法,仅供参考

asunfengpeng

java使用多线程读取超大文件

主要为大家详细介绍了java使用多线程读取超大文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

weixin_38713009

Android 中读取SD卡文件时抛出NullPointerException错误解决办法

主要介绍了Android 中读取SD卡文件时抛出NullPointerException错误解决办法的相关资料,需要的朋友可以参考下

weixin_38674569

Java多线程读取大文件

前言   是五一假期第,按理应该是快乐玩耍的日子,但是作为一个北漂到京师的开发人员,实在难想出去那玩耍。好玩的地方比较远,近处又感觉没意思。于是乎,闲着写篇文章,总结下昨天写的程序吧。   昨天下午朋友跟我聊起,他说有个需求,需要把上G的txt文件读取写入到数据库。用普通的io结果自然是OOM了,所以果断用NIO技术。为了提高速度,自然还得用上多线程技术。   接下来介绍一下实现思路以及相关的知识点。   内容   一、对文件分区   为了充分利用多线程读取,需要把文件划分成多个区域,供每个线程读取。那么需要有一个算法来计算出每个线程读取的开始位置和结束位置。那么首先根据配置的线程

weixin_38707217

删除无法读取源文件或磁盘的文件的解决办法

删除无法读取源文件或磁盘的文件 删除系统找不到指定路径的文件

qhmctt
评论 4
收藏 已收藏
分享
返回顶部
猫鸠
C++ 语言 65,214 成员 250,514 内容