Thread View: borland.public.cppbuilder.nativeapi
7 messages
7 total messages
Started by Tumurbaatar S.
Tue, 28 Nov 2006 09:03
TCriticalSection between 2 threads
Author: Tumurbaatar S.
Date: Tue, 28 Nov 2006 09:03
Date: Tue, 28 Nov 2006 09:03
9 lines
1297 bytes
1297 bytes
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?
Re: TCriticalSection between 2 threads
Author: Eelke
Date: Tue, 28 Nov 2006 10:18
Date: Tue, 28 Nov 2006 10:18
19 lines
1767 bytes
1767 bytes
Tumurbaatar S. wrote: > 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
Re: TCriticalSection between 2 threads
Author: Boba
Date: Tue, 28 Nov 2006 11:01
Date: Tue, 28 Nov 2006 11:01
7 lines
1160 bytes
1160 bytes
"Eelke" <e.klein@remove.mplussoftware.nl> wrote in message news:456c0cd1@newsgroups.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' ?
Re: TCriticalSection between 2 threads
Author: Remy Lebeau (TeamB)
Date: Tue, 28 Nov 2006 11:18
Date: Tue, 28 Nov 2006 11:18
17 lines
1453 bytes
1453 bytes
"Boba" <Boba@somewhere.world> wrote in message news:456c1747@newsgroups.borland.com... > 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
Re: TCriticalSection between 2 threads
Author: Eelke
Date: Tue, 28 Nov 2006 11:24
Date: Tue, 28 Nov 2006 11:24
19 lines
1672 bytes
1672 bytes
Boba wrote: > "Eelke" <e.klein@remove.mplussoftware.nl> wrote in message > news:456c0cd1@newsgroups.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
Re: TCriticalSection between 2 threads
Author: Boba
Date: Tue, 28 Nov 2006 12:09
Date: Tue, 28 Nov 2006 12:09
16 lines
1742 bytes
1742 bytes
"Eelke" <e.klein@remove.mplussoftware.nl> wrote in message news:456c1c40$1@newsgroups.borland.com... > 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.
Re: TCriticalSection between 2 threads
Author: Boba
Date: Tue, 28 Nov 2006 12:20
Date: Tue, 28 Nov 2006 12:20
12 lines
1468 bytes
1468 bytes
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message news:456c1c23$1@newsgroups.borland.com... > > "Boba" <Boba@somewhere.world> wrote in message > news:456c1747@newsgroups.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.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads