Some tips about the life cycle of Maya thread pool
2014-12-12 17:48
585 查看
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
// pool is reference counted. Release reference to current thread instance
MThreadPool::release();
// release reference to whole pool which deletes all threads
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.
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
// pool is reference counted. Release reference to current thread instance
MThreadPool::release();
// release reference to whole pool which deletes all threads
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.
相关文章推荐
- 3. Managing the lifecycle of a Basic Thread
- The Life Cycle of a JSP Page
- @the meaning of life@ about search machine
- 【Java多线程】之四:Life Cycle of Thread
- What events are fired as part of the ASP.NET System.Web.UI.Page lifecycle?
- 视图控制器生命周期中各个重要的方法(Swift) (Important Methods during the Lifecycle of a View Controller)
- The life cycle of a typical project 一个典型的项目生命周期
- DOJO组件生命周期(the life cycle of dojo widget)
- The Life Cycle of a Servlet
- M0+/M4中断优先级设置问题(Tips about the interrupt priority of M0+/M4)
- The Life Cycle of Hibernate entity object
- What are the phases of the maven default lifecycle?
- Some tips about the C++(自己看的,很乱)
- DOJO组件生命周期(the life cycle of dojo widget)
- What are the phases of the maven default lifecycle?
- A journey on the Android Main Thread - Lifecycle b
- Managing the Lifecycle of a Service--管理Service的生命周期
- How to invoke the method of managed bean and render view in JSF when we are outside the lifecycle of JSF
- Tips: How to resolve the issue of MySQL about abnormal login after system launched.
- Managing the Lifecycle of a Bound Service