From: Martin B. on
Q: Is it safe to call TerminateThread from the thread itself?

TerminateThread seems to be generally considered evil, however I was
wondering if the following is safe:

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
// ...
while( keep_running() ) {
// ...
if( terminate_immediately() ) {
fast_resource_cleanup();
::TerminateThread( GetCurrentThread(), 42 );
}
// ...
}
}

cheers,
Martin
From: Andy Sinclair on
On 12/02/2010 08:55, Martin B. wrote:
> Q: Is it safe to call TerminateThread from the thread itself?
>
> TerminateThread seems to be generally considered evil, however I was
> wondering if the following is safe:
>
> DWORD WINAPI ThreadProc(LPVOID lpParameter)
> {
> // ...
> while( keep_running() ) {
> // ...
> if( terminate_immediately() ) {
> fast_resource_cleanup();
> ::TerminateThread( GetCurrentThread(), 42 );
> }
> // ...
> }
> }

This doesn't really make sense as a concept.
As you are executing the thread you wish to exit, simply replace
'TerminateThread' with 'return 0'.

Andy

From: Martin B. on
On 12.02.2010 16:58, Andy Sinclair wrote:
> On 12/02/2010 08:55, Martin B. wrote:
>> Q: Is it safe to call TerminateThread from the thread itself?
>>
>> TerminateThread seems to be generally considered evil, however I was
>> wondering if the following is safe:
>>
>> DWORD WINAPI ThreadProc(LPVOID lpParameter)
>> {
>> // ...
>> while( keep_running() ) {
>> // ...
>> if( terminate_immediately() ) {
>> fast_resource_cleanup();
>> ::TerminateThread( GetCurrentThread(), 42 );
>> }
>> // ...
>> }
>> }
>
> This doesn't really make sense as a concept.
> As you are executing the thread you wish to exit, simply replace
> 'TerminateThread' with 'return 0'.
>

The use case I was thinking off was for a workaround to prevent
DLL_THREAD_DETACH from happening.
From: Pavel A. on

"Martin B." <0xCDCDCDCD(a)gmx.at> wrote in message
news:hl42fc$6ss$1(a)news.eternal-september.org...
> On 12.02.2010 16:58, Andy Sinclair wrote:
>> On 12/02/2010 08:55, Martin B. wrote:
>>> Q: Is it safe to call TerminateThread from the thread itself?
>>>
>>> TerminateThread seems to be generally considered evil, however I was
>>> wondering if the following is safe:
>>>
>>> DWORD WINAPI ThreadProc(LPVOID lpParameter)
>>> {
>>> // ...
>>> while( keep_running() ) {
>>> // ...
>>> if( terminate_immediately() ) {
>>> fast_resource_cleanup();
>>> ::TerminateThread( GetCurrentThread(), 42 );
>>> }
>>> // ...
>>> }
>>> }
>>
>> This doesn't really make sense as a concept.
>> As you are executing the thread you wish to exit, simply replace
>> 'TerminateThread' with 'return 0'.
>>
>
> The use case I was thinking off was for a workaround to prevent
> DLL_THREAD_DETACH from happening.

Have you considered DisableThreadLibraryCalls() instead?

--pa


From: Martin B. on
On 18.02.2010 04:51, Pavel A. wrote:
>
> "Martin B." <0xCDCDCDCD(a)gmx.at> wrote in message
> news:hl42fc$6ss$1(a)news.eternal-september.org...
>> On 12.02.2010 16:58, Andy Sinclair wrote:
>>> On 12/02/2010 08:55, Martin B. wrote:
>>>> Q: Is it safe to call TerminateThread from the thread itself?
>>>>
>>>> TerminateThread seems to be generally considered evil, however I was
>>>> wondering if the following is safe:
>>>>
>>>> DWORD WINAPI ThreadProc(LPVOID lpParameter)
>>>> {
>>>> // ...
>>>> while( keep_running() ) {
>>>> // ...
>>>> if( terminate_immediately() ) {
>>>> fast_resource_cleanup();
>>>> ::TerminateThread( GetCurrentThread(), 42 );
>>>> }
>>>> // ...
>>>> }
>>>> }
>>>
>>> This doesn't really make sense as a concept.
>>> As you are executing the thread you wish to exit, simply replace
>>> 'TerminateThread' with 'return 0'.
>>>
>>
>> The use case I was thinking off was for a workaround to prevent
>> DLL_THREAD_DETACH from happening.
>
> Have you considered DisableThreadLibraryCalls() instead?
>

Thanks for pointing out the function - I'm not sure if it's of any use
for me here.

My inital problem was that I needed to terminate+wait for a thread
during DLL unload[*] and I thought that when I'd signal the thread to
TerminateThread() itself I could actually wait for the thread. Since
AFAIK there is only one Loader Lock, DisableThreadLibraryCalls won't
help me much.

So, can anyone tell me if there is a technical reason why
::TerminateThread(self) should not be used? (Apart from the resulting
resource leaks in the DLLs that would expect a DLL_THREAD_DETACH)

cheers,
Martin


[*]: I know it's said it's a bad idea to wait for anything during Dll
unload in DllMain, but sometimes it's an option your temporarily have to
live with. (With ::ExitThread(self) waiting - if you have to do it - is
just a ::Sleep(long_enough) / with ::TerminateThread(self) I could
actually do a WaitForSingleObject.)