When two threads run a specific process separately, will the program end when one thread returns the value?
Here's the scenario:
You have two threads (which represent different machines) who take the same input from a singular data source, run through the same processes (which do not depend on any shared resources) and return the same value.
If one thread (read:machine) is faster than the other and finishes first, will the program accept that value and end, or will it wait for the other thread to finish? If the answer is the latter, is there anyway to force the program to take the first answer?
The practical reason开发者_运维百科 for this would be to handle unbearably slow machines.
This is entirely you to decide. If you spawn two threads, you can control them from the parent process and decide the behavior you want. You may wait
on both threads, or wait until one of them is available (eg. using select
from the parent thread or signal
from the child thread), and possibly kill the other one (using a signal again, or kill
).
For a very good reference on system programming (multi-processing, threads, communications, concurrency..), see Unix system programming in Objective Caml. It has an example (psearch
here) where threads collaborate to find a result, and stop as soon as one of them succeeded.
精彩评论