How to define thread-local local static variables?

前端 未结 5 437
执笔经年
执笔经年 2020-12-09 16:48

How to define local static variables (that keeps its value between function calls) that are not shared among different threads?

I am looking for an answer both in C

5条回答
  •  时光说笑
    2020-12-09 17:24

    Just use static and __thread in your function.

    Example:

    int test(void)
    {
            static __thread a;
    
            return a++;
    }
    

提交回复
热议问题