其他线程可以修改线程本地内存吗? [英] Can other threads modify thread-local memory?

查看:49
本文介绍了其他线程可以修改线程本地内存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下声明:

thread_local std::atomic<void*> local_var;

是否允许其他线程对 local_var 进行修改?也就是说,如果我将 local_var 的地址传达给另一个线程,会不会从那里修改 local_var 来召唤鼻恶魔?

Are modifications of local_var from other threads allowed? That is, if I communicate the address of the local_var to another thread, won't modifying local_var from there summon the nasal demons?

推荐答案

是的,允许修改另一个线程的线程局部变量.

Yes, it is allowed to modify another thread's thread-local variables.

thread_local 对变量的含义是 name 引用每个线程中的不同对象,但这并不意味着该对象是其线程的私有对象.如果您通过变量的名称(即地址)以外的其他名称来知道该变量,则其他线程可以访问该变量.

What thread_local means for a variable is that the name refers to a different object in each thread, but it doesn't mean the object is private to its thread. If you make the variable known by something other than its name, i.e. its address, then other threads can access it.

这篇关于其他线程可以修改线程本地内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆