On 16/11/2016 at 04:14, xxxxxxxx wrote:
User Information:
Cinema 4D Version: all
Platform: Windows ; Mac OSX ;
Language(s) : C++ ;
---------
During the time I spent developing various plugins, I often encounter various threading problems. Usually, it is related to accesing document from various threads in multithreaded environment of C4D. The only synchronization mechanism I found within docs is method StopAllThreads, but there is lots of missing details about how it actually works and how shoult it be used.
The documentation says it "Stops all running threads" but that is surely not true - for example if I run some code in C4DThread, it won't be affected. What I think is meant is "Stops all background handlers" (BackgroundHandler callback). That would make sense when StopAllThreads is called from main thread as no background handlers will be reexecuted (as I understand it, background handlers are only executed from main thread). But what happens when I call StopAllThreads from different thread? If it simply stops background handlers, then for how long? Workers could be reexecuted from main thread while I am still working with document from second thread... Or am I supposed to edit document only from main thread and StopAllThreads is useless from other threads? What about reading data from document, am I only allowed to do so from main thread after calling StopAllThreads and background handlers?
And am I even correct in my assumption StopAllThreads only stops background handlers? If so, how does for example drawing in viewport work. When calling StopAllThreads in a loop from second thread (for testing only ofcourse), preview in viewport still kind of works. How is access of drawing thread (which is probably main thread) synchronized with access from any other thread?
Note that I know C4D API has implementation of locks, semaphors, signals etc, but I am asking about situations when other threads working on documents are part of C4D (or other plugins) and therefore I cannot simply use locks to synchronize access (ie I can only control my threads).