[sheepdog] quesiton about function worker_routine() in lib/work.c

Bingpeng Zhu nkuzbp at foxmail.com
Sat Sep 6 13:58:02 CEST 2014


Hi, Yuan
Thanks very much for your reply. Actually, I am confused by the code in function worker_routine() in /lib/work.c .‍
In file  /lib/work.c of the master branch:


Line 317:	sd_mutex_lock(&wi->startup_lock);
Line 318:	 /* started this thread */
Line 319:	sd_mutex_unlock(&wi->startup_lock);‍



 I don't understand the purpose for lock wi->startup_lock and unlock it ‍immediately. Seems it wants to ‍block the new thread here until all the new threads are created when the thread pool need to grow.‍‍


Thanks!
Bingpeng




------------------ 原始邮件 ------------------
发件人: "Liu Yuan";<namei.unix at gmail.com>;
发送时间: 2014年9月4日(星期四) 上午10:07
收件人: "Bingpeng Zhu"<nkuzbp at foxmail.com>; 
抄送: "sheepdog"<sheepdog at lists.wpkg.org>; 
主题: Re: [sheepdog] quesiton about function worker_routine() in lib/work.c



On Wed, Sep 03, 2014 at 05:37:19PM +0800, Bingpeng Zhu wrote:
> Hi, all
>    I am reading sheepdog's source code. I have a question of the code in function worker_routine() in  lib/work.c. 
> It calls sd_mutex_lock(&wi->startup_lock)  in the beginning , and calls  sd_mutex_unlock(&wi->startup_lock) ‍
> immediately after the lock. It seems that the code want to block the new threads until all the new threads are created.
> I don't understand the purpose for doing this.  Can anybody answer my question?
> 

We have tow concepts in thread pool mechanism, workers and main thread, so
normally the task would be split into two parts, one can be executed in paralle
in different thread context, and the other can be executed in the main thread
sequentially.

worker pool

thread 1    ---- run someone's job1 in worker thread
thread 2    ++++++++++++++++++++ run some other's job2
thread 3    **** idle **************
            ....
         job 1 worker finishes and queue to run something in main thread 
	          |
	          V
main thread =====---============++++=========
                                |
				v
		             job2's main part

In this way, if job1 and job2 has something that need to run sequentially, then
we can put those sequential action in main thread.

work.fn will run in worker pool in parallel with other jobs
work.done will run in main thread sequentially

Thanks
Yuan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.wpkg.org/pipermail/sheepdog/attachments/20140906/53367aec/attachment-0004.html>


More information about the sheepdog mailing list