Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Threading
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 29-Jan-13 0:36am
Edited 29-Jan-13 20:01pm
v4
Comments
skydger at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
CPallini at 30-Jan-13 4:11am
   
5. Don't know why someone down voted this.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Philippe Mori at 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__ at 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 at 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__ at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.150302.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100