Click here to Skip to main content
Sign Up to vote bad
good
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 28 Jan '13 - 23:36
Edited 29 Jan '13 - 19:01

Comments
skydger - 30 Jan '13 - 1:23
You should make your methods thread-safe, if its use shared variables. Please read this article http://en.wikipedia.org/wiki/Thread_safety

3 solutions

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  
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 - 30 Jan '13 - 4:11
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.
  Permalink  
Comments
Philippe Mori - 4 Feb '13 - 21:31
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:18
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:33
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:27
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 355
1 OriginalGriff 325
2 Arun Vasu 268
3 CPallini 213
4 Tadit Dash 201
0 Sergey Alexandrovich Kryukov 10,005
1 OriginalGriff 7,654
2 CPallini 4,171
3 Rohan Leuva 3,447
4 Maciej Los 2,974


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid