Re: pthread_cancel causing infinite memory allocation loop



captain_oldfield <toldfield@xxxxxxxxxxxxxxxx> writes:

Hi,

I have a section of "cancel-unsafe" code in a thread that I am
protecting with pthread_setcancelstate(DISABLED) and
pthread_setcancelstate(ENABLED). This code is adding to a linked list
from the C++ STL.

The majority of my code is ANSI C. On occasions I have seen the thread
get stuck in the list memory allocation loop when I have attempted to
cancel it. I find this impossible to believe, as the code should not
even be receiving a cancel notifiation, due to the
pthread_setcancelstate protection. I am using deferred cancellation,
not asynchronous.

Does anyone have any ideas how this could occur?

Using pthread_cancel() is generally a very bad idea, as you have
witnessed. I have no other advice than to rewrite your code in a way
that doesn't need it.

Also, how does pthread_cancel actually work (this is in Solaris 9) -
does it use signals underneath?

Whatever it uses, you shouldn't, as an application author, need to
care about it. The exact mechanisms used will certainly vary between
implementations.

Is C++ STL compatible with the POSIX pthreads library?

I very much doubt that STL is thread-safe without additional locking
precautions, though I am not sure about this.

--
Måns Rullgård
mans@xxxxxxxxx
.



Relevant Pages