how multiple threads invoke singleton object's method and work on them?

前端 未结 4 886
無奈伤痛
無奈伤痛 2021-01-29 20:05

I have multiple threads running which access singleton object and call its method and pass objects in it. In the method I do some calculations only on recieved object. I have he

4条回答
  •  长情又很酷
    2021-01-29 20:36

    You can think about it in "pizza terms". You went to pizza cafe, and couple of other guys entered with you as well. Each one placed his order and then sat to his own table. Pizza boxes to be arrived then and each of you start eating his own meal.

    Here, the cafe is a singleton, pizza boys are CPU cores, you and other customers are threads, orders and pizza are input and output data respectively, table is a piece of memory.

    As you can see, each thread is served with his own piece of memory so CPU can distinguish your data and don't mix up. As you asked about stack, it's not a significant player here because each thread has it's own stack (as a part of your memory chunk).

提交回复
热议问题