<div>Hi, Yuan</div><div>Thanks very much for your reply. Actually, I am confused by the code in function worker_routine() in /lib/work.c .<span style="color: rgb(67, 67, 67); font-family: Tahoma, Arial; font-size: 12px; font-weight: bold; line-height: 19.1875px; background-color: rgb(242, 242, 242);"><span id="_editor_bookmark_start_0" style="display: none; line-height: 0px;">‍</span></span></div><div>In file  /lib/work.c of the master branch:</div><div><br></div><div><div><div>Line 317:<span class="Apple-tab-span" style="white-space:pre">        </span>sd_mutex_lock(&wi->startup_lock);</div><div>Line 318:<span class="Apple-tab-span" style="white-space:pre">    </span> /* started this thread */</div><div>Line 319:<span class="Apple-tab-span" style="white-space:pre">     </span>sd_mutex_unlock(&wi->startup_lock);<span id="_editor_bookmark_start_1" style="display: none; line-height: 0px;">‍</span></div></div><div><br></div><div> I don't understand the purpose for lock <span style="line-height: 1.5;">wi->startup_lock and unlock it <span id="_editor_bookmark_start_3" style="display: none; line-height: 0px;">‍</span></span><span style="line-height: 1.5;">immediately. Seems it wants to </span><span style="line-height: 1.5;"><span id="_editor_bookmark_start_4" style="display: none; line-height: 0px;">‍</span></span><span style="line-height: 1.5;">block the new thread here until all the new threads are created when the thread pool need to grow.<span id="_editor_bookmark_start_5" style="display: none; line-height: 0px;">‍</span></span><span id="_editor_bookmark_start_2" style="display: none; line-height: 0px;">‍</span></div><div><span style="line-height: 1.5;"><br></span></div><div>Thanks!</div><div>Bingpeng</div><div><br></div><div><br></div><div style="font-size: 12px;font-family: Arial Narrow;padding:2px 0 2px 0;">------------------ 原始邮件 ------------------</div><div style="font-size: 12px;background:#efefef;padding:8px;"><div><b>发件人:</b> "Liu Yuan";<namei.unix@gmail.com>;</div><div><b>发送时间:</b> 2014年9月4日(星期四) 上午10:07</div><div><b>收件人:</b> "Bingpeng Zhu"<nkuzbp@foxmail.com>; <wbr></div><div><b>抄送:</b> "sheepdog"<sheepdog@lists.wpkg.org>; <wbr></div><div><b>主题:</b> Re: [sheepdog] quesiton about function worker_routine() in lib/work.c</div></div><div><br></div>On Wed, Sep 03, 2014 at 05:37:19PM +0800, Bingpeng Zhu wrote:<br>> Hi, all<br>>    I am reading sheepdog's source code. I have a question of the code in function worker_routine() in  lib/work.c. <br>> It calls sd_mutex_lock(&wi->startup_lock)  in the beginning , and calls  sd_mutex_unlock(&wi->startup_lock) ‍<br>> immediately after the lock. It seems that the code want to block the new threads until all the new threads are created.<br>> I don't understand the purpose for doing this.  Can anybody answer my question?<br>> <br><br>We have tow concepts in thread pool mechanism, workers and main thread, so<br>normally the task would be split into two parts, one can be executed in paralle<br>in different thread context, and the other can be executed in the main thread<br>sequentially.<br><br>worker pool<br><br>thread 1    ---- run someone's job1 in worker thread<br>thread 2    ++++++++++++++++++++ run some other's job2<br>thread 3    **** idle **************<br>            ....<br>         job 1 worker finishes and queue to run something in main thread <br>                 |<br>             V<br>main thread =====---============++++=========<br>                                |<br>                           v<br>                          job2's main part<br><br>In this way, if job1 and job2 has something that need to run sequentially, then<br>we can put those sequential action in main thread.<br><br>work.fn will run in worker pool in parallel with other jobs<br>work.done will run in main thread sequentially<br><br>Thanks<br>Yuan<br></div>