Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi Everyone,

I am very new to multithreading. So my question is

How can we check if any method needs threads safety or not, Or in other words if the code is thread safe or not. For e.g

int Foo ()
{
   int i = 10;
   i++;
   cout << i <<endl;
}


Is this a thread safe? If yes why, if Not Why?

How should we check yes this function need thread safety or not?

It will be highly appreciable,If somebody can explain with few simple examples.

Thanks in advance
Posted
Updated 29-Jan-13 19:01pm
v4
Comments
skydger 30-Jan-13 1:23am    
You should make your methods thread-safe, if its use shared variables.
Please read this article http://en.wikipedia.org/wiki/Thread_safety

Thread-safe issues will come up anywhere you have the potential for two or more threads (or processes or instructions or...) that can have concurrent access to an object at the same time, and at least one of those accesses is a 'write'. In the code example you provide, there is no race condition with 'i' but there might be with the stream object that accesses cout.
 
Share this answer
 
Thread safety is an issue is a function uses shared resources (that can be anything from a variable to a file on disk). Access to those shared resources must be synchronized, for a function to be considered thread-safe. Otherwise, race conditions can appear and lead to undexpected behavior.

Now, your function is not thread safe because it uses cout, which is an object that streams output to a console. This is a shared resource, and you should sync the access to it.
 
Share this answer
 
Comments
CPallini 30-Jan-13 4:11am    
5. Don't know why someone down voted this.
Hello!

This is implementation-specific. It is possible that the cout-stream is threadsafe, ask your compiler-vendor, or read the manual. Nevertheless there is no requirement for it to be, by the standard.

For the variable "int i" it is easy. It is local to the thread starting "int Foo()", which needs to return an int btw.

Marius said the rest. Thread-Safety means that ressources being in access by multiple threads but are only allowed to be in access by one single thread at a time need to be secured.
 
Share this answer
 
Comments
Philippe Mori 4-Feb-13 21:31pm    
Even though << operator might be thread-safe in itself, output would not be deterministic if multiple thread write at the same time. Usually you would want to ensure that a whole line is written at once or something similar.
auge__ 5-Feb-13 2:18am    
A deterministic system is a system in which no randomness is involved in the development of future states of the system. It IS deterministic when multiple threads put mixed rubbish into a threadsafe cout.
Philippe Mori 5-Feb-13 8:33am    
It is determinated which characters will be outputted but if you want to ensure that each thread write on its own line you will need external synchronization.
auge__ 5-Feb-13 9:27am    
Exactly, Philippe Mori. And so it is thread-safe. What you construe about requirements like "a line in the output for each call of 'operator <<()'" was not asked for.
Thread-Safety has nothing to do with design-oddities. A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900