w***u 发帖数: 156 | 1 最近要写一个多线程的应用, 碰到一些问题,
主线程在一开始就create好4个子线程,子线程一开始wait状态,然后主线程开始往下
走,开始task准备分给子线程,然后broadcast给子线程,自己进入wait状态, 子线程
wakeup,做完task,就wait,做后一个子线程完成后,发信号给主线程,然后wait,
主线程在接到信号后,wakeup,继续下一个task, 然后broadcast。问题是主线程激活
到broadcast时间很短, 而子线程从发信号到wait时间长的话,主线程broadcast的信
号就不会正确叫醒子线程,因为那时的子线程还没有进入wait状态。 如何解决呢?其
实就是要实现一个join的功能,但是又不要释放掉子线程, |
w***u 发帖数: 156 | |
W***o 发帖数: 6519 | 3 use a lock to synch?
【在 w***u 的大作中提到】 : 最近要写一个多线程的应用, 碰到一些问题, : 主线程在一开始就create好4个子线程,子线程一开始wait状态,然后主线程开始往下 : 走,开始task准备分给子线程,然后broadcast给子线程,自己进入wait状态, 子线程 : wakeup,做完task,就wait,做后一个子线程完成后,发信号给主线程,然后wait, : 主线程在接到信号后,wakeup,继续下一个task, 然后broadcast。问题是主线程激活 : 到broadcast时间很短, 而子线程从发信号到wait时间长的话,主线程broadcast的信 : 号就不会正确叫醒子线程,因为那时的子线程还没有进入wait状态。 如何解决呢?其 : 实就是要实现一个join的功能,但是又不要释放掉子线程,
|
p*u 发帖数: 2454 | 4 most common way is main thread puts tasks on a message queue, and worker
threads pull from it.
i don't understand why ur worker threads hv to sleep/wait. if u hv to do
this, i assume u use a condition variable to broadcast. u can use a mutex-
protected flag to slow main thread down: worker threads set this flag to
true right before they go to sleep/wait; main thread always checks this flag
before it tries to broadcast.
【在 w***u 的大作中提到】 : 最近要写一个多线程的应用, 碰到一些问题, : 主线程在一开始就create好4个子线程,子线程一开始wait状态,然后主线程开始往下 : 走,开始task准备分给子线程,然后broadcast给子线程,自己进入wait状态, 子线程 : wakeup,做完task,就wait,做后一个子线程完成后,发信号给主线程,然后wait, : 主线程在接到信号后,wakeup,继续下一个task, 然后broadcast。问题是主线程激活 : 到broadcast时间很短, 而子线程从发信号到wait时间长的话,主线程broadcast的信 : 号就不会正确叫醒子线程,因为那时的子线程还没有进入wait状态。 如何解决呢?其 : 实就是要实现一个join的功能,但是又不要释放掉子线程,
|
p***o 发帖数: 1252 | 5 估计是自己实现的同步逻辑。这个需求用pthread barrier就好。
flag
【在 p*u 的大作中提到】 : most common way is main thread puts tasks on a message queue, and worker : threads pull from it. : i don't understand why ur worker threads hv to sleep/wait. if u hv to do : this, i assume u use a condition variable to broadcast. u can use a mutex- : protected flag to slow main thread down: worker threads set this flag to : true right before they go to sleep/wait; main thread always checks this flag : before it tries to broadcast.
|
v******y 发帖数: 84 | 6 同意这个,楼主估计不知道boss - worker pattern
boss不需要和worker沟通
boss产生work load, 放入shared queue (用mutex同步)
boss产生work load时,如果queue 满了,就wait on queue_full condition
worker thread从queue中读取work load,如果queue空,就wait on queue_empty
condition
boss, 产生一个word load, signal 在等的work thread去读
work load 除了完了,就signal boss产生新的work load
flag
【在 p*u 的大作中提到】 : most common way is main thread puts tasks on a message queue, and worker : threads pull from it. : i don't understand why ur worker threads hv to sleep/wait. if u hv to do : this, i assume u use a condition variable to broadcast. u can use a mutex- : protected flag to slow main thread down: worker threads set this flag to : true right before they go to sleep/wait; main thread always checks this flag : before it tries to broadcast.
|
k***5 发帖数: 583 | 7 楼主自己把自己绕晕了,不要考虑什么主,子shread,就看成5个shread,然后各个都
pending/sleep在某个signal或mutex,等被唤醒。 |
p*u 发帖数: 2454 | 8 he needs to explain this better: "子线程从发信号到wait时间长"
【在 k***5 的大作中提到】 : 楼主自己把自己绕晕了,不要考虑什么主,子shread,就看成5个shread,然后各个都 : pending/sleep在某个signal或mutex,等被唤醒。
|