Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Fork and creating a thread exits the process

by jcb (Parson)
on Aug 01, 2020 at 02:47 UTC ( [id://11120170]=note: print w/replies, xml ) Need Help??


in reply to Fork and creating a thread exits the process

First, there is no fork(2) call on Windows, so (as other monks have mentioned) Windows perls implement fork using interpreter threads. The obvious problem here is that creating a thread or forking a subprocess completes immediately and the main thread continues, then exits at the end of the script while the children are waiting at sleep 300. When the main thread exits the process, Windows kills the other threads.

On Linux, threads are implemented in terms of clone(2), which is a fork-like call, (NPTL added new system calls with different names long ago but has very similar semantics) so threads are actually lightweight processes. (The Linux kernel calls them both "tasks" instead of distinguishing threads and processes.) Again, both programs will do the same (different) thing: the original process spawns the others and exits, but under Linux, the created threads are more like forked processes, so either they continue to run after the parent exits or the kernel blocks the parent at exit(2) until the other threads complete.

For a solution, try either wait/waitpid or the ->join() method in threads instead of detaching your worker threads.

Replies are listed 'Best First'.
Re^2: Fork and creating a thread exits the process
by fluks (Novice) on Aug 01, 2020 at 11:17 UTC
    The obvious problem here is that creating a thread or forking a subprocess completes immediately and the main thread continues, then exits at the end of the script while the children are waiting at sleep 300. When the main thread exits the process, Windows kills the other threads.

    The main thread didn't continue because the part of the code I showed was running in an indefinite loop. And nothing in the child processes or in the worker threads was executed either.

    For a solution, try either wait/waitpid or the ->join() method in threads instead of detaching your worker threads.

    I think I tried threads without detaching them. And since the part of the program is in an indefinite loop, I don't want to wait the processes to finish because the loop needed to be fast.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11120170]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-25 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found