0

Thread local : global vs local

Hi What's the difference when a thread local variable is global and inside a function? I don't see any output difference between two from below code: https://sololearn.com/compiler-playground/cp94edD0I8DL/?ref=app

22nd Feb 2025, 3:56 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 odpowiedzi
+ 1
The core issue isn't whether the thread_local variable is declared globally or within the function. The key difference lies in how the threads are used and how many times the function containing the thread_local variable is called within each thread.
22nd Feb 2025, 6:55 PM
BroFar
BroFar - avatar
0
Based on your code, the output will be the same but here is a modification code that demonstrates the difference between the two a bit better imo https://sololearn.com/compiler-playground/c6vmus4uLT0g/?ref=app
22nd Feb 2025, 4:54 PM
BroFar
BroFar - avatar
0
What changes you did BroFar ? I still see the same output practically. You gave different tasks to both functions and hence output is different. I feel thread local as global vs local does not make any difference. To prove my point: Run 1 : Let's not have vec1 and only have vec2 related code as it is ( I mean vec2 with test2 function) Run2: Let's again not call vec1 and only have vec2 related call making call to test1 function. Both output does not change. In meaning, vec2 is only executed with test1 (global thread local ) and test2 ( functional thread local) producing same result Am I missing something 😕?
22nd Feb 2025, 6:15 PM
Ketan Lalcheta
Ketan Lalcheta - avatar