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

Please discuss general Delphi programming topics here.

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

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 someor ... if tempObject = nil then // is this threadsafe ...

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

There is abit confusion in my thread codes
Code: Select all
...if something then  suspendelse  terminateEvent.waitfor(...);...

actalk
Junior Member

Posts: 29
Joined: October 5th, 2007, 3:56 pm

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.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

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.
actalk
Junior Member

Posts: 29
Joined: October 5th, 2007, 3:56 pm