Thread functions are thread-safe?
Author |
Message |
Rafael Pivat #1 / 10
|
 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 |
|
 |
Igor Tandetni #2 / 10
|
 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 |
|
 |
Rafael Pivat #3 / 10
|
 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 |
|
 |
Igor Tandetni #4 / 10
|
 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 |
|
 |
Arnaud Debaen #5 / 10
|
 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 |
|
 |
William DePalo [MVP VC++ #6 / 10
|
 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 |
|
 |
Igor Tandetni #7 / 10
|
 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 |
|
 |
Carl Daniel [VC++ MVP #8 / 10
|
 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 |
|
 |
William DePalo [MVP VC++ #9 / 10
|
 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 |
|
 |
Rafael Pivat #10 / 10
|
 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 |
|
|
|