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 between 2 threads

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
Tumurbaatar S.
Guest





PostPosted: Tue Nov 28, 2006 9:10 am    Post subject: TCriticalSection between 2 threads Reply with quote



To communicate and pass data (several integers and AnsiString's) between 2
threads,
I use TCriticalSection object. The first thread only writes/modifies the
protected section and
the second one only reads. Of course, read/write operations are executed
within TCriticalSection's
Enter/Leave(). My question: do I need to use Enter/Leave() if the writing
thread (the 1st one)
needs to _read_ data that it modified?
Back to top
Eelke
Guest





PostPosted: Tue Nov 28, 2006 4:18 pm    Post subject: Re: TCriticalSection between 2 threads Reply with quote



Tumurbaatar S. wrote:
Quote:
To communicate and pass data (several integers and AnsiString's) between 2
threads,
I use TCriticalSection object. The first thread only writes/modifies the
protected section and
the second one only reads. Of course, read/write operations are executed
within TCriticalSection's
Enter/Leave(). My question: do I need to use Enter/Leave() if the writing
thread (the 1st one)
needs to _read_ data that it modified?


No you don't have to as problems can only arise when data is written

while it is being read.

Actually for trivial data like a single integer no critical section is
needed because the assignment of an int is an atomic operation.

Eelke
Back to top
Boba
Guest





PostPosted: Tue Nov 28, 2006 5:01 pm    Post subject: Re: TCriticalSection between 2 threads Reply with quote



"Eelke" <e.klein (AT) remove (DOT) mplussoftware.nl> wrote in message
news:456c0cd1 (AT) newsgroups (DOT) borland.com...
Quote:
...
Actually for trivial data like a single integer no critical section is
needed because the assignment of an int is an atomic operation.

and what do you call an 'atomic operation' ?
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Nov 28, 2006 5:18 pm    Post subject: Re: TCriticalSection between 2 threads Reply with quote

"Boba" <Boba (AT) somewhere (DOT) world> wrote in message
news:456c1747 (AT) newsgroups (DOT) borland.com...

Quote:
and what do you call an 'atomic operation' ?

An operation that can be executed in a single CPU cycle, such that only one
thread can possible access the memory at a time without extra
synchronization.

However, this fails in a multi-CPU or hyper-threaded system, such that one
CPU can be writing the data, even atomically, while the other CPU is
accessing it. So you should not rely on the accuracy of atomic operations
anymore.


Gambit
Back to top
Eelke
Guest





PostPosted: Tue Nov 28, 2006 5:24 pm    Post subject: Re: TCriticalSection between 2 threads Reply with quote

Boba wrote:
Quote:
"Eelke" <e.klein (AT) remove (DOT) mplussoftware.nl> wrote in message
news:456c0cd1 (AT) newsgroups (DOT) borland.com...
...
Actually for trivial data like a single integer no critical section is
needed because the assignment of an int is an atomic operation.

and what do you call an 'atomic operation' ?


An atomic operation is an operation which is executed completely before

another thread can see the result of the operation. For instance the
assignment of an int is atomic because the CPU changes all 32-bits at
the same time so another thread sees either the old value or the new but
never some combination.

This is only true when the data is properly aligned.

Eelke
Back to top
Boba
Guest





PostPosted: Tue Nov 28, 2006 6:09 pm    Post subject: Re: TCriticalSection between 2 threads Reply with quote

"Eelke" <e.klein (AT) remove (DOT) mplussoftware.nl> wrote in message
news:456c1c40$1 (AT) newsgroups (DOT) borland.com...
Quote:
An atomic operation is an operation which is executed completely before
another thread can see the result of the operation. For instance the
assignment of an int is atomic because the CPU changes all 32-bits at
the same time so another thread sees either the old value or the new but
never some combination.

This is only true when the data is properly aligned.

Eelke

all i ment was synchronization. see Mr.Lebeau's msg.
the op does not mention it. if [s]he is after the correctness
of rw only (regadless of timing) on a single cpu then yes, you
are right. with a multi processor system the story is different.
Back to top
Boba
Guest





PostPosted: Tue Nov 28, 2006 6:20 pm    Post subject: Re: TCriticalSection between 2 threads Reply with quote

"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:456c1c23$1 (AT) newsgroups (DOT) borland.com...
Quote:

"Boba" <Boba (AT) somewhere (DOT) world> wrote in message
news:456c1747 (AT) newsgroups (DOT) borland.com...

and what do you call an 'atomic operation' ?

An operation that can be executed in a single CPU cycle, ...

i bet you meant to say 'single CPU operation'.
btw, the're not many single cycle operations.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) 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.