Maya provides a class named MThreadPool to help developers to create or reuse a thread pool, if you want to know more about this, you can check out the online SDK here.
If you want to know how to use MThreadPool, besides the above link, you can check out our 2 samples, threadTestCmd and threadTestWithLocksCmd in the Maya devkit.
In the 2 samples mentioned above, you will see that MThreadPool is actually released twice by using MThreadPool::release(). The big question for me was why do you need to release it twice?
// pool is reference counted. Release reference to current thread instance MThreadPool::release(); // release reference to whole pool which deletes all threads MThreadPool::release();
Whenever the thread pool is created, it actually sets the count reference to 1, instead of 0, so when we call MThreadPool::init(), the reference count 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. This is the reason why you have to always release it 2 times to destroy it.
The SDK online help 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.". Maya is trying to keep the thread pool alive during the life cycle to improve performances, and the thread pool always survives unless you manually decrease the reference count to 0.
Comments
You can follow this conversation by subscribing to the comment feed for this post.