Jump to content
Sign in to follow this  
JoshJ

HDK Vex initialize function and multithreading

Recommended Posts

///////////////////

I'm writing a custom c++ vex function.  I have a somewhat expensive initial setup that only needs to be done once per instance(not per thread).I have the following scenarios working already:

 

1) multi-threaded off.  Each time the vex function is instanced, my initial setup is called once.  This is fine, but single-threaded.

2) multi-threaded on.  Each time the vex function is instanced, each thread calls the initial setup once.  This is working, but too heavy, not optimal.

3) multi-threaded on.  Initial setup is done in a singleton class which is forced to be called only once.  This is similar to the gamma table example in the help.  Initial setup is called only once, but on subsequent instantiation, each instance shares the same initial setup.  This is not correct, as each instance will require a different setup.

 

What I'd like is:

multi-threaded on. Initial setup is done once per instance, and shared among all the threads invoking that instance. Once the vex function is used again, initial setup is done only once again, not 1*numthreads.

///////////////////

 

Also, is there any way to pass in input args data from parameters to the initialize function?  Currently they only seem to be passed to the evaluate function.

 

 

Share this post


Link to post
Share on other sites

It sounds like your evaluate function needs to do some thread-safe caching of its own.

Share this post


Link to post
Share on other sites

A question:  In a vex function multi-threading scenario, how can I determine which instance certain threads belong to?

 

Background information:

I've come up with a way to cache information for the threads throughout the Init, evaluate, cleanup vex function lifecycle.  However, I'm running into a problem. 

 

When I advance frame by frame slowly, one by one, my function is stable with multi-threading on. When I play it back at realtime, it crashes after a couple of frames.  However, if I cause each thread to print out information to the console during cleanup and init, it plays back without crashing. I believe I know the reason, but I can't think of a solution within the current architecture.

 

Each thread has no awareness of whether the other threads have finished their cleanup.  I also don't see any way to determine which instance different threads belong to.  When some threads move on to the next init function, and other threads are still in the previous cleanup function, this causes a crash (since I've set it up so that all threads are sharing the same per-frame global data). I may have multiple instances of my custom vex function within the same process, and I don't see a way to deal with this per instance.  Is there anything like getMyInstance() for the entire instance?

 

The documentation states "The initialization/cleanup functions are called for each instance of your user function."  However, I found instead that the initialization and cleanup functions are run per thread.

 

 

Share this post


Link to post
Share on other sites

Perhaps you can use some atomic counter so that you only clean up when no instances need the cache item anymore.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×