Page 1 of 1

How can be known a fuc. is thread-safe or not ?

PostPosted: July 25th, 2008, 12:31 pm
by actalk
Hello !

How can I determine (understand) a function is thread-safe or not,

may be stupid question but for eaxample Assigned function
Code: Select all
if Assigned(tempObject) then // check if points some
or ...
if tempObject = nil then // is this threadsafe
 ...


in other thread

Code: Select all
if SOmeThread.Suspended then //check if suspended
 ...


There is abit confusion in my thread codes :)
in my thread's Execute
Code: Select all
...
if something then
  suspend
else
  terminateEvent.waitfor(...);
...

and another threads or main thread resumes or setEvent for this waiting thread...

PostPosted: July 25th, 2008, 2:47 pm
by Kambiz
There is a simple rule to make a code thread safe: The shared objects must be accessed within a critical section.

By the way, mentioning critical section doesn't mean using TCriticalSection object. Depends on what you need, a critical section can be achieved using Semaphore, Event, or Critical Section objects.

PostPosted: July 25th, 2008, 4:25 pm
by actalk
Thank you Kambiz.

I don't have shared objects in difrent threads. Each thread has its own data. My threads wait in two mode -suspend or TEvent.waitfor...it depends...

I have a problem while closing the application. While I close the app. I access to instance of TEvent(belongs to the thread) to signaled.
I think I should change the waiting method (now TEvent.waitfor method).

thanks.