Board index » cppbuilder » TCriticalSection does not work?

TCriticalSection does not work?


2004-01-24 10:00:55 PM
cppbuilder62
Hi,
I am trying to use critical sections for non blocking socket programming,
but they are not functioning?
For example, the below code should be blocked, but it is not.
TCriticalSection * critical = new TCriticalSection ();
critical->Enter();
critical->Enter();
ShowMessage("Critical section does not wor !");
This one also should block but it is not again?
TCOMCriticalSection * critical = new TCOMCriticalSection();
TCOMCriticalSection::Lock * lock1 = new TCOMCriticalSection::Lock (
*critical );
TCOMCriticalSection::Lock * lock2 = new TCOMCriticalSection::Lock (
*critical );
kind regards,
Cagatay
 
 

Re:TCriticalSection does not work?

Cagatay Undeger wrote:
Quote
TCriticalSection * critical = new TCriticalSection ();
critical->Enter();
critical->Enter();
ShowMessage("Critical section does not wor !");
read about critical sections in winapi help.
Critical sections are specially made in this way that entering it in
thread that already has entered it does not have effect - just for
avoiding thread lock-ups.
TP.
--
| _ _ _ |
| _____ _| |_| | __ (o) | | __ __ @poczta.onet.pl |
| | \ | | |o \| \| |/o |/ _\| \ |
| |_|_|_| \_| |__/|_| |_|\__|\__||_| Tomasz Piasecki |
 

Re:TCriticalSection does not work?

I think non blocking server socket uses just one thread, so critical
sections does not work.
Thank you,
"Tomasz Piasecki" < XXXX@XXXXX.COM >wrote in message
Quote
Cagatay Undeger wrote:
>TCriticalSection * critical = new TCriticalSection ();
>critical->Enter();
>critical->Enter();
>ShowMessage("Critical section does not wor !");
read about critical sections in winapi help.

Critical sections are specially made in this way that entering it in
thread that already has entered it does not have effect - just for
avoiding thread lock-ups.

TP.

--
| _ _ _ |
| _____ _| |_| | __ (o) | | __ __ @poczta.onet.pl |
| | \ | | |o \| \| |/o |/ _\| \ |
| |_|_|_| \_| |__/|_| |_|\__|\__||_| Tomasz Piasecki |

 

{smallsort}

Re:TCriticalSection does not work?

"Cagatay Undeger" < XXXX@XXXXX.COM >wrote in message
Quote
I am trying to use critical sections for non blocking
socket programming, but they are not functioning?
Yes, they do work. You are just using them incorrectly.
Quote
For example, the below code should be blocked, but it is not.
It appears that you are creating new critical section instances each time
you want to apply a lock. You should not be doing that. Critical sections,
mutexes, semiphores, TMultiReadExclusiveWriteSynchronizer, all of these
synch objects have one thing in common - every time you want to support
locking/unlocking of shared resources somewhere, you are supposed to be
sharing a common instance. You are not supposed to be creating new
instances each time you want to lock something, that defeats the whole
purpose. Synch locks only work correctly when multiple threads are trying
to access and lock/unlock the same sych object, not their own individual
sych objects.
Gambit
 

Re:TCriticalSection does not work?

Sorry, I didn't informed clearly that the code I sent was just a test
sample, not the way I use.
I always have one instance within an application.
I was expecting that 2 sequential enters on a single critical section will
block itself.
Thank you
Cagatay
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Cagatay Undeger" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>I am trying to use critical sections for non blocking
>socket programming, but they are not functioning?

Yes, they do work. You are just using them incorrectly.

>For example, the below code should be blocked, but it is not.

It appears that you are creating new critical section instances each time
you want to apply a lock. You should not be doing that. Critical
sections,
mutexes, semiphores, TMultiReadExclusiveWriteSynchronizer, all of these
synch objects have one thing in common - every time you want to support
locking/unlocking of shared resources somewhere, you are supposed to be
sharing a common instance. You are not supposed to be creating new
instances each time you want to lock something, that defeats the whole
purpose. Synch locks only work correctly when multiple threads are trying
to access and lock/unlock the same sych object, not their own individual
sych objects.


Gambit


 

Re:TCriticalSection does not work?

Cagatay Undeger wrote:
Quote
I think non blocking server socket uses just one thread, so critical
sections does not work.
I don't understand you. TCriticalSection is a wrapper of WinAPI
CRITICAL_SECTION and it has nothing to do with client socket. The code
you presented does just what it should do.
And as far as I know, nonblocking socket runs in separate thread, not
in main thread. That's why it is nonblocking.
TP.
--
| _ _ _ |
| _____ _| |_| | __ (o) | | __ __ @poczta.onet.pl |
| | \ | | |o \| \| |/o |/ _\| \ |
| |_|_|_| \_| |__/|_| |_|\__|\__||_| Tomasz Piasecki |
 

Re:TCriticalSection does not work?

Cagatay Undeger wrote:
Quote
I was expecting that 2 sequential enters on a single critical section will
They don't if you enter critical section twice in the same thread.
If you enter it in one thread, then trying to enter it in second thread
will lock this thread until first one leaves critical section.
TP.
--
| _ _ _ |
| _____ _| |_| | __ (o) | | __ __ @poczta.onet.pl |
| | \ | | |o \| \| |/o |/ _\| \ |
| |_|_|_| \_| |__/|_| |_|\__|\__||_| Tomasz Piasecki |
 

Re:TCriticalSection does not work?

"Cagatay Undeger" < XXXX@XXXXX.COM >wrote in message
Quote
I was expecting that 2 sequential enters on a single critical
section will block itself.
If it allowed that in the manner that you have shown, then you would
deadlock your code. Which is exactly why synch objects do not allow a
thread to obtain the lock more than once at a time. If a thread already has
the lock, further attempts to obtain the lock from within the same thread
will have no effect.
Gambit