RE: [GD-Windows] TryEnterCriticalSection
Brought to you by:
vexxed72
From: Brian S. <bs...@mi...> - 2002-01-03 20:20:47
|
There's also all the Interlocked* functions, like InterlockedCompareExchange, for those who have a hard time remembering x86 asm instructions. And for those, like myself, who never knew the instructions :). =20 I've used mutexes (mutices?) a lot, for instance when walking common data structures like network message queues (receive messages on worker thread, process messages on main thread). My usage has typically been with 2 threads going. I've never sensed a problem. I don't know what the internal implementation is like, but I would guess that lots of time has been spent making sync primitives as efficient as possible. =20 If you're worried about the cost of wait objects vs. critical sections, then I guess one question you could ask yourself is, does the time required for WaitForSingleObject(0) compare at all to the time that your thread would just sit there if you just used EnterCritSection instead of TryEnterCritSection? How long do the threads typically hold the critical section? Is your goal to actually avoid having multiple threads execute through the guarded section in close temporal sequence, or are you just worried about having other threads waiting idle while one thread goes through it? Maybe more details about how many threads you have going, what they're doing, how many different places the critical section is currently being used, etc. would help get answers that are more useful to you. --brian -----Original Message----- From: Andy Glaister [mailto:an...@mi...]=20 Sent: Thursday, January 03, 2002 11:42 AM To: gam...@li... Subject: RE: [GD-Windows] TryEnterCriticalSection There is a good performance comparison of different sync objects found at=20 http://www.usenix.org/publications/library/proceedings/usenix-nt98/full_ papers/zabatta/zabatta_html/zabatta.html It depends upon what you are trying to do and how often you will check/set/block in your code. Personally I would write my own sync object using _asm lock cmpxchg - then it would run on all CPUs 486 and above... There are very neat sync objects in the later OS's - SLIST for example is very clever, but only in XP and above (of course, you could implement it yourself without too much trouble) - also spin locks may be useful if you only lock the resource very briefly. Andy Glaister. -----Original Message----- From: Brian Hook [mailto:bri...@py...]=20 Sent: Thursday, January 03, 2002 11:18 AM To: gam...@li... Subject: [GD-Windows] TryEnterCriticalSection It looks like TryEnterCriticalSection is only available on >=3D NT4 (and according to the docs, it's not available on Win9x at all). To get around this I'm thinking that I could switch to using a mutex and WaitForSingleObject with a timeout of 0 milliseconds, but mutexes are heavier weight than critical sections (I'm not sure how bad this is in practice though). Any opinions, comments or shared experiences with this? Brian _______________________________________________ Gamedevlists-windows mailing list Gam...@li... https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows _______________________________________________ Gamedevlists-windows mailing list Gam...@li... https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows |