digitalmars.D - synchronized { }
- Walter Bright (3/3) Jun 25 2008 Right now, if you use a synchronized statement with no argument, it will...
- Jason House (3/7) Jun 25 2008 I tried to once but the design quickly outgrew the method. I wouldn't mi...
- Walter Bright (2/12) Jun 25 2008 The idea that there isn't a legitimate use for it.
- BCS (3/19) Jun 26 2008 How about replace it by allowing any global (of any type) to be used. A ...
- Michel Fortin (8/12) Jun 26 2008 I've used it before, thinking it was equivalent to synchronize(this)
- Graham St Jack (2/13) Jun 29 2008 Same.
- Sean Kelly (3/16) Jun 30 2008 Um, it /is/ equivalent to synchronized(this). What made you think diffe...
- torhu (5/22) Jun 30 2008 Don't the docs say that they're not equivalent?
- Sean Kelly (4/26) Jun 30 2008 They're identical, unless something changed recently in D 2.0. Walter c...
- Sean Kelly (13/38) Jul 27 2008 I was finally inspired to track down the reference for this statement:
- Bruno Medeiros (8/49) Jul 30 2008 What do you mean inconsistent with Java? I checked it out now, and
- Sean Kelly (28/90) Jul 30 2008 See:
- Bruno Medeiros (13/115) Aug 04 2008 Yes, it's not useful in that case. But it might be useful in a situation...
- Jarrett Billingsley (8/10) Jun 30 2008 Uh? In the OP:
- Sean Kelly (19/29) Jun 30 2008 I'd hate to disagree with Walter since he created D, but I don't know th...
- Steven Schveighoffer (9/12) Jun 26 2008 Yeah, that seems not too useful, it only makes sense when writing functi...
- Vladimir Panteleev (11/14) Jun 26 2008 I think I used it once for:
- cemiller (13/16) Jun 26 2008 I use it on occasion. It can be useful for singleton:
- BCS (19/32) Jun 26 2008 Hmmm. Slight variation on my last suggestion, Allow synchronized to take...
- cemiller (5/36) Jun 26 2008 I could synchronized(typeid(Foo)) but I don't think D guarantees that ea...
- Steven Schveighoffer (11/53) Jun 27 2008 I think this is guaranteed as long as you are not loading D from multipl...
- torhu (8/17) Jun 27 2008 Isn't that the same as doing this?
- Steven Schveighoffer (7/23) Jun 27 2008 It's close, but in Tango, you have methods for the mutex, plus you can u...
- torhu (3/29) Jun 30 2008 Sure, but my point was just that your example doesn't do anything that
- Sean Kelly (26/43) Jun 30 2008 No. Mutexes in Tango can be set as the monitor for any object, and
- Lionello Lunesu (19/22) Jun 26 2008 Never understood that one, so: no.
- downs (21/21) Jun 27 2008 gentoo-pc ~ $ cat test50.d; echo "==="; gdc test50.d -o test50 && ./test...
- janderson (6/10) Jun 27 2008 Slightly off topic.
- Sean Kelly (5/14) Jun 30 2008 It is already effectively a library function in Tango. Tango contains m...
- Sean Kelly (4/7) Jun 29 2008 It's certainly convenient in some instances, but obviously not necessary...
Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?
Jun 25 2008
Walter Bright Wrote:Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?I tried to once but the design quickly outgrew the method. I wouldn't miss it. What inspires you to abandon or replace it?
Jun 25 2008
Jason House wrote:Walter Bright Wrote:The idea that there isn't a legitimate use for it.Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?I tried to once but the design quickly outgrew the method. I wouldn't miss it. What inspires you to abandon or replace it?
Jun 25 2008
Reply to Walter,Jason House wrote:How about replace it by allowing any global (of any type) to be used. A global mutex would need to be created in such a cases.Walter Bright Wrote:The idea that there isn't a legitimate use for it.Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?I tried to once but the design quickly outgrew the method. I wouldn't miss it. What inspires you to abandon or replace it?
Jun 26 2008
On 2008-06-25 21:18:41 -0400, Walter Bright <newshound1 digitalmars.com> said:Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?I've used it before, thinking it was equivalent to synchronize(this) {}, an incorrect assumption obviously. If you get rid of it, I won't miss it. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Jun 26 2008
On Thu, 26 Jun 2008 08:15:24 -0400, Michel Fortin wrote:On 2008-06-25 21:18:41 -0400, Walter Bright <newshound1 digitalmars.com> said:Same.Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?I've used it before, thinking it was equivalent to synchronize(this) {}, an incorrect assumption obviously. If you get rid of it, I won't miss it.
Jun 29 2008
== Quote from Graham St Jack (graham.stjack internode.on.net)'s articleOn Thu, 26 Jun 2008 08:15:24 -0400, Michel Fortin wrote:Um, it /is/ equivalent to synchronized(this). What made you think differently? SeanOn 2008-06-25 21:18:41 -0400, Walter Bright <newshound1 digitalmars.com> said:Same.Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?I've used it before, thinking it was equivalent to synchronize(this) {}, an incorrect assumption obviously. If you get rid of it, I won't miss it.
Jun 30 2008
Sean Kelly wrote:== Quote from Graham St Jack (graham.stjack internode.on.net)'s articleDon't the docs say that they're not equivalent? http://www.digitalmars.com/d/2.0/statement.html#SynchronizedStatement I thought they were the same too, before Walter checked in something to phobos that made me think otherwise. Can't remember what exactly.On Thu, 26 Jun 2008 08:15:24 -0400, Michel Fortin wrote:Um, it /is/ equivalent to synchronized(this). What made you think differently?On 2008-06-25 21:18:41 -0400, Walter Bright <newshound1 digitalmars.com> said:Same.Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?I've used it before, thinking it was equivalent to synchronize(this) {}, an incorrect assumption obviously. If you get rid of it, I won't miss it.
Jun 30 2008
== Quote from torhu (no spam.invalid)'s articleSean Kelly wrote:They're identical, unless something changed recently in D 2.0. Walter confirmed this explicitly for me a while back, though I don't have a link handy. Sean== Quote from Graham St Jack (graham.stjack internode.on.net)'s articleDon't the docs say that they're not equivalent? http://www.digitalmars.com/d/2.0/statement.html#SynchronizedStatement I thought they were the same too, before Walter checked in something to phobos that made me think otherwise. Can't remember what exactly.On Thu, 26 Jun 2008 08:15:24 -0400, Michel Fortin wrote:Um, it /is/ equivalent to synchronized(this). What made you think differently?On 2008-06-25 21:18:41 -0400, Walter Bright <newshound1 digitalmars.com> said:Same.Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?I've used it before, thinking it was equivalent to synchronize(this) {}, an incorrect assumption obviously. If you get rid of it, I won't miss it.
Jun 30 2008
== Quote from Sean Kelly (sean invisibleduck.org)'s article== Quote from torhu (no spam.invalid)'s articleI was finally inspired to track down the reference for this statement: http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D.bugs&artnum=5268 http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D.bugs&artnum=5276 The second post from Walter is a reply to my post in the first link. Given this, I can only assume that Walter either misunderstood me or that the behavior was changed silently at some point--I'm pretty sure the latter since I tested this way back when. In any case, the current behavior is as Walter described, and I consider it utterly broken. Not only is it inconsistent with Java, which is what I believe this design was based on, but it's completely useless in the general case. It has to change, but I really wish that the behavior could be fixed in D 1.0 as well. Not likely I know, but oh well. SeanSean Kelly wrote:They're identical, unless something changed recently in D 2.0. Walter confirmed this explicitly for me a while back, though I don't have a link handy.== Quote from Graham St Jack (graham.stjack internode.on.net)'s articleDon't the docs say that they're not equivalent? http://www.digitalmars.com/d/2.0/statement.html#SynchronizedStatement I thought they were the same too, before Walter checked in something to phobos that made me think otherwise. Can't remember what exactly.On Thu, 26 Jun 2008 08:15:24 -0400, Michel Fortin wrote:Um, it /is/ equivalent to synchronized(this). What made you think differently?On 2008-06-25 21:18:41 -0400, Walter Bright <newshound1 digitalmars.com> said:Same.Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?I've used it before, thinking it was equivalent to synchronize(this) {}, an incorrect assumption obviously. If you get rid of it, I won't miss it.
Jul 27 2008
Sean Kelly wrote:== Quote from Sean Kelly (sean invisibleduck.org)'s articleWhat do you mean inconsistent with Java? I checked it out now, and apparently Java does not have a synchronized statement with no arguments Why is the current behavior broken? -- Bruno Medeiros - Software Developer, MSc. in CS/E graduate http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D== Quote from torhu (no spam.invalid)'s articleI was finally inspired to track down the reference for this statement: http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D.bugs&artnum=5268 http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D.bugs&artnum=5276 The second post from Walter is a reply to my post in the first link. Given this, I can only assume that Walter either misunderstood me or that the behavior was changed silently at some point--I'm pretty sure the latter since I tested this way back when. In any case, the current behavior is as Walter described, and I consider it utterly broken. Not only is it inconsistent with Java, which is what I believe this design was based on, but it's completely useless in the general case. It has to change, but I really wish that the behavior could be fixed in D 1.0 as well. Not likely I know, but oh well. SeanSean Kelly wrote:They're identical, unless something changed recently in D 2.0. Walter confirmed this explicitly for me a while back, though I don't have a link handy.== Quote from Graham St Jack (graham.stjack internode.on.net)'s articleDon't the docs say that they're not equivalent? http://www.digitalmars.com/d/2.0/statement.html#SynchronizedStatement I thought they were the same too, before Walter checked in something to phobos that made me think otherwise. Can't remember what exactly.On Thu, 26 Jun 2008 08:15:24 -0400, Michel Fortin wrote:Um, it /is/ equivalent to synchronized(this). What made you think differently?On 2008-06-25 21:18:41 -0400, Walter Bright <newshound1 digitalmars.com> said:Same.Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?I've used it before, thinking it was equivalent to synchronize(this) {}, an incorrect assumption obviously. If you get rid of it, I won't miss it.
Jul 30 2008
Bruno Medeiros wrote:Sean Kelly wrote:See: I would consider a synchronized statement with no arguments within a class member function to synchronize on the same monitor as a synchronized function in Java. I believe Walter has even said in the past that 'synchronized' with no arguments is the D equivalent of the synchronized function label in Java.== Quote from Sean Kelly (sean invisibleduck.org)'s articleWhat do you mean inconsistent with Java? I checked it out now, and apparently Java does not have a synchronized statement with no arguments== Quote from torhu (no spam.invalid)'s articleI was finally inspired to track down the reference for this statement: http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmar .D.bugs&artnum=5268 http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmar .D.bugs&artnum=5276 The second post from Walter is a reply to my post in the first link. Given this, I can only assume that Walter either misunderstood me or that the behavior was changed silently at some point--I'm pretty sure the latter since I tested this way back when. In any case, the current behavior is as Walter described, and I consider it utterly broken. Not only is it inconsistent with Java, which is what I believe this design was based on, but it's completely useless in the general case. It has to change, but I really wish that the behavior could be fixed in D 1.0 as well. Not likely I know, but oh well. SeanSean Kelly wrote:They're identical, unless something changed recently in D 2.0. Walter confirmed this explicitly for me a while back, though I don't have a link handy.== Quote from Graham St Jack (graham.stjack internode.on.net)'s articleDon't the docs say that they're not equivalent? http://www.digitalmars.com/d/2.0/statement.html#SynchronizedStatement I thought they were the same too, before Walter checked in something to phobos that made me think otherwise. Can't remember what exactly.On Thu, 26 Jun 2008 08:15:24 -0400, Michel Fortin wrote:Um, it /is/ equivalent to synchronized(this). What made you think differently?On 2008-06-25 21:18:41 -0400, Walter Bright <newshound1 digitalmars.com> said:Same.Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?I've used it before, thinking it was equivalent to synchronize(this) {}, an incorrect assumption obviously. If you get rid of it, I won't miss it.Why is the current behavior broken?I'm stating that based on Walter's description above where such blocks will "sync on a mutex unique to that statement." Obviously, the whole point of synchronization is coordinating the efforts of multiple concurrent threads. However, in my experience it's unheard of to have a single function be the means with which shared data is accessed. And not even a single function in this case, but a single statement. For example, let's say that we have a linked list that needs synchronization because we're reading from it and writing to it concurrently. How often would both the read and write operations be done in the exact same synchronized statement in code? Right, never. One could argue that a synchronized statement with no supplied expression should simply always synchronize on the global monitor, since that monitor can't be supplied explicitly. However, I would argue that the correct behavior is to default to the immediate context surrounding the function. So a class member function would sync on the class instance, a static class function would sync on the static classinfo, and a global function would sync on the global monitor. Simple, straightforward, and it corresponds to the most common use cases for synchronization. Sean
Jul 30 2008
Sean Kelly wrote:Bruno Medeiros wrote:Yes, it's not useful in that case. But it might be useful in a situation where the synchronized statement is the only statement/block with access to a certain shared global data. For example, what about cemiller's use of the synchronized statement for singleton initialization (see his OP)?Sean Kelly wrote:See: I would consider a synchronized statement with no arguments within a class member function to synchronize on the same monitor as a synchronized function in Java. I believe Walter has even said in the past that 'synchronized' with no arguments is the D equivalent of the synchronized function label in Java.== Quote from Sean Kelly (sean invisibleduck.org)'s articleWhat do you mean inconsistent with Java? I checked it out now, and apparently Java does not have a synchronized statement with no arguments== Quote from torhu (no spam.invalid)'s articleI was finally inspired to track down the reference for this statement: http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmar .D.bugs&artnum=5268 http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmar .D.bugs&artnum=5276 The second post from Walter is a reply to my post in the first link. Given this, I can only assume that Walter either misunderstood me or that the behavior was changed silently at some point--I'm pretty sure the latter since I tested this way back when. In any case, the current behavior is as Walter described, and I consider it utterly broken. Not only is it inconsistent with Java, which is what I believe this design was based on, but it's completely useless in the general case. It has to change, but I really wish that the behavior could be fixed in D 1.0 as well. Not likely I know, but oh well. SeanSean Kelly wrote:They're identical, unless something changed recently in D 2.0. Walter confirmed this explicitly for me a while back, though I don't have a link handy.== Quote from Graham St Jack (graham.stjack internode.on.net)'s articleDon't the docs say that they're not equivalent? http://www.digitalmars.com/d/2.0/statement.html#SynchronizedStatement I thought they were the same too, before Walter checked in something to phobos that made me think otherwise. Can't remember what exactly.On Thu, 26 Jun 2008 08:15:24 -0400, Michel Fortin wrote:Um, it /is/ equivalent to synchronized(this). What made you think differently?On 2008-06-25 21:18:41 -0400, Walter Bright <newshound1 digitalmars.com> said:Same.Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?I've used it before, thinking it was equivalent to synchronize(this) {}, an incorrect assumption obviously. If you get rid of it, I won't miss it.Why is the current behavior broken?I'm stating that based on Walter's description above where such blocks will "sync on a mutex unique to that statement." Obviously, the whole point of synchronization is coordinating the efforts of multiple concurrent threads. However, in my experience it's unheard of to have a single function be the means with which shared data is accessed. And not even a single function in this case, but a single statement. For example, let's say that we have a linked list that needs synchronization because we're reading from it and writing to it concurrently. How often would both the read and write operations be done in the exact same synchronized statement in code? Right, never.One could argue that a synchronized statement with no supplied expression should simply always synchronize on the global monitor, since that monitor can't be supplied explicitly. However, I would argue that the correct behavior is to default to the immediate context surrounding the function. So a class member function would sync on the class instance, a static class function would sync on the static classinfo, and a global function would sync on the global monitor. Simple, straightforward, and it corresponds to the most common use cases for synchronization. SeanWhatever the case, you do agree it's not a major problem what the no-args synchronized statement does, since it is only syntactic sugar for the with-args version? (Like I said, Java doesn't even have a no-args synchronized statement) -- Bruno Medeiros - Software Developer, MSc. in CS/E graduate http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Aug 04 2008
"Sean Kelly" <sean invisibleduck.org> wrote in message news:g4bhn1$ufo$1 digitalmars.com...Um, it /is/ equivalent to synchronized(this). What made you think differently?Uh? In the OP: ----- Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. ----- Doesn't seem like synchronized(this) to me.
Jun 30 2008
== Quote from Jarrett Billingsley (kb3ctd2 yahoo.com)'s article"Sean Kelly" <sean invisibleduck.org> wrote in message news:g4bhn1$ufo$1 digitalmars.com...I'd hate to disagree with Walter since he created D, but I don't know that there's even a means of doing this in the runtime right now. Storage is reserved for a single global monitor which is shared by all free functions and probably all functions in structs as well since there's no room to store a per-instance monitor for them (I never bothered to verify the behavior with structs, so I'm making an educated guess). Also, each object instance gets its own monitor which is shared by all non-static class member functions. Finally, static class member functions use the monitor on the TypeInfo object for that class. This aside, synchronizing in the manner implied by Walter's remark would be largely useless anyway, since it's quite common for multiple functions to share access to some resource and all such functions must synchronize on the same mutex. Better to be safe and synchronize everything on a single global mutex that try for performance and end up using multiple unrelated mutexes for the same operation. If this is truly the way that Phobos 2.0 works, well... I suppose that's one more reason I'd be glad I'm using Tango. SeanUm, it /is/ equivalent to synchronized(this). What made you think differently?Uh? In the OP: ----- Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. ----- Doesn't seem like synchronized(this) to me.
Jun 30 2008
"Walter Bright" wroteRight now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?Yeah, that seems not too useful, it only makes sense when writing functions that use global data. In that case, it makes more sense to synchronize on the data instead of the code that accesses the data. Otherwise, if you eventually have to access the data through other code, then you have to switch to synchronizing on the data anyways. And if you forget to switch, then you now have a subtle bug :) Better to force coders to think about why they are synchronizing and lock the appropriate object. -Steve
Jun 26 2008
On Thu, 26 Jun 2008 04:18:41 +0300, Walter Bright <newshound1 digitalmars.com> wrote:Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?I think I used it once for: void write_synced(string s) { synchronized writef("%s", s); } or maybe it was some other external resource not linked to a class. -- Best regards, Vladimir mailto:thecybershadow gmail.com
Jun 26 2008
I use it on occasion. It can be useful for singleton: if(!foo) { synchronized { if(!foo) foo = new Foo(); } } return foo; especially useful because you don't need an object instance. On Wed, 25 Jun 2008 18:18:41 -0700, Walter Bright <newshound1 digitalmars.com> wrote:Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?
Jun 26 2008
Reply to cemiller,I use it on occasion. It can be useful for singleton: if(!foo) { synchronized { if(!foo) foo = new Foo(); } } return foo; especially useful because you don't need an object instance.Hmmm. Slight variation on my last suggestion, Allow synchronized to take any global symbol as the mutex if(!foo) { synchronized(Foo) // global mutex on Foo { if(!foo) foo = new Foo(); } } Or if you need more resolution: class Foo { alias void single; } ... synchronized(Foo.single) // global mutex on Foo.single ...
Jun 26 2008
On Thu, 26 Jun 2008 16:42:32 -0700, BCS <ao pathlink.com> wrote:Reply to cemiller,I could synchronized(typeid(Foo)) but I don't think D guarantees that each type has its own instance and monitor. This could potentially be a substitute; create a type and lock on it, no runtime allocation, not many language changes needed.I use it on occasion. It can be useful for singleton: if(!foo) { synchronized { if(!foo) foo = new Foo(); } } return foo; especially useful because you don't need an object instance.Hmmm. Slight variation on my last suggestion, Allow synchronized to take any global symbol as the mutex if(!foo) { synchronized(Foo) // global mutex on Foo { if(!foo) foo = new Foo(); } } Or if you need more resolution: class Foo { alias void single; } ... synchronized(Foo.single) // global mutex on Foo.single ...
Jun 26 2008
"cemiller" wroteOn Thu, 26 Jun 2008 16:42:32 -0700, BCS wrote:I think this is guaranteed as long as you are not loading D from multiple DLLs. In any case, Tango avoids needing this statement by making mutexes fully accessible objects. If you need a mutex without having it attached to an object, it's easy: Mutex m = new Mutex; synchronized(m) { } -SteveReply to cemiller,I could synchronized(typeid(Foo)) but I don't think D guarantees that each type has its own instance and monitor. This could potentially be a substitute; create a type and lock on it, no runtime allocation, not many language changes needed.I use it on occasion. It can be useful for singleton: if(!foo) { synchronized { if(!foo) foo = new Foo(); } } return foo; especially useful because you don't need an object instance.Hmmm. Slight variation on my last suggestion, Allow synchronized to take any global symbol as the mutex if(!foo) { synchronized(Foo) // global mutex on Foo { if(!foo) foo = new Foo(); } } Or if you need more resolution: class Foo { alias void single; } ... synchronized(Foo.single) // global mutex on Foo.single ...
Jun 27 2008
Steven Schveighoffer wrote:In any case, Tango avoids needing this statement by making mutexes fully accessible objects. If you need a mutex without having it attached to an object, it's easy: Mutex m = new Mutex; synchronized(m) { }Isn't that the same as doing this? --- Object m = new Object; synchronized(m) { } ---
Jun 27 2008
"torhu" wroteSteven Schveighoffer wrote:It's close, but in Tango, you have methods for the mutex, plus you can use them with conditions. Plus, it's not as hackish :) It would be nice if the language supported conditions based on the Monitor object by default. -SteveIn any case, Tango avoids needing this statement by making mutexes fully accessible objects. If you need a mutex without having it attached to an object, it's easy: Mutex m = new Mutex; synchronized(m) { }Isn't that the same as doing this? --- Object m = new Object; synchronized(m) { }
Jun 27 2008
Steven Schveighoffer wrote:"torhu" wroteSure, but my point was just that your example doesn't do anything that my example doesn't.Steven Schveighoffer wrote:It's close, but in Tango, you have methods for the mutex, plus you can use them with conditions. Plus, it's not as hackish :)In any case, Tango avoids needing this statement by making mutexes fully accessible objects. If you need a mutex without having it attached to an object, it's easy: Mutex m = new Mutex; synchronized(m) { }Isn't that the same as doing this? --- Object m = new Object; synchronized(m) { }
Jun 30 2008
== Quote from torhu (no spam.invalid)'s articleSteven Schveighoffer wrote:No. Mutexes in Tango can be set as the monitor for any object, and are in fact their own monitors. So synchronized(m) actually locks the Mutex, not a separate monitor object associated with the Mutex object. You can just as easily do this: class C { this(Mutex x) { m = x; setMonitor(this, m); } Mutex m; void fn() { // the following are all equivalent synchronized {} synchronized(this) {} synchronized(m) {} } } Ore read-write mutexes: ReadWriteMutex m; synchronized(m.reader) {} synchronized(m.writer) {} SeanIn any case, Tango avoids needing this statement by making mutexes fully accessible objects. If you need a mutex without having it attached to an object, it's easy: Mutex m = new Mutex; synchronized(m) { }Isn't that the same as doing this? --- Object m = new Object; synchronized(m) { } ---
Jun 30 2008
"Walter Bright" <newshound1 digitalmars.com> wrote in message news:g3uqph$2s3n$1 digitalmars.com...Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?Never understood that one, so: no. But I do think there's a place for synchronized { }: memory barrier. IIRC a mutex always implies a memory barrier anyway. synchronized without argument could only create a memory barrier. This could then be used to achieve what the Interlocked* functions in Win32 do. It's such a basic operation that I think it warrants a language construct. static int b;//some global int a;//local // native cross-platform InterlockedIncrement synchronized { a = b++; } I should note however that I'm currently having a hard-time implementing some multi-threaded algorithms.. I probably don't know enough about MT to be messing with it :S L.
Jun 26 2008
gentoo-pc ~ $ cat test50.d; echo "==="; gdc test50.d -o test50 && ./test5 0 import std.stdio; template _SyncObject(string KEY) { Object obj; static this() { obj = new Object; } } template SyncObject(string KEY) { alias _SyncObject!(KEY).obj SyncObject; } void main() { writefln(SyncObject!("main_1") is SyncObject!("main_1")); writefln(SyncObject!("main_1") !is SyncObject!("main_2")); synchronized (SyncObject!("main_1")) { writefln("Test"); } } === true true Test
Jun 27 2008
Walter Bright wrote:Right now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?Slightly off topic. Just a thought. If we had the ability to do block operators, or turn functions with delegates into blocks, or any other incarnation we could have this as a library function. -Joel
Jun 27 2008
== Quote from janderson (askme me.com)'s articleWalter Bright wrote:It is already effectively a library function in Tango. Tango contains mutexes which may operate as object monitors and there is no real code dependency for this between the runtime and library code. SeanRight now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?Slightly off topic. Just a thought. If we had the ability to do block operators, or turn functions with delegates into blocks, or any other incarnation we could have this as a library function.
Jun 30 2008
== Quote from Walter Bright (newshound1 digitalmars.com)'s articleRight now, if you use a synchronized statement with no argument, it will sync on a mutex unique to that statement. Does anyone write threading code that depends on this behavior?It's certainly convenient in some instances, but obviously not necessary. Still, I find the current behavior to be easily understandable. Sean
Jun 29 2008