Is it necessary to lock an array that is *only written to* from one thread and *only read from* another?

前端 未结 10 1491
后悔当初
后悔当初 2020-12-13 19:26

I have two threads running. They share an array. One of the threads adds new elements to the array (and removes them) and the other uses this array (read operations only).

10条回答
  •  眼角桃花
    2020-12-13 20:14

    "Classical" POSIX would indeed need a lock for such a situation, but this is overkill. You just have to ensure that the reads and writes are atomic. C and C++ have that in the language since their 2011 versions of their standards. Compilers start to implement it, at least the latest versions of Clang and GCC have it.

提交回复
热议问题