Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access ThreadLocal variable given a thread object

I want to access thread local variable in another thread.

e.g.

I have Thread object A for thread-1,

it has one local variable L1.

and I have another thread Thread-2, how can I access L1 in Thread-2 ? Thanks

like image 425
zjffdu Avatar asked Oct 19 '22 01:10

zjffdu


1 Answers

No because memory visibility. All the ThreadLocal infrastructure assumes that access to the threadlocal objects is thread-confined. If another thread tampers with the contents of a threadLocal object, there's no memory barrier to let the original owner thread know it needs to update its cached version of that object, and the JIT wouldn't know to take into account modifications from other threads when reasoning about what instructions it can reorder; if you update it the original thread could still be seeing a stale version with no clue a change took place.

This is in addition to the observation made by yshavit that tampering with the internals of a third-party library is a bad idea in general.

like image 168
Nathan Hughes Avatar answered Nov 02 '22 04:11

Nathan Hughes