Why this simple program give no output, and terminates immidiatly
after its launched? There should be thread running and giving lots of
'lala', am I right?
class Bar
def initialize
end
def foo
Thread.new do
while true
puts "lala\n"
end
end
end
end
b=Bar.new
b.foo
|
|
0
|
|
|
|
Reply
|
pjastrz (1)
|
10/19/2009 10:03:23 PM |
|
On 20 Oct, 2009, at 1:05 AM, pawel wrote:
> Why this simple program give no output, and terminates immidiatly
> after its launched? There should be thread running and giving lots of
> 'lala', am I right?
> class Bar
> def initialize
>
> end
> def foo
> Thread.new do
try changing this to a = Thread.new
> while true
> puts "lala\n"
> end
> end
then add an a.join here
> end
> end
>
> b=Bar.new
> b.foo
Someone else would have to explain why that happens. Don't know enough
about threads (let alone ruby's) to explain that.
--
patrick
|
|
0
|
|
|
|
Reply
|
pokui (11)
|
10/19/2009 10:34:16 PM
|
|
El Martes, 20 de Octubre de 2009, pawel escribi=F3:
> Why this simple program give no output, and terminates immidiatly
> after its launched? There should be thread running and giving lots of
> 'lala', am I right?
> class Bar
> def initialize
>=20
> end
> def foo
> Thread.new do
> while true
> puts "lala\n"
> end
> end
> end
> end
>=20
> b=3DBar.new
> b.foo
>=20
When you create a thread, the main threads continues. In you case the main=
=20
thread terminates after calling "Thread.new" so the program exists.
If you want to *wait* all the created threads to finish their work, then yo=
u=20
must "join" them:
my_thread =3D Thread.new do .... end
do other stuff in main thread
my_thread.join
=2D-=20
I=F1aki Baz Castillo <ibc@aliax.net>
|
|
0
|
|
|
|
Reply
|
ibc (607)
|
10/19/2009 10:45:58 PM
|
|
pawel wrote:
> Why this simple program give no output, and terminates immidiatly
> after its launched? There should be thread running and giving lots of
> 'lala', am I right?
> class Bar
> def initialize
>
> end
> def foo
> Thread.new do
> while true
> puts "lala\n"
> end
> end
> end
> end
>
> b=Bar.new
> b.foo
>
>
>
Hi Pawel, here is one example, see if you can follow it.
class Bar
def initialize( msg )
@msg = msg
end
def foo
t_exit = Time.now.to_i + 3
while true
puts @msg
break if Time.now.to_i > t_exit
sleep 0.5
end
end
end
b1 = Thread.new{ Bar.new("x").foo }
b2 = Thread.new{ Bar.new("o").foo }
# here main thread waits for thread b1 and b2 to exit
b1.join
b2.join
--
Kind Regards,
Rajinder Yadav
http://DevMentor.org
Do Good ~ Share Freely
|
|
0
|
|
|
|
Reply
|
devguy.ca (119)
|
10/20/2009 1:46:02 AM
|
|
On 20.10.2009 00:34, Patrick Okui wrote:
> On 20 Oct, 2009, at 1:05 AM, pawel wrote:
>
>> Why this simple program give no output, and terminates immidiatly
>> after its launched? There should be thread running and giving lots of
>> 'lala', am I right?
>> class Bar
>> def initialize
>>
>> end
>> def foo
>> Thread.new do
>
> try changing this to a = Thread.new
>
>> while true
>> puts "lala\n"
>> end
>> end
>
> then add an a.join here
>
>> end
>> end
>>
>> b=Bar.new
>> b.foo
>
> Someone else would have to explain why that happens. Don't know enough
> about threads (let alone ruby's) to explain that.
All threads other than main are demon threads in Ruby, i.e. as has been
explained they do not keep the process alive.
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
|
|
0
|
|
|
|
Reply
|
shortcutter (5766)
|
10/20/2009 8:03:43 PM
|
|
|
4 Replies
30 Views
(page loaded in 0.145 seconds)
Similiar Articles: C++: PTHREAD_CANCEL_ASYNCHRONOUS and random crash - comp ...I am trying to terminate threads in a C++ program using POSIX threads. I use pthread_cancel(), and the thread is configured with PTHREAD_CANCEL_ENABLE... Using thread-specific data in shared libraries - comp.programming ...Since only the code owning the key can assign a value to the key, and it can get control via a destructor on thread termination, it's easy for code to track the lifetime. Problem with pthreads C++ wrapper class on Linux - comp ...int Create(void* pThreadArg, unsigned int* pThreadID); // Terminate this thread // Note that the calling thread DOES NOT return from this call void EndThread ... 'Java-like' synchronization for pthreads with C++. - comp.unix ...In other words, while a thread holds a lock on a resource, the same thread should ... Simple synchronization between delete and thread termination ... and specific values in ... RDTSC (was: Fastest logical not) - comp.lang.asm.x86It might be possible to 'outsource' testees and run them in single threads, terminating themselves after the test was done. Another way was to call computation intensive ... My JVM is closed as soon as I close my Applet - comp.lang.java ...They should inform all there non-daemon worker threads to terminate, and should dispose any active windows (calling frame.dispose()). For several reasons: One ... Tab-delimited records in list-directed READ - comp.lang.fortran ...Whereas, threads, process initiate/terminate, process priority control would be more valuable. Another would be a well designed bit string (for my definition of ... suspending a process - comp.unix.programmerNow i terminate the parent process using kill -9 pid. After i terminate the parent ... How to tell if a process is paused? - comp.os.ms-windows ... > > "Each thread has a ... waitpid == -1 and errno == ECHILD - comp.unix.programmer ...waitpid() -- wait for process termination The other threads return -1, with errno set to ECHILD. If the calling ... of the process terminate, at which time waitpid ... sed, remove last new line - comp.lang.awkDear All, I couldnt find the solution other threads, and as the other threads are ... there *may* be hacks of sed that will output the last line > without a terminating \n ... TerminateThread functionThe thread handle returned by the CreateThread and CreateProcess functions has THREAD_TERMINATE access, so any caller holding one of these handles can terminate ... Multithreading: Terminating ThreadsTwo normal situations cause a thread to terminate: The controlling function exits or the thread is not allowed to run to completion. If a word processor used a thread ... 7/14/2012 4:56:53 AM
|