| View previous topic :: View next topic |
| Author |
Message |
Tumurbaatar S. Guest
|
Posted: Tue Nov 28, 2006 9:10 am Post subject: TCriticalSection between 2 threads |
|
|
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
|
Posted: Tue Nov 28, 2006 4:18 pm Post subject: Re: TCriticalSection between 2 threads |
|
|
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
|
Posted: Tue Nov 28, 2006 5:01 pm Post subject: Re: TCriticalSection between 2 threads |
|
|
"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
|
Posted: Tue Nov 28, 2006 5:18 pm Post subject: Re: TCriticalSection between 2 threads |
|
|
"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
|
Posted: Tue Nov 28, 2006 5:24 pm Post subject: Re: TCriticalSection between 2 threads |
|
|
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
|
Posted: Tue Nov 28, 2006 6:09 pm Post subject: Re: TCriticalSection between 2 threads |
|
|
"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
|
Posted: Tue Nov 28, 2006 6:20 pm Post subject: Re: TCriticalSection between 2 threads |
|
|
"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 |
|
 |
|