Some tips about the life cycle of Maya thread pool

As you know, Maya provides class MThreadPool to help develop to create or reuse a thread pool, if you want to know more about this, you can reference the online SDK at http://help.autodesk.com/view/MAYAUL/2015/ENU/?guid=__files_GUID_2BEF58D3_6162_4235_A6CE_3D8B0742A0AE_htm and http://help.autodesk.com/view/MAYAUL/2015/ENU/?guid=__cpp_ref_class_m_thread_pool_html.

If you want to know how to use MThreadPool, besides the above link, you can reference our 2 samples, threadTestCmd and threadTestWithLocksCmd. But here, I just want to talk about the life cycle of the thread pool.

If you check with the above 2 samples, you will see MThreadPool actually be released twice by MThreadPool::release() as follow, why? And what is the purpose of doing each one? I am also interested in this, so I just spend some time to dig into the source code.

Code Snippet
  1. // pool is reference counted. Release reference to current thread instance
  2. MThreadPool::release();
  3.  
  4. // release reference to whole pool which deletes all threads
  5. MThreadPool::release();

 

When I dig into the code, I find that whenever the thread pool is created, it actually sets the count reference to 1, instead of 0, so when we call MThreadPool::init(), the cound reference will be increased to 2. So if you release it once, it just decrease the reference count to 1, which will not kill the thread pool, you have to always release it one more time to make it be destroyed. What the benifit for this behavior? Actually, from the online SDK, it says "Since creation and deletion of threads is expensive, it is a good idea to make use of the thread pool where possible, and try to keep it around between invocations of the plug-in rather than recreate it each call.", so everything is clear now, Maya will keep the thread pool alive during the life cycle to save the performance, and it always survives unless you manually decrease the reference count to 0.     

  

posted @ 2014-12-12 17:48  如梦令  阅读(321)  评论(0编辑  收藏  举报