POSIX线程和分段错误... [英] POSIX Threading and Segmentation Fault...

查看:58
本文介绍了POSIX线程和分段错误...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿那里:)


刚加入小组,寻找解决问题的方法我和我的

三个小组成员都有(我们' 学生,制作类似FTP的

服务器/客户端作为考试项目。


我们在使用以下线程进行线程处理时会出现分段错误

代码(这段代码不是实际的代码,因为它看起来很简单,但是用于调试目的的缩短版本,但它以相同的方式进行了段落修改/>
作为真实代码):


----------------------- -------------------------------------------------- ------


#ifdef HAVE_CONFIG_H

#include< config.h>

#endif


#include< iostream>

#include< cstdlib>

#include< vector>

#include" executer.h"

#include" taskmanager.h"


using namespace std;


typede f void ReporterFunctionType(const string&);

typedef bool QueueGetFunctionType(SymbolSequence&,

PerformerFunctionType *&);


struct ThreadParameters

{

字符串主机,用户名,密码;

int port;

ReporterFunctionType * reporter;

QueueGetFunctionType * Retriever;

};


void reporter(const string&消息){

cout<<消息<<结束;

}


void * worker(void * structPtr)

{/ *

// TODO:在全局的某个地方取出它,它也在TaskManager中:

struct ThreadParameters

{

字符串主机,用户名,密码;

int port;

ReporterFunctionType *记者;

QueueGetFunctionType *猎犬;

};


//我们需要参数可读:

ThreadParameters * MyParams =(ThreadParameters *)structPtr;


(* (MyParams->记者))(在线程内部!);


//完成,所以我们释放参数struct占用的内存:

删除MyParams; * /

}


int main(int argc,char * argv [])

{

//测试开始:

pthread_t处理程序;

pthread_attr_t attr;


//为线程创建一个参数struct:

ThreadParameters *参数;

参数= new Thr eadParameters(); //注意:线程必须记住

才能在内存完成后释放内存。


//填写常用参数值:

参数 - > host ="" ;;

参数 - >用户名="" ;;

参数 - >密码=" " ;;

参数 - > port = 21;

参数 - >记者=&记者;

参数 - >检索器= NULL;


pthread_create(& handler,& attr,worker,(void *)参数);

// TEST STOP。


返回EXIT_SUCCESS;

}


----------------- -------------------------------------------------- ------------


这个项目是用kdevelop编写的,所以我们怀疑

或许kdevelop是遗忘" -lpthread make参数

(之前我们在线程时遇到段错误,因为我们在定制的makefile中忘记了这个参数
),但是我们的

使用-lpthread参数自带的makefile产生相同的

结果。编译工作,执行没有。


正如您将注意到的,线程函数中的实际代码被注释

out,只是为了尝试确定段错是否发生在线程内部,线程之前或实际的pthread_create调用中。这是后者的情况,后者就是这种情况,通过使用调试确认

工具(gdb,是吗?)。


我们认为它必须是我们的参数,但我们是

不确定是什么......帮助将非常感激,因为我们' '$

真的让我们在这里感到愚蠢的头脑;)


提前致谢,

Daniel加上船员:)

Hey there :)

Just joined the group, looking for a resolution to a problem I and my
three groups members are having (we''re students, making an FTP-like
server/client as an exam project).

We''re getting segmentation faults when threading with the following
code (this code is not the actual code as it''s going to look, but a
shortened version for debugging purposes, yet it segfaults the same way
as the "real" code):

-------------------------------------------------------------------------------

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <cstdlib>
#include <vector>

#include "executer.h"
#include "taskmanager.h"

using namespace std;

typedef void ReporterFunctionType(const string&);
typedef bool QueueGetFunctionType(SymbolSequence&,
PerformerFunctionType*&);

struct ThreadParameters
{
string host, username, password;
int port;
ReporterFunctionType* reporter;
QueueGetFunctionType* retriever;
};

void reporter(const string& message) {
cout << message << endl;
}

void *worker(void *structPtr)
{/*
// TODO: Get this out somewhere global, it''s in TaskManager, too:
struct ThreadParameters
{
string host, username, password;
int port;
ReporterFunctionType* reporter;
QueueGetFunctionType* retriever;
};

// We need the parameters readable:
ThreadParameters *MyParams = (ThreadParameters*) structPtr;

(*(MyParams->reporter))("inside the thread!");

// Done, so we deallocate the memory occupied by our parameter struct:
delete MyParams;*/
}

int main(int argc, char *argv[])
{
// TEST START:
pthread_t handler;
pthread_attr_t attr;

// Create a parameter struct for the thread:
ThreadParameters* Parameters;
Parameters = new ThreadParameters(); // NOTE: The thread must remember
to deallocate memory when it''s done.

// Fill in common parameter values:
Parameters->host = "";
Parameters->username = "";
Parameters->password = "";
Parameters->port = 21;
Parameters->reporter = &reporter;
Parameters->retriever = NULL;

pthread_create(&handler, &attr, worker, (void *) Parameters);
// TEST STOP.

return EXIT_SUCCESS;
}

-------------------------------------------------------------------------------

The project is being written in kdevelop, so we were suspecting that
perhaps kdevelop was "forgetting" the "-lpthread" make parameter
(earlier on we were getting segfaults when threading because we had
forgotten this parameter in our custom made makefile), but making our
own makefile with the -lpthread parameter in place yielded the same
result. Compiling works, execution doesn''t.

As you''ll notice, the actual code in the thread function is commented
out, simply to try to determine if the segfault was happening inside
the thread, before the thread, or in the actual pthread_create call. It
is the latter that is the case, as was confirmed by using the debug
tool ("gdb", is it?).

We''re thinking it has to be something with our parameters, but we''re
not sure what... Help will be very very much appreciated, as we''re
really scratching our dumb heads here ;)

Thanks in advance,
Daniel plus crew :)

推荐答案

DanielEKFA写道:

.....
DanielEKFA wrote:
.....
提前致谢,
Daniel加上工作人员:)
Thanks in advance,
Daniel plus crew :)




我猜这是错误的组,有特定于线程的组。但是这里

一些评论:


1.你必须初始化attr:pthread_attr_init(& attr);

2。您必须链接到pthread:-pthread(不是-lpthread)

3.您必须包含正确的标题:


externC {

#include" pthread.h"

}


我不知道为什么这会编译。也许其中一个标题包括

pthread.h。

Christoph



I guess this is the wrong group, there are thread specific ones. But here
some remarks:

1. you have to initialize attr: pthread_attr_init(&attr);
2. You have to link against pthread: -pthread (not -lpthread)
3. You have to include the right header:

extern "C" {
#include "pthread.h"
}

I do not know why this compiles. Maybe one of the other headers includes
pthread.h.

Christoph


一点点更新:注意到我们忘记了几个初始化器,所以我们

添加了它们,代码现在是:


----------- -------------------------------------------------- ------------------


#ifdef HAVE_CONFIG_H

#include< config.h>

#endif


#include< iostream>

#include< cstdlib>

#包括< vector>


#include" executer.h"

#include" taskmanager.h"


using namespace std;


typedef void ReporterFunctionType(const string&);

typedef bool QueueGetFunctionType(SymbolSequence&,

PerformerFunctionType *&);


struct ThreadParameters

{

字符串主机,用户名,密码;

int port;

ReporterFunct ionType * reporter;

QueueGetFunctionType * retriever;

};


void reporter(const string&消息){

cout<<消息<<结束;

}


void * worker(void * structPtr)

{/ *

// TODO:在全局的某个地方取出它,它也在TaskManager中:

struct ThreadParameters

{

字符串主机,用户名,密码;

int port;

ReporterFunctionType *记者;

QueueGetFunctionType *猎犬;

};


//我们需要参数可读:

ThreadParameters * MyParams =(ThreadParameters *)structPtr;


(* (MyParams->记者))(在线程内部!);


//完成,所以我们释放参数struct占用的内存:

删除MyParams; * /

}


int main(int argc,char * argv [])

{

//测试开始:

pthread_t处理程序;

pthread_attr_t attr;


pthread_attr_init( & attr);

pthread_attr_setdetachstate(& attr,PTHREAD_CREATE_DETACHED);

//为线程创建一个参数struct:

ThreadParameters *参数;

参数= new ThreadParameters(); //注意:线程必须记住

才能在内存完成后释放内存。


//填写常用参数值:

参数 - > host ="" ;;

参数 - >用户名="" ;;

参数 - >密码=" " ;;

参数 - > port = 21;

参数 - >记者=&记者;

参数 - >检索器= NULL;


pthread_create(& handler,& attr,worker,(void *)参数);

// TEST STOP。


char pikko;

cin>> pikko;


返回EXIT_SUCCESS;

}


------------ -------------------------------------------------- -----------------


我们还添加了cin>> pikko;"部分是为了确保程序不会在线程后立即退出(认为可能是问题)。


但是,唉,问题仍然存在...

A little update: Noticed we forget a couple of initializers, so we
added them and the code is now:

-------------------------------------------------------------------------------

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <cstdlib>
#include <vector>

#include "executer.h"
#include "taskmanager.h"

using namespace std;

typedef void ReporterFunctionType(const string&);
typedef bool QueueGetFunctionType(SymbolSequence&,
PerformerFunctionType*&);

struct ThreadParameters
{
string host, username, password;
int port;
ReporterFunctionType* reporter;
QueueGetFunctionType* retriever;
};

void reporter(const string& message) {
cout << message << endl;
}

void *worker(void *structPtr)
{/*
// TODO: Get this out somewhere global, it''s in TaskManager, too:
struct ThreadParameters
{
string host, username, password;
int port;
ReporterFunctionType* reporter;
QueueGetFunctionType* retriever;
};

// We need the parameters readable:
ThreadParameters *MyParams = (ThreadParameters*) structPtr;

(*(MyParams->reporter))("inside the thread!");

// Done, so we deallocate the memory occupied by our parameter struct:
delete MyParams;*/
}

int main(int argc, char *argv[])
{
// TEST START:
pthread_t handler;
pthread_attr_t attr;

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

// Create a parameter struct for the thread:
ThreadParameters* Parameters;
Parameters = new ThreadParameters(); // NOTE: The thread must remember
to deallocate memory when it''s done.

// Fill in common parameter values:
Parameters->host = "";
Parameters->username = "";
Parameters->password = "";
Parameters->port = 21;
Parameters->reporter = &reporter;
Parameters->retriever = NULL;

pthread_create(&handler, &attr, worker, (void *) Parameters);
// TEST STOP.

char pikko;
cin >> pikko;

return EXIT_SUCCESS;
}

-------------------------------------------------------------------------------

We also added the "cin >> pikko;" part to make sure the program doesn''t
exit immediately after threading (thinking that might be the problem).

But, alas, the problem remains...


您好Christoph,感谢您的回复:)


我们已经添加了attr_init电话,但它没有帮助。你是

吧,pthread.h库包含在taskmanager.h中。

我们将尝试-pthread参数而不是-lpthread,但是

-lpthread参数之前是有效的,所以我们有点

怀疑论者;)无论如何,你不会知道怎么检查是否并且

其中kdevelop在其自动生成的

makefile中设置了这样的参数,对吗?如果事实证明它确实是罪魁祸首那么仍然能够使用kdevelop

会很高兴。


再次感谢,

丹尼尔:)


PS:很抱歉长引号(我假设) - 还没有完全挂掉

googlegroups,它''只是我们的学校阻止了流量到b
newsservers,所以我不能使用knode :(

Hi Christoph, thanks for the reply :)

We already added the attr_init call, but it didn''t help. And you''re
right, the pthread.h library is included in the taskmanager.h include.
We will try the -pthread parameter instead of -lpthread, but the
-lpthread parameter is what worked before, so we''re a little bit
sceptic ;) Anyways, you wouldn''t happen to know how to check if and
where kdevelop sets such a parameter in its automatically generated
makefiles, would you? It would be nice to still be able to use kdevelop
if indeed it turns out to be the culprit :)

Thanks again,
Daniel :)

PS: Sorry about the long quotes (I assume) - not quite gotten a hang of
googlegroups yet, it''s just that our school blocks traffic to
newsservers, so I can''t use knode :(


这篇关于POSIX线程和分段错误...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆