Forum: Ruby atomic statements in multithreading

A011f0e5355664647f04aadad1bf7ce4?d=identicon&s=25 Prog Rammer (proggrammer)
on 2012-11-26 12:03
suppose I am working in multiple thread each thread runs following
steps:



step-1
step-2
step-3
step-4
step-5

now I want to make step 2 and step 3 an atomic statement, it means when
one thread goes to statement 2, every other thread should stop and run
only when that particular thread completes step 3. Is it possible? How?

note mutex is not the solution, as mutex keeps thread not running a
block of code at same time, here if one thread goes to the block all
other thread should stop.
8bfb3a1eb9879049b4886cd3f9f321c4?d=identicon&s=25 Arlen Cuss (unnali)
on 2012-11-26 12:14
(Received via mailing list)
That is not what "atomic" would usually mean; what you're asking for is
a mix of many other concepts. Thread.exclusive may be want you want:

step-1
Thread.exclusive do
step-2
step-3
end
step-4
step-5

But it's loaded with gotchas and you may want to be more explicit; or
indeed, work out a way to not need this synchronisation between threads.
A011f0e5355664647f04aadad1bf7ce4?d=identicon&s=25 Prog Rammer (proggrammer)
on 2012-11-26 12:35
Arlen Cuss wrote in post #1086450:
> That is not what "atomic" would usually mean; what you're asking for is
> a mix of many other concepts. Thread.exclusive may be want you want:
>
> step-1
> Thread.exclusive do
> step-2
> step-3
> end
> step-4
> step-5
>
> But it's loaded with gotchas and you may want to be more explicit; or
> indeed, work out a way to not need this synchronisation between threads.

Does it do the same as:  ??

Thread.list.each do |t|
   if t!=Thread.current
  t.stop
   end
end
step-2
step-3
Thread.list.each do |t|
   if t!=Thread.current
  t.run
   end
end
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2012-11-26 13:07
(Received via mailing list)
On Mon, Nov 26, 2012 at 12:03 PM, ajay paswan <lists@ruby-forum.com>
wrote:
> now I want to make step 2 and step 3 an atomic statement, it means when
> one thread goes to statement 2, every other thread should stop and run
> only when that particular thread completes step 3. Is it possible? How?
>
> note mutex is not the solution, as mutex keeps thread not running a
> block of code at same time, here if one thread goes to the block all
> other thread should stop.

Why do you want to do that?  That will cripple throughput more than
necessary.

Kind regards

robert
A011f0e5355664647f04aadad1bf7ce4?d=identicon&s=25 Prog Rammer (proggrammer)
on 2012-11-26 13:15
Robert Klemme wrote in post #1086457:

> Why do you want to do that?  That will cripple throughput more than
> necessary.
>
> Kind regards
>
> robert
To solve: http://www.ruby-forum.com/topic/4408352#new
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2012-11-26 13:31
(Received via mailing list)
On Mon, Nov 26, 2012 at 1:15 PM, ajay paswan <lists@ruby-forum.com>
wrote:
> Robert Klemme wrote in post #1086457:
>
>> Why do you want to do that?  That will cripple throughput more than
>> necessary.

> To solve: http://www.ruby-forum.com/topic/4408352#new

I don't see how your solution is necessary to solve that.

Cheers

robert
A011f0e5355664647f04aadad1bf7ce4?d=identicon&s=25 Prog Rammer (proggrammer)
on 2012-11-26 14:05
Robert Klemme wrote in post #1086464:
> On Mon, Nov 26, 2012 at 1:15 PM, ajay paswan <lists@ruby-forum.com>
> wrote:
>> Robert Klemme wrote in post #1086457:
>>
>>> Why do you want to do that?  That will cripple throughput more than
>>> necessary.
>
>> To solve: http://www.ruby-forum.com/topic/4408352#new
>
> I don't see how your solution is necessary to solve that.
>
> Cheers
>
> robert

basically I am unable to click a link on ie, so I got an alternating bad
solution (sometimes doesnt work) to focus on link and then clicking. I
don't want to lose focus of the link before it is clicked due to
multithreading. am I able to make you understand?

Please reply if I need to clarify more.
C218577e556e6e5351c9691fc4b1aff6?d=identicon&s=25 Nikita Zubkov (Guest)
on 2012-11-26 14:23
(Received via mailing list)
Are you sure you need to stop other threads in CURRENT process? Very
unlikely. Why don't you write threads stopper and resumer yourself and
test?

2012/11/26 ajay paswan <lists@ruby-forum.com>
A011f0e5355664647f04aadad1bf7ce4?d=identicon&s=25 Prog Rammer (proggrammer)
on 2012-11-27 19:25
Nikita Zubkov wrote in post #1086480:
> Are you sure you need to stop other threads in CURRENT process? Very
> unlikely. Why don't you write threads stopper and resumer yourself and
> test?
>
> 2012/11/26 ajay paswan <lists@ruby-forum.com>

Isnt what i wrote is the stopper and resumer you are talking about? is
that correct?
A74a68807619459925cc1d8e1045c7bd?d=identicon&s=25 Tony Arcieri (Guest)
on 2012-11-27 21:01
(Received via mailing list)
On Mon, Nov 26, 2012 at 4:03 AM, ajay paswan <lists@ruby-forum.com>
wrote:

> note mutex is not the solution, as mutex keeps thread not running a
> block of code at same time, here if one thread goes to the block all
> other thread should stop.


I'm still not sure I entirely understand what you want, but you can make
other threads block on a Queue if you would like for them to wait until
some event has completed.

Alternatively you can use a Mutex + ConditionVariable.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.