c# - Thread Synchronisation : Threads having their own copy of string type lock -
recently came across code following:
void callthisindifferentthreads(return return) { var lock = "lock"; lock(lock) { //do here. } }
my first reaction lock in code won't work because creating lock , using in same method. every thread calling method has it's own copy of lock there no synchronisation.
but later realized should work because string goes string pool , there 1 instance of particular string. i'm not sure if i'm correct.
locking on strings super bad. don't it. have no guarantee other clever soul not lock on strings, , because "super" global because of string interning, moment becomes accepted practice, wheels fall off.
lock private object has 1 purpose... locking. strings don't fit description.
Comments
Post a Comment