Click here to Skip to main content
15,921,716 members
Home / Discussions / C#
   

C#

 
GeneralRe: Change text of messageBox buttons Pin
Pete O'Hanlon22-Mar-08 9:59
mvePete O'Hanlon22-Mar-08 9:59 
GeneralRe: Change text of messageBox buttons Pin
CPallini23-Mar-08 0:41
mveCPallini23-Mar-08 0:41 
GeneralRe: Change text of messageBox buttons Pin
pcaeiro23-Mar-08 1:42
pcaeiro23-Mar-08 1:42 
Questioncombo box moving and adding items. Pin
steve_rm22-Mar-08 7:15
steve_rm22-Mar-08 7:15 
QuestionSearching System Drive for a file Pin
md.mostafiz22-Mar-08 4:08
md.mostafiz22-Mar-08 4:08 
GeneralRe: Searching System Drive for a file Pin
Colin Angus Mackay22-Mar-08 4:18
Colin Angus Mackay22-Mar-08 4:18 
GeneralRe: Searching System Drive for a file Pin
MNFlyer22-Mar-08 4:19
MNFlyer22-Mar-08 4:19 
Generalusing this as synchronization object Pin
George_George22-Mar-08 3:52
George_George22-Mar-08 3:52 
Hello everyone,


As mentioned in this article, why using this or type itself as lock object is bad idea? What means "it potentially offers public scope to the synchronization object"?

http://www.albahari.com/threading/part2.html

I quote the whole paragraph below,

--------------------
Choosing the Synchronization Object
Any object visible to each of the partaking threads can be used as a synchronizing object, subject to one hard rule: it must be a reference type. It’s also highly recommended that the synchronizing object be privately scoped to the class (i.e. a private instance field) to prevent an unintentional interaction from external code locking the same object. Subject to these rules, the synchronizing object can double as the object it's protecting, such as with the list field below:

class ThreadSafe {
  List <string> list = new List <string>();
 
  void Test() {
    lock (list) {
      list.Add ("Item 1");
      ...


A dedicated field is commonly used (such as locker, in the example prior), because it allows precise control over the scope and granularity of the lock. Using the object or type itself as a synchronization object, i.e.:

lock (this) { ... }
or:

lock (typeof (Widget)) { ... }    // For protecting access to statics


is discouraged because it potentially offers public scope to the synchronization object.
--------------------


thanks in advance,
George
GeneralRe: using this as synchronization object [modified] Pin
girm22-Mar-08 4:01
girm22-Mar-08 4:01 
GeneralRe: using this as synchronization object Pin
George_George22-Mar-08 4:03
George_George22-Mar-08 4:03 
GeneralRe: using this as synchronization object Pin
Colin Angus Mackay22-Mar-08 4:09
Colin Angus Mackay22-Mar-08 4:09 
GeneralRe: using this as synchronization object Pin
George_George23-Mar-08 2:53
George_George23-Mar-08 2:53 
GeneralRe: using this as synchronization object Pin
girm22-Mar-08 4:09
girm22-Mar-08 4:09 
GeneralRe: using this as synchronization object Pin
George_George23-Mar-08 2:54
George_George23-Mar-08 2:54 
GeneralRe: using this as synchronization object Pin
girm24-Mar-08 8:19
girm24-Mar-08 8:19 
GeneralRe: using this as synchronization object Pin
George_George24-Mar-08 19:01
George_George24-Mar-08 19:01 
GeneralRe: using this as synchronization object Pin
Colin Angus Mackay22-Mar-08 4:06
Colin Angus Mackay22-Mar-08 4:06 
GeneralRe: using this as synchronization object Pin
George_George23-Mar-08 2:51
George_George23-Mar-08 2:51 
GeneralRe: using this as synchronization object Pin
Colin Angus Mackay23-Mar-08 4:06
Colin Angus Mackay23-Mar-08 4:06 
GeneralRe: using this as synchronization object Pin
George_George23-Mar-08 4:17
George_George23-Mar-08 4:17 
QuestionHow do I create a subclass dynamically from a base class? [modified] Pin
loderxp22-Mar-08 2:57
loderxp22-Mar-08 2:57 
AnswerRe: How do I create a subclass dynamically from a base class? Pin
pmarfleet22-Mar-08 3:41
pmarfleet22-Mar-08 3:41 
Questionhow to search particular word Pin
sugunavathysubramanian22-Mar-08 1:04
sugunavathysubramanian22-Mar-08 1:04 
AnswerRe: how to search particular word Pin
Christian Graus22-Mar-08 11:43
protectorChristian Graus22-Mar-08 11:43 
GeneralRe: how to search particular word Pin
sugunavathysubramanian24-Mar-08 20:24
sugunavathysubramanian24-Mar-08 20:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.