Reputation: 9
I have a multi-touch application. It has 3 ccv applications sending udp packets to it. The application created receives all of these packets via ofxTuio which is mulit-threaded. These touches are then coming into the code we wrote on a single thread. We have display objects. They each have a list containing pointers to all of the objects they contain. For example we have written, onEnterFrame, addChild, removeChild; functions similar to the display list syntax you would see with actionscript. We, however, are getting iterating errors (concurrency) issues on the object's list because of the multi-threaded tuio events. How do we prevent this from happening. It seems as though locking (mutex) would not be right because these lists are not being accessed on multiple threads. I have created a custom queue for this list where they never iterate while objects are being removed or added to the list with a couple booleans and some extra lists for queueing. I suspect people have run into things like this, whats proper practice?
Upvotes: 0
Views: 170
Reputation: 299
If you are using the std::list anything but removing from the list will not invalidate iterators, in fact you have to be iterating the element being removed for this to pose a problem. If you can structure your algorithm to handle it this way on the single thread this may be enough.
I'm curious as to what kind of iterator errors you are getting, are you getting incompatible errors? How do the display object's get their lists? Via this Queue? Once the object has the list can the list be changed?
Upvotes: 0
Reputation: 9278
Use mutexes or atomic compare_and_swap instructions to make your data structures thread-safe.
Upvotes: 1
Reputation: 16207
Flags aren't enough if you have multiple threads. Use a mutex or some other critical section to lock access for reads and writes. That's what they are for especially if you have any asynchronous or unsolicited events.
Upvotes: 2