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

猫鸠 2019-03-16 05:57:10
我想写一个用多线程来读取文件并且计算文件中的单词词频的程序。但是我一直得到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'。我试了很久都没办法解决,求助!
...全文
172 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
smwhotjay 2019-03-16
  • 打赏
  • 举报
回复
链接问题。 -lpthread
猫鸠 2019-03-16
  • 打赏
  • 举报
回复
引用 1 楼 WNs_ACE 的回复:
应该是需要添加-lpthread选项吧,你这个应该是gcc的编译器来的吧,你查一下如何添加多线程选项吧
我用的是Xcode
WNs_ACE 2019-03-16
  • 打赏
  • 举报
回复
应该是需要添加-lpthread选项吧,你这个应该是gcc的编译器来的吧,你查一下如何添加多线程选项吧
WNs_ACE 2019-03-16
  • 打赏
  • 举报
回复
Xcode一样是需要添加-lpthread选项,我没有记错的话,xcode好像也是用gun的编译工具来的,你去查一下如何添加多线程设置就找到答案了

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧