0
Is this thread safe
Hi Please refer code below: https://code.sololearn.com/cpijN473VCHg/?ref=app As we have atomic used, is this always thread safe or not?
4 Antworten
+ 2
I had to do some research to understand the static keyword as a modifier of local variable declaration inside a free function. But now I get it. Indeed in this case the data variable is only initialized once, so next calls of the function with work on the same piece of memory.
https://stackoverflow.com/questions/15235526/the-static-keyword-and-its-various-uses-in-c
Atomic will prevent the change of the variable by two different threads at the same time. So I think technically this is not a data race situation, but the sequence you described seems to be possible. Because the function contains 3 lines that do atomic operations individually, but their actual execution order across multiple threads can vary. So in this case it is better to use other synchronization technique, like a mutex.
+ 6
In your code, both of your test functions are completely independent and "pure", they do not interact with each other or with a shared global state. Thread safety can be meaningful only in a situation where there is interaction. For example there is a shared resource, object, variable or data structure, which is potentially modified by multiple threads at the same time. So it doesn't matter if inside the function you declare your variable as atomic.
https://www.reddit.com/r/cpp_questions/comments/150jvqf/how_to_make_c_thread_safe
Quote from here:
>> Something is "thread safe" when it can be interacted with from multiple separate threads simultaneously. C++ is thread safe, if your separate threads never use the same memory at any point.
+ 2
Thanks @Tibor Santa
First of all, My Bad. I forgot to declare data in both test1 and test2 functions as static. Updated code now.
Following confusion had make me think and raise a question.
There is no global variables so not to worry.
test1 is called once so again not to worry.
test2 is called twice and hence we should focus on it. But Again this function is not using any heap allocated data or global data. But it does have a static data. Thus, we should focus about data race.
Now , As data is atomic<int> , I thought of ignoring this with an understanding that atomic is thread safe.
Then comes my actual issue now.
Refer code below with line number to discuss in detail:
int test2()
{
static atomic<int> data;//line 1
data.store(0);//line 2
data.store(data.load()+10);//line 3
return data.load();//line 4
}
Consider below two from main function
future<int> res2 = async(std::launch::async,test2);//Let's call this as thread 1
auto res3 = async(std::launch::async,test2);//Let's call this as thread 2
Assume that thread1 started function test2 and it executed line 3 which makes data 10
Now thread 2 started and executed till line 2 which makes data as 0
Now thread 1 resumed execution and will return 0
Is this correct data race even with atomic ? Does atomic not guarantee all type of thread safety ?
+ 1
Thanks Tibor Santa