简体   繁体   English

分段错误,使用pthread_exit()终止线程

[英]Segmentation Fault, on terminating a thread using pthread_exit()

I have a client/server scenario. 我有一个客户端/服务器方案。 Client sends a message to server and immediately starts a thread that sleeps for 10 secs. 客户端向服务器发送消息,并立即启动一个睡眠10秒的线程。 While the main thread is waiting for any reply from server. 当主线程正在等待服务器的任何答复时。 If client gets any reply, within 10 sec, it will indicate the timer thread to terminate. 如果客户端在10秒内收到任何答复,它将指示计时器线程终止。 The problem I am facing is that the thread don't terminate itself, everything else (communication, indication to thread) is fine. 我面临的问题是线程不会自行终止,其他所有内容(通信,线程指示)都可以。 The code for this is: 的代码是:

server: 服务器:

if (recvfrom(conn_sock, buf, buff_length, 0, (struct sockaddr*)&client_addr, &slen) == -1)
    cout<<"ERROR: recvfrom()";

cout<<"\nRECEIVED:\nClient : "
<<"\nData : "<<buf;
cout<<"\n\n";

cout<<"\nEnter data to send(Type exit and press enter to exit) : ";
cin.getline(buf, sizeof(buf), '\n');
if(strcmp(buf,"exit") == 0)
    exit(0);

if(sendto(conn_sock, buf, buff_length, 0, (struct sockaddr*)&client_addr, slen) == -1)
    cout<<"ERROR: Problem sending data";

client: 客户:

cout<<"\nEnter data to send(Type exit and press enter to exit) :\n";
cin.getline(buf, sizeof(buf), '\n');
if(strcmp(buf,"exit") == 0)
    exit(0);

if(sendto(conn_sock, buf, buff_length, 0, (struct sockaddr*)&serv_addr, addr_len) == -1)
    cout<<"ERROR: Problem sending data";

pthread_t thread_id;
pthread_attr_t attr; // thread attribute
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&thread_id, &attr, thread_timer_process, NULL);

if(recvfrom(conn_sock, buf, buff_length, 0, (struct sockaddr*)&serv_addr, &slen) == -1)     cout<<"ERROR: recvfrom()";
else
termin = true;

cout<<"\nRECEIVED:\nServer : "
<<"\nData : "<<buf;
cout<<"\n\n";

static variables and thread_timer_process(): 静态变量和thread_timer_process():

static bool termin = false;
static int times = 10;
static const int INTERVAL_SEC = 1;
static void* thread_timer_process(void*)
{
    int i=0;
    cout<<"thread started\n";
    signal(SIGINT, signal_callback_handler);
    do
    {
        sleep(INTERVAL_SEC);
        cout<<"In thread, i : "<<i<<"\n";
        ++i;
    }
    while(i<times && termin == false);

    if(termin == true)
    {
        termin = false;
        cout<<"--is exiting-\n";
    }
    pthread_exit(NULL);
    cout<<"thread end\n";
}

Is this the right way of doing what I am trying to do? 这是做我想做的正确方法吗?

"Is this the right way of doing what I am trying to do?" “这是我想做的正确方法吗?”

Probably not. 可能不是。 Why are you calling pthread_exit(NULL); 为什么要调用pthread_exit(NULL); explicitly? 明确?

Just 只是

cout << "thread end" << endl; // Note endl flushes the output
return NULL;

instead of 代替

pthread_exit(NULL);
cout<<"thread end\n";

in the thread_timer_process() function should work well, and also may fix this error, since you're invoking undefined behavior specifying a return type for a function, but not returning anything actually (the compiler should have issued a warning about this point, did it?) . thread_timer_process()函数中应该可以正常工作,并且还可以解决此错误,因为您正在调用未定义的行为,为函数指定返回类型,但实际上未返回任何内容(编译器应该对此发出警告,它吗?)

See the reference for pthread_exit : 请参阅pthread_exit参考

An implicit call to pthread_exit() is made when a thread other than the thread in which main() was first invoked returns from the start routine that was used to create it. 当除最初调用main()的线程以外的其他线程从用于创建它的启动例程返回时,将隐式调用pthread_exit()。 The function's return value serves as the thread's exit status. 该函数的返回值用作线程的退出状态。

The behaviour of pthread_exit() is undefined if called from a cancellation cleanup handler or destructor function that was invoked as a result of either an implicit or explicit call to pthread_exit(). 如果从取消清理处理程序或析构函数中调用了pthread_exit(),则该行为是由于对pthread_exit()的隐式或显式调用而调用的。


See here also, for some more hints about the pthread_exit() behavior and usage: 另请参见此处,以获取有关pthread_exit()行为和用法的更多提示:

pthread memory leak with stack variables 带堆栈变量的pthread内存泄漏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM