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 ?

Postby actalk » July 25th, 2008, 12:31 pm

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...
actalk
Junior Member
Junior Member
 
Posts: 29
Joined: October 5th, 2007, 3:56 pm

Postby Kambiz » July 25th, 2008, 2:47 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
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Postby actalk » July 25th, 2008, 4:25 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
Junior Member
 
Posts: 29
Joined: October 5th, 2007, 3:56 pm


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 10 guests

cron