last day (14 days later) » 

8:49 AM
0
A: JS-style async/non-blocking callback execution with Ruby, without heavy machinery like threads?

lolmaus - Andrey MikhaylovOkay, after some fiddling with threads and studying contributions by apeiros and asQuirreL, i came up with a solution that suits me. I'll show sample usage first, source code in the end. Example 1: simple non-blocking execution First, a JS example that i'm trying to mimic: setTimeout( functio...

 
Supplying a new mutex per branch adds nothing. They might as well not be there. The rest of it is pretty neat.
 
D'oh, of course, it should be a @mutex defined in initialization. Will fix.
 
If you want to abstract away mutexes, then rather than do it in the branch class, i would do it in the thread safe data it is supposed to protect.
 
Can you explain why?
 
Suppose you have multiple threads accessing 2 completely independent arrays. With one mutex, accessing one will block the other, whereas having one mutex per array allows as much concurrency as you can afford whilst maintaining safety.
 
8:49 AM
I've updated the code to allow multiple mutexes. Please see the source and the second example. Note how you don't have to instantiate each mutex manually, they are created lazily when you use them. What do you think?
 
It is a neat trick, but ultimately not the Branch classes concern so it breaks SRP.
 
Why not, @asQuirreL? And can you anticipate any problem that this violation might cause?
 
SRP = Single Responsibility Principle en.wikipedia.org/wiki/Single_responsibility_principle
 
From my point of view, my class does not break SRP because the one task it is aimed to solve is to automate monotonous scaffolding required to fire blocks asynchronously.
I know what that is.
 
that is not how SRP is defined
a responsibility is any task that could cause the codebase to change
in this case there are two: the asynchronous calling of blacks, and the provisioning of mutexes
From a maintainability point of view, it is worth differentiating when you first instantiate a mutex, and when you are using it
s/blacks/blocks
from a testability point of view, single responsibility principle makes it easier to test, because every responsibility you add, results in an combinatorial increase in scenarios to test
Really, if you want to modify ann array inside an asynchronous block, you should create a ThreadSafeArray class, which does the locking for you
like I said, there is no "one size fits all" solution to use mutexes, so in my books, that is not a monotonous problem, but one that requires careful thought and consideration
in fact, somebody has done it for you: github.com/ruby-concurrency/thread_safe
 
 
1 hour later…
10:14 AM
My goal was to reduce the scaffolding required to do what JS does with a single setTimeout() call. It is intended for most simple use cases only. If i happen to need complex mutex manipulation, i would indeed build the scaffolding manually.
 

last day (14 days later) »