开发者

multithreading dispute on local variables

I had implemented a few methods that were being handled by individual background threads. I understand the complexity of doing things this way but when I tested the results it all seemed fine. Each thread accesses the same variables at times and there is maximum of 5 threads working at any given time and I guess I should have used synchlock but my question is whether there can be any way the threads could have been executing the processes without overwriting the variable contents. I was under the impression that each thread was allocated a site in memory for that variable and even though it is named the same, in memory it is a different location mapped with a specific thread, right? so if there were collisions you should be getting an error that it cannot access that variable if it开发者_运维知识库 were used by another thread.

Am I wrong on this?


  • If you are talking about local variables of a function - no, each thread has its own copy of those on its stack.
  • If you are talking about member variables of a class being accessed from different threads - yes, you need to protect them (unless they are read-only)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜