Thread functions are thread-safe? 
Author Message
 Thread functions are thread-safe?

Hi,

Thread functions like _beginthreadex, _endthreadex, TerminateThread,
GetExitCodeThread, are thread-safed?

I am using this functions to access the same handle between various
threads... Is that ok?

(maybe the handle must be...) :-/ not sure...

--
Rafael Pivato
Correio do Povo
Porto Alegre/RS - Brazil



Tue, 22 Nov 2005 07:33:21 GMT  
 Thread functions are thread-safe?

Quote:
> Thread functions like _beginthreadex, _endthreadex, TerminateThread,
> GetExitCodeThread, are thread-safed?

_beginthreadex does not access any shared resources - two simultaneous
calls create two independent threads. I'm not sure what you mean by it
being thread-safe.

The same for _endthreadex - it applies to the calling thread, so two
simultaneous calls on different threads are OK - each call ends its
calling thread and does not affect the other thread in any way.

TerminateThread and GetExitCodeThread are thread-safe.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Wed, 23 Nov 2005 03:05:22 GMT  
 Thread functions are thread-safe?
what about Resume and Suspend?

thanks...



Quote:


> > Thread functions like _beginthreadex, _endthreadex, TerminateThread,
> > GetExitCodeThread, are thread-safed?

> _beginthreadex does not access any shared resources - two simultaneous
> calls create two independent threads. I'm not sure what you mean by it
> being thread-safe.

> The same for _endthreadex - it applies to the calling thread, so two
> simultaneous calls on different threads are OK - each call ends its
> calling thread and does not affect the other thread in any way.

> TerminateThread and GetExitCodeThread are thread-safe.
> --
> With best wishes,
>     Igor Tandetnik

> "For every complex problem, there is a solution that is simple, neat,
> and wrong." H.L. Mencken



Wed, 23 Nov 2005 04:05:48 GMT  
 Thread functions are thread-safe?
All the Win32 API functions working with threads are thread-safe, in the
sense that they don't crash when called simultaneously. However, if one
thread is calling SuspendThread and another is calling ResumeThread at
the same time, both manipulating some third thread, the final state of
this third thread is unpredictable. If SuspendThread happens to execute
first, then ResumeThread resumes it and the thread is running. If
ResumeThread happens to execute first, it fails (because the thread is
already running) and SuspendThread then suspends it.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> what about Resume and Suspend?

> thanks...





> > > Thread functions like _beginthreadex, _endthreadex,
TerminateThread,
> > > GetExitCodeThread, are thread-safed?

> > _beginthreadex does not access any shared resources - two
simultaneous
> > calls create two independent threads. I'm not sure what you mean by
it
> > being thread-safe.

> > The same for _endthreadex - it applies to the calling thread, so two
> > simultaneous calls on different threads are OK - each call ends its
> > calling thread and does not affect the other thread in any way.

> > TerminateThread and GetExitCodeThread are thread-safe.
> > --
> > With best wishes,
> >     Igor Tandetnik

> > "For every complex problem, there is a solution that is simple,
neat,
> > and wrong." H.L. Mencken



Wed, 23 Nov 2005 04:12:58 GMT  
 Thread functions are thread-safe?
Quote:

> what about Resume and Suspend?

"Thread safety" is meaningful only when there is a unique resource
(variable, file, whatever else...) shared by several threads. In this
context, "thread safety" means that you insure that the resource is accesded
by the different threads in such a way that it's state is always correct and
predictable.

In all the examples you have spoken about (CreateThread, beginthreadex,
SuspendThread, ....), there is no such unique, shared ressource. Therefore
"thread safety" is irrelevant to these functions. If you explain more
clearly what worries you, we may be able to give you more information.

Arnaud
MVP - VC



Wed, 23 Nov 2005 05:54:12 GMT  
 Thread functions are thread-safe?

Quote:
> All the Win32 API functions working with threads are thread-safe, in the
> sense that they don't crash when called simultaneously. However, if one
> thread is calling SuspendThread and another is calling ResumeThread at
> the same time, both manipulating some third thread, the final state of
> this third thread is unpredictable.

One function decrements the suspend count and one increments it so after
both operations are complete the thread is as it was "before", no?

Regards,
Will



Wed, 23 Nov 2005 07:44:36 GMT  
 Thread functions are thread-safe?


Quote:


> > All the Win32 API functions working with threads are thread-safe, in
the
> > sense that they don't crash when called simultaneously. However, if
one
> > thread is calling SuspendThread and another is calling ResumeThread
at
> > the same time, both manipulating some third thread, the final state
of
> > this third thread is unpredictable.

> One function decrements the suspend count and one increments it so
after
> both operations are complete the thread is as it was "before", no?

But can the count become negative? I would expect ResumeThread to fail
and not set the count to -1 if the thread is already resumed and
running. I admit I have not actually tested it.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Fri, 25 Nov 2005 22:01:30 GMT  
 Thread functions are thread-safe?

Quote:





>>> All the Win32 API functions working with threads are thread-safe,
>>> in the sense that they don't crash when called simultaneously.
>>> However, if one thread is calling SuspendThread and another is
>>> calling ResumeThread at the same time, both manipulating some third
>>> thread, the final state of this third thread is unpredictable.

>> One function decrements the suspend count and one increments it so
>> after both operations are complete the thread is as it was "before",
>> no?

> But can the count become negative? I would expect ResumeThread to fail
> and not set the count to -1 if the thread is already resumed and
> running. I admit I have not actually tested it.

From the docs for ResumeThread:

===========

The ResumeThread function checks the suspend count of the subject thread. If
the suspend count is zero, the thread is not currently suspended. Otherwise,
the subject thread's suspend count is decremented. If the resulting value is
zero, then the execution of the subject thread is resumed.

===========

Clearly resuming a thread that's not suspended does nothing.

-cd



Fri, 25 Nov 2005 22:30:35 GMT  
 Thread functions are thread-safe?

Quote:
> But can the count become negative? I would expect ResumeThread to fail
> and not set the count to -1 if the thread is already resumed and
> running. I admit I have not actually tested it.

Mea culpa, mea culpa, mea maxima culpa. :-) I didn't realize that the resume
operation fails when the count is zero. It is a good thing I don't use that
function. :-)

Regards,
Will



Sat, 26 Nov 2005 01:41:01 GMT  
 Thread functions are thread-safe?
will try...

The resource in question is the thread handle.



Quote:

> > what about Resume and Suspend?

> "Thread safety" is meaningful only when there is a unique resource
> (variable, file, whatever else...) shared by several threads. In this
> context, "thread safety" means that you insure that the resource is
accesded
> by the different threads in such a way that it's state is always correct
and
> predictable.

> In all the examples you have spoken about (CreateThread, beginthreadex,
> SuspendThread, ....), there is no such unique, shared ressource. Therefore
> "thread safety" is irrelevant to these functions. If you explain more
> clearly what worries you, we may be able to give you more information.

> Arnaud
> MVP - VC



Sat, 26 Nov 2005 22:24:08 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Using a non-thread-safe library with threads?

2. how do i create thread safe worker thread

3. ANN: sigslot - C++ Portable, Thread-Safe, Type-Safe Signal/Slot Library

4. Why is function static singleton not thread-safe?

5. Is this function thread safe ?

6. Another thread as a member function thread

7. threads, threads, and more threads

8. Is NetworkStream.Write() thread safe?

9. Why OleDbCommand objects are not thread safe?

10. Thread-safe DataTable.Rows.Add

11. Is ADO really thread-safe?

12. Thread Safe Events and Static Methods

 

 
Powered by phpBB® Forum Software