Engineers call situations like this race conditions, yes. The strategy you describe inherently does lead to race condition no question about it. QueueUserAPC would do exactly the same thing as the mutex locking, or have the exact same race condition problem, which you describe you dont want.
To be honest mutex locking is slightly better than you make it sound, since it frees the computer to do other things. But again you have no control over such things.
Theres no way around for this unfortunately IF you really design thisway. See the MObject must ensure you access while nothing else has access. And only running in same thread, or locking the other thread, ensures this. In fact theres little you can do about this in older maya implementations. So whetever code you have now needs a LOT of change to become therad safe, even possibly 99% rewirite.
On a second note you should NEVER make a persistent list of MObjects, you dont own the object. See whet might happen is that maya deletes whatever the MObject points to while your hibernating, unless you can 100% guarantee that your list is always up to date. Which you dont want to do, because it would slow down you EVEN MORE, if you dont do it tough you will cause maya to crash, go unstable or something on that line. Fighting instability by introducing more instability is clearly not the way to go. instead use a list of MObjectHandles as they provide you with the functionality you need for persistent storage.
only accept ULONG_PTR as a argument,which is a pointer to unsigned long.
ULONG_PTR is way of saying a pointer to anything arbitrary, cast your handle.
In essence you should not be doing it this way, since you can not just win it. To do this properly you MUST know what the hell takes time and what is not taking up time. So you should profile all your calls. Then redesign your loop so that.
Everything that does not take time is taken in main thread, but also ALL HEAVY MAYA INTERFACING CALLS MUST BE IN THE MAIN THREAD, if you want same code to work across maya generations. And everything that is NOT maya internals and that does take time is put the the second tread. Now then you may need to start thinking fo caching to minimize maya api calls if indeed thats the problem.
However its much more likely that the call to storage of the data is whats costing you the actual bulk of time, in which case you retrieve data you need, possibly just what changed, into your own memory structure, then pass that to a separate thread that does the heavy lifting. (tcpip, com and disk images carry HUGE time penelties per call.)
whishing good luck is in order. My advice would be to think really hard if your solving the real problem, or just the symptom. I mean really, you probably get much better bang for the buck if you take a look at what causes your crash and autosave just before that, also defeering things for minutes or seconds can not be taht big a deal.** Anyway since you say it frequently crashes saving, its a indicator that theres something worng iwth some plugin, possibly hardware.
But yeah if you do viligant caching, profile well, and write well designed code then i dont really see a problem for succeeding. Just not entirely optimal use of time tough. Say it crashes 1 time for a 3 minute discomfort and 1 minute loss, versus 3 minute startup and 0 second loss, yoru looking at a minute gain. For say, a very optimistic, 2 weeks of more coding then you need 2*8*60/2 = 960 crashes to pay your efforts back in the most naive situation. With say 5 crashes a day (ifmore seriously look at the stuff you have much closer) 192 days to bay back. Which is remotely feasible if not for the fact that you really cant expect it to be all this clean. Especially since you can probebly make the 1 minute loss one in a hour.
- I dont actually think you have much choice in all the nifty threading things, since maya 7.0 to my recollection is nowhere nearly thread safe enough for you to succeed, without a lot of hard work. Sicne you cant make internals behave better you probably cant leverage all possibilities either. They only became ready with the major threading rehaoul in maya 2008
** it is after all a tradeof, you can not possibly save everey state of a computer even if you wished to without slowing it to a crawl. See each second the i7 computer maxing capacity out shuffles hundreds of gigabytes of data. Maybe it would be cool, but you know if it was even remotely easy and feasible they would have done it.
PS: programming multithreaded applications IS HARD on so many levels, so be preopared to work for much longer that you think is needed now.
PPS: it might not be animator friendly but if its not engineering friedly it might take years to make it animator friencly.