Click here to Skip to main content
       

C / C++ / MFC

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionpthread_cancel issuememberytubis27 Jun '12 - 9:32 
Hi,
I have an issue when using this function,
 
the scenario is like that:
thread 1 invokes and creates thread 2 which monitors some data, using pthread_create with   PTHREAD_CANCEL_ENABLE and PTHREAD_CANCEL_DEFERRED, then when thread 1 finishes his job it cancels thread 2 using pthread_cancel and everything is working fine, but the problem is that it seems that the threads are dead but still connected to the main projcess.
I am usin QNX OS which is a BSD flavor, same as Unix,
 
process view:
 
37560404   1 ./omadm_client         10r JOIN            5
37560404   2 ./omadm_client         10r CONDVAR     
37560404   3 ./omadm_client         10r CONDVAR     
37560404   4 ./omadm_client         10r REPLY        
37560404   5 ./omadm_client         10r REPLY        
37560404   6 ./omadm_client         10r DEAD
37560404   7 ./omadm_client         10r DEAD
37560404   8 ./omadm_client         10r DEAD
37560404   9 ./omadm_client         10r DEAD
37560404   10 ./omadm_client         10r DEAD
 
is it ok? how can I detach them from my main proccess? I do not see the point of uisn pthread_join unless it needs to be invoked for the termination process..
 
Many thanks,
 
Yossi,
AnswerRe: pthread_cancel issue PinmvpLuc Pattyn27 Jun '12 - 10:05 
It has been a long time since I used QNX, however what follows applies to all kernels and operating systems AFAIK:
- cancelling a thread often is unreliable; in general you don't know what state the thread and its data is in when you issue the cancel command, so you could end up with inconsistent data, with locked resources, etc.
- the better approach is "cooperative termination", where the thread's code helps in the thread either keeping active or exiting regularly.
 
So what I prefer to do in a monitoring thread typically looks like this (pseudo-code):
while(!enough) {
    do whatever is needed for monitoring
    wait a while
}
and when the caller wants it to terminate, just have it set the global boolean "enough" to true, the monitoring thread will exit (although not immediately, but that most often is OK).
 
Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

GeneralRe: pthread_cancel issue Pinmemberytubis27 Jun '12 - 10:31 
Hi,
Thanks for the answer,
it is a problem because this thread is monitoring a file which it waits (sleeps) on it till something changes, so if nothing changes we will still sleep on the IO forever,
 
I free the global resources of the thread so this is not my cuncern, but do you know how to detach the thread from the process?
 
Many thanks,
 
Yossi,
AnswerRe: pthread_cancel issue PinmvpLuc Pattyn27 Jun '12 - 11:06 
ytubis wrote:
do you know how to detach the thread from the process?

Sorry, I don't recall. I'm sure you could find that easily in the doc though.
 

ytubis wrote:
if nothing changes we will still sleep on the IO forever

I tend never to do that; when the OS supports it, I prefer giving a reasonable timeout to all system calls, and then implement my own loop around it (possibly with logging and other logistics).
 
Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 26 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid