Click to See Complete Forum and Search --> : FIFO thread synchronizing


menny_ed
January 11th, 2005, 03:38 AM
Hello,
i'm programming a multithreaded communication application. Some of the communication operations are being hold in a a shared (for all threads) synchronized queue. the synchronized queue is using c++ 'CRITICAL_SECTION' handle (using the 'EnterCriticalSection' and 'LeaveCriticalSection' functions).

have three questions (actually it's two):
1) Is 'CRITICAL_SECTION' ensure FIFO threads access into the critial sections of code?
2) If not, what can i use to ensure FIFO access?
3) If yes, is there a better (less CPU time, less resources) way to ensure FIFO access to critical sections of code?

thanks

Andreas Masur
January 11th, 2005, 03:53 AM
[ Moved thread ]

Andreas Masur
January 11th, 2005, 03:54 AM
1) Is 'CRITICAL_SECTION' ensure FIFO threads access into the critial sections of code?
2) If not, what can i use to ensure FIFO access?
3) If yes, is there a better (less CPU time, less resources) way to ensure FIFO access to critical sections of code?

To answer all three questions in once...as long as you are working within the same process, you are fine with critical sections...

menny_ed
January 11th, 2005, 04:38 AM
To answer all three questions in once...as long as you are working within the same process, you are fine with critical sections...

As i understasnd, if my shared objects are shared in the same application only,
using CRITICAL_SECTION method will ensure FIFO access by the threads to the shared objects?

Andreas Masur
January 11th, 2005, 06:46 AM
As i understasnd, if my shared objects are shared in the same application only,
using CRITICAL_SECTION method will ensure FIFO access by the threads to the shared objects?
Oopppss....sorry...misunderstood your first question....no, if you need FIFO access, then you have to implement this on your own (using a critical section for example). Critical sections as well as other synchronization objects don't do it theirselves...

Sorry for the misleading answer in my previous post...

menny_ed
January 11th, 2005, 07:12 AM
....no, if you need FIFO access, then you have to implement this on your own (using a critical section for example...

Thanks.
i was quite suprised when you said that CRITICAL_SECTION IS implementing FIFO access. Could you point me to an implementation of a FIFO locking mechanism?
Or maybe give me some pointers to implement it myself?

thanks

Andreas Masur
January 11th, 2005, 09:54 AM
Well...actually no....since I never had such a problem yet...sorry...try searching the internet...

menny_ed
January 12th, 2005, 04:02 AM
hi.
please check this link:
http://www.codeproject.com/script/comments/forums.asp?forumid=1647&fr=76#xx1013134xx

Blake Miller claims that the CRITICAL_SECTION operations ARE in FIFO order.
could you point me to someplace that says it's not?

It's realy important that i'll figure it out.

thanks

marten_range
January 12th, 2005, 05:12 AM
From MSDN:


The threads of a single process can use a critical section object for mutual-exclusion synchronization. There is no guarantee about the order in which threads will obtain ownership of the critical section, however, the system will be fair to all threads.


See:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/initializecriticalsection.asp

Hope this helps

marten_range
January 12th, 2005, 05:19 AM
FYI:

See thread library in boost (www.boost.org). Although I haven't used that library except for small test applications the following link suggest that you can specify a FIFO Scheduling Policy for the mutexes in boost.

http://www.boost.org/doc/html/threads/concepts.html#threads.concepts.FIFO-scheduling-policy

Hope this helps

darwen
January 12th, 2005, 06:12 AM
Here's a class I wrote which does a FIFO queued critical section.

I've found that critical sections are NOT fifo queued which is why I wrote this. I've also found that under high loads with a large number of threads critical sections can cause thread starvation : i.e. some threads are permenantly waiting.

These classes are MFC based but should give you some idea about how to do this.

Darwen.

menny_ed
January 12th, 2005, 07:10 AM
Thanks alot guys.
i've tried to build a FIFO critical section which is implemented with EVENT handles, CRITICAL_SECTION, and a STL's list.
I've attached the files.
I think that it consumes too much CPU time. could you go over it?

thanks