如何使C中的胎面本地存储在C中

发布于 2025-02-07 07:55:36 字数 474 浏览 1 评论 0原文

static __thread vars* ptr=NULL;
void thread_init(vars* new_vars){
    if(new_vars){
        ptr=new_vars
    }
    printf("%p",ptr);
}

__ thread是GCC的扩展

线程1是主线程的一个线程

1_CHILD是thread1 thread1

call thread_init(a)in main // ptr是a,而不是null

call thread_init(b)thread1 // prthe1 // ptr中的ptr是b,而不是null

call thread_init(null)(null)(null)在thread1_child // ptr为null,而不是b

如何动态初始化 (thread1 == thread1_child的ptr)?

static __thread vars* ptr=NULL;
void thread_init(vars* new_vars){
    if(new_vars){
        ptr=new_vars
    }
    printf("%p",ptr);
}

__thread is GCC's extention

thread1 is one thread of main

thread1_child is child of thread1

call thread_init(a) in main //ptr is a, not NULL

call thread_init(b) in thread1 //ptr is b, not NULL

call thread_init(NULL) in thread1_child //ptr is NULL,not b

how to dynamically initialize
(ptr of thread1 == ptr of thread1_child) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文