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

前端 未结 10 1475
后悔当初
后悔当初 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:05

    As far as I know, this is exactly the usecase for a lock. Two threads which access one array concurrently must ensure that one thread is ready with its work. Thread B might read unfinished data if thread A did not finish work.

提交回复
热议问题