Re: atexit handler: pthread_cancel, pthread_testcancel, pthread_join problem



Salut Joe,

> I think exit() just terminates all the threads so there are no threads to
> cancel or join. You should probably get EINVAL though unless something
> somewhere says you can have undefined behavior. The core dump is probably
> from the thread id being invalid. Thread id's are opaque handles and on
> Linux appear to be pointers to thread local storage and you tend to get core
> dumps when no longer valid thread id's are dereferenced.

It is true that, potentially, exit() may invalidate thread's ID before
calling the atexit handler. However, accordingly to SUS:

<copy>
The exit() function shall first call all functions registered by
atexit(), in the reverse order of their registration, except that a
function is called after any previously registered functions that had
already been called at the time it was registered. Each function is
called as many times as it was registered. If, during the call to any
such function, a call to the longjmp() function is made that would
terminate the call to the registered function, the behavior is
undefined.
</copy>

So, from my understanding, OP code is legal and should work.

( Note aside. In the case you join a thread with an invalid thread ID,
the function fails with ESRCH. )


> exit isn't a clean way to terminate threads so the obvious answer is don't
> use it if you want clean thread termination.

I share Joe's opinion here. It doesn't make a lot of sense to register
an atexit handler that cancels the thread. The function exit() does
just that. If you need a cleaner termination mechanism, you might want
to implement another solution (based, for instance, on a condition
variable).

Regards,
Loic.

.



Relevant Pages

  • Re: long threads
    ... >>However, before the file has completed uploading, say, the user exits the ... > You must wait for the other thread to terminate. ... When the user wants to exit, ... the terminate request (CANCEL)? ...
    (comp.lang.java.programmer)
  • Re: Detect console close
    ... None of these work when a console running a Python script is simply closed. ... Break to terminate the script, again it will exit without doing any ... exception instead of terminating the program. ...
    (comp.lang.python)
  • Re: Use of TerminateThread Function
    ... clean up any resources allocated by the thread it's terminating and so ... Your while loop seems a bit strange... ... normal event or an "exit" event. ... terminate a thread it's time for some code review... ...
    (microsoft.public.windowsce.platbuilder)
  • Re: C++ Question: Crashing C++
    ... The problem is that the word "crash" is a meaningless noise word. ... Newbie developers use it to describe ... value as calling exit() explicitly: only newbies make those mistakes, ... that the user and the user alone owns the right to terminate the program escapes them. ...
    (microsoft.public.vc.mfc)
  • Re: how to run a command-line program from Perl/Tk (ExecuteCommand?)
    ... I _am_ detaching the thread, so I cannot join it explicitly, if I am ... Another thing to watch for, is using 'exit' in a thread, will exit all ... threads and terminate the application. ... if ($Script) { ...
    (comp.lang.perl.tk)