2014-12-01 40 views
1

我有一個客戶端/服務器方案。客戶端向服務器發送消息並立即啓動睡眠10秒的線程。主線程正在等待來自服務器的任何答覆。如果客戶得到任何答覆,在10秒內,它將指示計時器線程終止。我面臨的問題是線程不會自行終止,其他任何事情(通信,線程指示)都可以。這個代碼是:分段錯誤,使用pthread_exit()終止一個線程

服務器:

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"; 

客戶端:

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"; 

靜態變量和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"; 
} 

這是做的正確方法是什麼我正在努力?

+0

什麼阻止一個線程訪問'termin'而另一個線程正在修改呢? – 2014-12-01 20:34:51

+0

應該有一些鎖。我的錯。 – user2831683 2014-12-01 20:56:11

回答

1

「這是做我正在做的事情的正確方法嗎?」

可能不是。你爲什麼明確地打給pthread_exit(NULL);

只是

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

,而不是

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

應該很好地工作,而且還可以解決這個錯誤,因爲你調用未定義行爲指定返回類型的函數,但實際上沒有返回任何東西(the compiler should have issued a warning about this point, did it?)

reference for pthread_exit

到了pthread_exit隱式調用()由當比其中主()已首先被調用從開始例程返回線程用於創建它的其他線程。該函數的返回值用作線程的退出狀態。

了pthread_exit的行爲()如果從被援引作爲隱式或顯式調用的結果到了pthread_exit()的取消清理處理或destructor函數調用是未定義的。


看到這裏還,有關pthread_exit()行爲和使用一些提示:

pthread memory leak with stack variables

+0

它的工作!但是用正確的方式使用全局變量「終止」來終止這個終止,還有沒有其他方法可以指示線程自行終止。謝謝,回答 – user2831683 2014-12-01 20:06:06

+0

@ user2831683 *** ...如果從取消清除處理程序中調用,則不確定... ***您是否真的需要_global變量_?我建議將線程管理封裝到類中,並使其更容易使用['std :: thread'](http://en.cppreference.com/w/cpp/thread/thread)。 – 2014-12-01 20:07:27

+0

pthread_exit(),不適合我使用它的方式,對不對?那麼,在任何情況下,我們都需要這種類型的終止,所以返回NULL只是簡單地實現這個技巧 – user2831683 2014-12-01 20:11:41