tag:blogger.com,1999:blog-8070878148487530022.post1537334322646942387..comments2023-10-03T09:25:15.120-04:00Comments on We will rule the galaxy together: Locking Objects And Base TypesGreg Finzerhttp://www.blogger.com/profile/12920629780529451188noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-8070878148487530022.post-70992513600213727662008-04-29T11:43:00.000-04:002008-04-29T11:43:00.000-04:00NEVER lock strings. Strings are interned, i.e. str...<B>NEVER</B> lock strings. Strings are interned, i.e. string a = "abcd"; string b = "abcd"; are the same underlying object. So by locking string a, in effect string b is also locked. <BR/><BR/>Interned strings span application domains, so the havoc one can do by locking strings can be quite extensive.<BR/><BR/>Check out this article for more locking gotchas to watch out for.<BR/><BR/>http://www.deez.info/sengelha/2006/05/25/csharp-locking-rulesSean Bibbeyhttps://www.blogger.com/profile/00636683273060813734noreply@blogger.comtag:blogger.com,1999:blog-8070878148487530022.post-66562899108605952142008-01-28T22:00:00.000-05:002008-01-28T22:00:00.000-05:00Never, EVER lock "this." Bad juju. See Phil Haac...Never, EVER lock "this." Bad juju. See Phil Haack's article at http://haacked.com/archive/2005/04/12/neverlockthis.aspx.Jim Holmeshttps://www.blogger.com/profile/05869146736565695900noreply@blogger.comtag:blogger.com,1999:blog-8070878148487530022.post-43180387168930561112008-01-28T12:31:00.000-05:002008-01-28T12:31:00.000-05:00What would be the syntax for locking an integer th...What would be the syntax for locking an integer then? When compiling using the lock on an integer, I get the error "int is not a reference type as required by the lock statement"Greg Finzerhttps://www.blogger.com/profile/12920629780529451188noreply@blogger.comtag:blogger.com,1999:blog-8070878148487530022.post-69808360671926184682008-01-28T11:32:00.000-05:002008-01-28T11:32:00.000-05:00The lock statement doesn't just take objects; it a...The lock statement doesn't just take objects; it also takes types. A very common method is to just lock "this".<BR/><BR/>You can also create an object that is used only for locking, which is useful as well if you want to lock on more than one object in a single class or throughout multiple classes. But, really, how often does this come up?<BR/><BR/>Also, you might want to take a look at the volatile keyword for accessing values inside of thread.Matt Castohttps://www.blogger.com/profile/10228422353049379964noreply@blogger.com