Thread safety object - static or not?

后端 未结 6 2007
故里飘歌
故里飘歌 2021-01-11 15:09

I was recently in an interview and the tech guy asked me about how to make an application thread-safe.

Well, after explaining the lock() correctly, he s

6条回答
  •  無奈伤痛
    2021-01-11 15:26

    If a lock object should be static or not depends on the object you want to lock. If you want to lock an instance of a class you cannot use a static lock object. If you want to lock static data you cannot use an instance lock object. So there seems not to be any choice.

    You could think about using a static or an instance lock object to lock the access to instance data, but this results in different behaviors. With an instance lock object you lock only an instance while an static lock object will lock all instances. So no choice for performance tuning here, too.

提交回复
热议问题