What is a thread safe object?

2b_or_not_2bxx used Ask the Experts™
What would make an object thread safe?

Can someone give me an example of a thread safe object?

Please give details.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
an object that is shared by multiple threads and is guarded by simulataneous access from different threads in a multi-threaded environment is thread safe.

Speciifcally its the data for the object that needs to be guarded. guarded here means that the multiple simultanous access from different threads leave the object in a consistent state.


Can you please give an example.

I need to get a mental picture of this, an so far I just don't see.
lets say you have a class that has the property
 int mynumber;
it can be accessed by
int getmynumber(void);
void setmynumber(int a);
threads work kind of parallel.
we have to threads in our application,
and both can access the object.
consider: the first one calls getmynumber to receive the value of the mynumber property around the sametime the second thread calls setmynumber to set it to a whole other value.
what does the first one get returned? either the value of mynumber before or after it was changed.
this is a simple example. but what if the property of the object was an array of 1024 byte?
then we got the third possibility, that the return value is somewhat inbetween, maybe the second thread is about to set it and has already 512 byte done when the first one receives it's result.
this is what can happen in a thread unsafe object.
a threadsave objects prohibits parallel access to its (property changing) methods by serializing calls.
remember, threads don't run parallel in reality, they're switched, so one procedure is always begun slightly before the other.
there synchronisation kicks in, which is used to serialize calls.
though i'm a windows-guy, i explain it from the windows view.
in windows you got a lot of objects that manage synchronisation, such as events, mutexes, critical_sections and semathores.
for out example, an event would be proper.
a event is kind of a boolean managed by the system.
you can create one by the CreateEvent api. there you can specify it's behaviour, lets assume our event is auto-reset and set at creation.

the setmynumber and getmynumber methods would look like

int cfoo::getmynumber(void)
int x;
waitforsingleobject(myevent, infinite);
x = mynumber;
return x;

void cfoo:setmynumber(int a)
waitforsingleobject(myevent, infinite);
mynumber = a;

with waitforsingleobject we wait until the event is set. because it's autoreset, at the same time waitforsingleobject returns, it resets myevent.
now it is safe to access member data.
after we're done with member data, we set the event again.
so it's secured that only one thread can access the memberdata at a time, because how many threads ever are waiting for the event, only one waitforsingleobject returns at a time because its autoreset.
now the object thread safe
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.


don't make it more complex then it is. Here some pseudo code.

You have two threads.
Both access the same string via a function "foo" to append something to it.

string s;

foo( string arg )
  s += arg;

now it can happen that these threads try to run foo at the same time, which results at two foo functions to overwrite some chars in the string s.

if foo wants to be thread save it must restrict access to the critical section where arg gets appended.

mutex bar;

foo( string arg )
  lock( bar );
  s += arg;
  unlock( bar );

lock essentialy puts the thread that arrives there into sleep mode if the mutex bar is already locked. The first thread in the line of the sleeping ones is awaked and proceedes to lock the area again until he's finished.

Thus thread safety refers to the ability to keep data integrity in a multi threaded envoirment. This is archieved by facilities like mutex and other.

thats the same like what i said,
(just with a simpler example ;-))

I know. But I put it in a simpler example, with simpler words, PLUS, my formatting is not as ugly as yours ( I got a headache looking at your lines ). Superfly ;)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial