简体   繁体   中英

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. 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. 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():

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); 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?) .

See the reference for 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. 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().


See here also, for some more hints about the pthread_exit() behavior and usage:

pthread memory leak with stack variables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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