JoshJ Posted April 11, 2015 Share Posted April 11, 2015 /////////////////// 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. Quote Link to comment Share on other sites More sharing options...
edward Posted April 12, 2015 Share Posted April 12, 2015 It sounds like your evaluate function needs to do some thread-safe caching of its own. Quote Link to comment Share on other sites More sharing options...
JoshJ Posted April 12, 2015 Author Share Posted April 12, 2015 Thanks edward, I'll look into it. Quote Link to comment Share on other sites More sharing options...
JoshJ Posted April 16, 2015 Author Share Posted April 16, 2015 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. Quote Link to comment Share on other sites More sharing options...
edward Posted April 17, 2015 Share Posted April 17, 2015 Perhaps you can use some atomic counter so that you only clean up when no instances need the cache item anymore. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.