[英]Why do I get a segmentation fault when using pthread_join?
Here is the code I have, it compiles and runs using g++ but I get a segmentation fault. 这是我的代码,它使用g ++编译并运行,但出现分段错误。 I know it happens around the pthread_join statement but I cant figure out why.
我知道它发生在pthread_join语句周围,但是我不知道为什么。
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <pthread.h>
#include <stdlib.h>
#include <sstream>
using namespace std;
struct data{
string filename;
int x;
int y;
};
void *threadFunction(void *input){
data *file = (data *) input;
string filename = file->filename;
ifstream myFile;
int xCount = 0;
int yCount = 0;
myFile.open(filename.c_str());
string line;
while(myFile >> line){
if(line == "X"){
xCount++;
}else if(line == "Y"){
yCount++;
}
}
file->x = xCount;
file->y = yCount;
return (void *) file;
}
int main(){
pthread_t myThreads[20];
data *myData = new data[20];
for(int i = 0; i < 20; i++){
ostringstream names;
names << "/filepath/input" << i+1 << ".txt";
myData[i].filename = names.str();
myData[i].x = 0;
myData[i].y = 0;
}
for(int i = 0; i < 20; i++){
int check = pthread_create(&myThreads[i], NULL, threadFunction, (void *) &myData[i]);
if(check != 0){
cout << "Error Creating Thread\n";
exit(-1);
}
}
int xCount = 0;
int yCount = 0;
for(int i = 0; i < 20; i++){
data* returnedItem;
pthread_join(myThreads[i], (void**) returnedItem);
xCount += returnedItem->x;
yCount += returnedItem->y;
}
cout << "Total X: " << xCount << "\n";
cout << "Total Y: " << yCount << "\n";
}
Am I not calling return properly from my threadFunction? 我不是从threadFunction正确调用return吗? I've been trying a bunch of different things and I still don't know what's going on...any help would be greatly appreciated!
我一直在尝试很多不同的事情,但我仍然不知道发生了什么...任何帮助将不胜感激! (the text file I open contain either an X or Y per line. My goal is to count the total number of Xs and Ys in 20 text files)
(我打开的文本文件每行包含X或Y。我的目标是计算20个文本文件中X和Y的总数)
pthread_join(myThreads[i], (void**) returnedItem);
should be 应该
pthread_join(myThreads[i], (void**) &returnedItem);
You're asking join to set the value of returnedItem
to be whatever void*
your thread function returned ... so you need to give the address of returnedItem
. 你问加入到设定值
returnedItem
是什么void*
你的线程函数返回...所以你需要给的地址 returnedItem
。
The second argument to pthread_join()
is a void**
into which the result is to be stored. pthread_join()
的第二个参数是void**
,结果将存储在其中。 You are passing a random value, however. 但是,您正在传递随机值。 This could should rather look something like this:
这应该看起来像这样:
void* result;
pthread_join(myThread[i], &result);
data* returnedItem = static_cast<data*>(result);
Of course, this assumes that a data*
was, indeed, returned. 当然,这假设确实返回了
data*
。
Second argument of pthread_join
will be used to return, return value of the thread, so somewhere inside pthread_join
we have a code that call *secondArgument = thread_return_value
, but lets look at what you are doing here: pthread_join
第二个参数将用于返回线程的返回值,因此在pthread_join
内的某个地方,我们有一个代码调用*secondArgument = thread_return_value
,但让我们在这里看一下自己在做什么:
// You are not initializing returnedItem, so it contain some garbage value
// For example 0x12345678
data* returnedItem;
// Now you cast that garbage to (void**), and pthread_join will call
// *(0x12345678) = thread_return_value that will cause segmentation fault
pthread_join(myThreads[i], (void**) returnedItem);
But you want return value to be copied to returnedItem
, am I right? 但是您想将返回值复制到
returnedItem
,对吗? in case that you answer is yes you should pass address of returnedItem
to pthread_join
so it can copy it there. 如果您的回答是肯定的,则应将
returnedItem
地址传递给pthread_join
以便它可以将其复制到那里。 So change your call to: 因此,将您的电话更改为:
pthread_join(myThreads[i], (void**) &returnedItem);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.