BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TCriticalSection does not work?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Cagatay Undeger
Guest





PostPosted: Sat Jan 24, 2004 2:00 pm    Post subject: TCriticalSection does not work? Reply with quote



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


Back to top
Tomasz Piasecki
Guest





PostPosted: Sat Jan 24, 2004 2:28 pm    Post subject: Re: TCriticalSection does not work? Reply with quote



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.

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


Back to top
Cagatay Undeger
Guest





PostPosted: Sat Jan 24, 2004 9:48 pm    Post subject: Re: TCriticalSection does not work? Reply with quote



I think non blocking server socket uses just one thread, so critical
sections does not work.

Thank you,

"Tomasz Piasecki" <mtbrider (AT) _-nospam-_ (DOT) poczta.onet.pl> wrote

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 |




Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Sat Jan 24, 2004 10:55 pm    Post subject: Re: TCriticalSection does not work? Reply with quote

"Cagatay Undeger" <cagatay (AT) interaktif (DOT) gen.tr> wrote


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



Back to top
Cagatay Undeger
Guest





PostPosted: Sun Jan 25, 2004 1:08 pm    Post subject: Re: TCriticalSection does not work? Reply with quote

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)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> wrote

Quote:

"Cagatay Undeger" <cagatay (AT) interaktif (DOT) gen.tr> wrote in message
news:40127a7a (AT) newsgroups (DOT) borland.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





Back to top
Tomasz Piasecki
Guest





PostPosted: Sun Jan 25, 2004 1:28 pm    Post subject: Re: TCriticalSection does not work? Reply with quote

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.

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


Back to top
Tomasz Piasecki
Guest





PostPosted: Sun Jan 25, 2004 1:30 pm    Post subject: Re: TCriticalSection does not work? Reply with quote

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.

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


Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Sun Jan 25, 2004 10:56 pm    Post subject: Re: TCriticalSection does not work? Reply with quote


"Cagatay Undeger" <cagatay (AT) interaktif (DOT) gen.tr> wrote


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



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.