如果你寻求一种单纯的校对政策(可能是一种联系或任何资源),则有两种简单的做法:
半衰期/半衰期模型(通常使用电站通过信息)。
领导人/随行人员模式(通常使用通过信息的请求)。
第一种做法类似:
- You create a pool of threads to
handle a resource. Often this size
(number of threads) needs to be
configurable. Call these threads
Workers .
- You then create a master thread that
will dispatch the work to the
Worker threads. The application program dispatches the task as a
message to the master thread.
- The master thread puts the same on
the message Q of a chosen Worker
thread and the Worker thread removes itself from the
pool. Choosing and removing the
Worker thread needs synchronization.
- After the Worker completes the
task, it returns to the thread-pool.
主人自己可以消费FCFS或prioritized上完成的任务。 这将取决于你们的执行情况。
第二个模式(Leader/eners)类似:
- Create a thread pool. Initially all
are Workers. Then elect a
Leader, automatically rest-all become followers. Note that electing
a Leader has to be synchronized.
- Put all the data to be processed on a
single request Q.
- The thread-pool Leader dequeues
the task. It then immediately
elects a new Leader and starts executing the task.
- The new Leader picks up the next
task.
可能还有其他办法,但上文概述的办法很简单,与大多数使用案例一起工作。
5. 半天
- Higher context switching,
synchronization, and data copying
overhead.
领导人/村长
- Implementation complexity of
Leader election in thread pool.
Now you can decide for yourself the more correct approach.
HTH,