Thread safety with heap-allocated memory

前端 未结 3 1932
自闭症患者
自闭症患者 2021-02-05 17:46

I was reading this: http://en.wikipedia.org/wiki/Thread_safety

Is the following function thread-safe?

void foo(int y){
    int * x = new int[50];
    /*...         


        
3条回答
  •  感情败类
    2021-02-05 18:27

    new and delete may or may not be thread safe. They probably are, but that is implementation-dependent. See: C++ new operator thread safety in linux and gcc 4

    To be thread safe, a function must either use stack variables or synchronize its access to other resources with other threads. As long as separate calls to new allocate different space on the heap when called from different threads, you should be fine.

提交回复
热议问题