Re: Thread safe event loop
On Mar 24, 3:30 am, jonat...@sliid.org wrote:
For anyone interested, this is what I ran off. Note that it uses an
__asm__ block...
Any comments welcome. This shouldn't be considered "robust".
The only real comment is: why aren't you using the system level
primitives. A CriticalSection (under Windows) or a
pthread_mutex_t (under Unix and look-alikes) shouldn't cost any
more if there is no contention, and will handle the waiting a
lot more elegantly (and probably with less total overhead) if
there is. A queue, of course, will require a conditional (I
don't know how this is done under Windows) if you actually want
to wait on it.
The usual solution is to wrap the locking itself in a class,
with the destructor releasing the lock, see boost::mutex, for
example. And boost::conditional.
There do exist non-locking algorithms as well, but they require
careful ordering and atomic access to specific variables in the
data structure itself (thus, asm access). The ones I'm familiar
with also only work on fixed length structures (which may be
perfectly acceptable in your case).
If you don't need to support wait, and use boost::mutex and
std::deque, the entire code should only be about 10 lines, and
will probably be considerably faster than your solution.
(Requiring dynamic allocation for an enum is NOT a very good
solution.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34