Jacek Olszak wrote:
> Jacek Olszak wrote:
>> Robert Klemme wrote:
>>> Jacek Olszak <jacekolszak / o2.pl> wrote:
>>>>
>>>>
>>>> Unfortunelty "gets" block my whole application - the "b" thread
>>>> waits until user writes something to the stdin. But I want the b
>>>> thread not wait and run at the same time.
>>>>
>>>> Thanks
>>>> Jacek
>>>
>>> I'm not sure what you're up to in the end but I'd start with
>>> commenting Thread.pass. Ruby normally schedules threads
>>> automatically and thus you usually don't need Thread.pass.  And
>>> btw, you can do $stdout.sync=true - which removes the necessity of
>>> invoking flush.
>>>
>>> Ah, and another note: the first thread blocks the whole app until
>>> you enter
>>> something simply because it's joined on.  The main thread won't exit
>>> until
>>> you enter something.
>>>
>>> HTH
>>>
>>>     robert
>>
>> First of all... thanks for help. I've set sync to true and remove
>> Thread.pass. (but as you said these things won't fix the problem). So
>> I've removed _a.join_ call and now everything works! (but wait a
>> second - only on my newly installed linux :(, on windows works only
>> with Bill solution)
>>
>
> I don't know hot it is possible but this code works on windows too :
>
> $stdout.sync=true
>
> a=Thread.new do
>   50.times do
>     $stdin.gets
>     puts "=="+$aaa.to_s
>   end
> end
>
> b=Thread.new do
>   10000000000.times do |i|
>     $aaa=i
>     # Thread.pass - when uncomment this - works on linux only, when
> comment this - works on windows only :)
>   end
>
> end
>
> b.join

Are you aware of the fact that ruby will exit as soon as the main thread
exits?  So your difference might well be timing related.

10:58:20 [source]: ruby -e 'Thread.new { sleep 1; puts "end" }'
10:59:22 [source]: ruby -e 'Thread.new { sleep 1; puts "end" }; sleep 2'
end
10:59:28 [source]:

Notice how the first piece doesn't print?  That's because the main thread
simply exits killing the sleeping first thread.

Again, I don't know what you're up to so it's difficult to provide advice
how to do it right.  In your simple example thread a is pointless IMHO -
you could do that as well in the main thread.

Cheers

    robert