January 22, 2013
Second try at a translation of the third Go version. It seems to work correctly (comments welcome), but it seems slower:

http://codepad.org/y1LnjLl0

Bye,
bearophile
January 22, 2013
On Tuesday, 22 January 2013 at 09:47:25 UTC, monarch_dodra wrote:
> Avoids deadlock.
>
> imagine 2 threads:
> a: from 2 to 5.
> b: from 5 to 2.
>
> If both threads acquire their first lock, then you have a dead lock, and your program is basically dead.
>
> By always locking low first, you avoid the deadlock in a rather simple but elegant way.

Ah neat. And what about the case from = to? Why doesn' that deadlock in this code? (Concurrency is rather new to me)
January 22, 2013
On Monday, 21 January 2013 at 20:35:16 UTC, bearophile wrote:
> qznc:
>
>> Code: http://dpaste.dzfl.pl/e6615a53
>>
>> Any comments or improvements?
>
> I have reformatted your code a little, according to the style used in all other D entries of RosettaCode:
>
> http://codepad.org/ceDyQ8lE
>
> The usage of a sink for toString() isn't common, but it's OK.
>
> I suggest to add something to make it stop after 20 seconds or so (I run all the rosettacode entries every so often to test them, so I prefer them to not go on forever. Some entries are required to produce infinite loops, but I think this time it's not necessary).
>
>
>> I actually implemented a more scalable version than most of the other languages used, by using a lock per item instead of a single lock for the whole array.
>
> Then I suggest you to add this comment before the entry.
>
> Bye,
> bearophile

I note you always seem to use "in" in your functions and on reddit seemed to imply that this was the idiomatic way of using D yet I recall Jonathan M Davies posting that using "in" was a bad idea.
January 22, 2013
On Tuesday, 22 January 2013 at 18:10:27 UTC, cal wrote:
> On Tuesday, 22 January 2013 at 09:47:25 UTC, monarch_dodra wrote:
>> Avoids deadlock.
>>
>> imagine 2 threads:
>> a: from 2 to 5.
>> b: from 5 to 2.
>>
>> If both threads acquire their first lock, then you have a dead lock, and your program is basically dead.
>>
>> By always locking low first, you avoid the deadlock in a rather simple but elegant way.
>
> Ah neat. And what about the case from = to? Why doesn' that deadlock in this code? (Concurrency is rather new to me)

Because a single thread may acquire the same lock more than once. At which point, it will increment the lock counter. The lock is released when the counter reaches zero.

This allows having easy "nested" logic, where a function does not have to worry if the caller function already has a lock.
January 22, 2013
ixid:

> I note you always seem to use "in" in your functions and on reddit seemed to imply that this was the idiomatic way of using D yet I recall Jonathan M Davies posting that using "in" was a bad idea.

I think "in" is supposed to be(come) idiomatic, because it's short, and it's supposed to combine two attributes that you usually want, const and scope.

On the other hand scope for arguments is not implemented yet (and Walter doesn't show lot of interest in implementing it, I don't know why. Few days ago Hara has said he wants to try to implement scope. But it's a lot of work, so I don't know if and when he will be done). So if you annotate something with scope, and you let the reference escape, the code now compiles, but later will break.

Breaking the D code on Rosettacode is acceptable, because that site is like a wide variety of tiny test programs. So using "in" in Rosettacode is good. But if you are writing a largish D2 project, then Jonathan is right, it's better to not use "in" and scope arguments, unless you want to fix ton of future errors.

Bye,
bearophile
1 2 3
Next ›   Last »