Click to See Complete Forum and Search --> : Critical sections


Giuseppe
11-04-2001, 05:19 AM
Hi all!

I have a question about critical sections. Do they protect a section in the
code so that no thread can access it or they just mark that section as accessed
and another thread will not access that same section only if it checks for
this (using EnterCriticalSection)?

CRITICAL_SECTION cs;
int i;
void Thread1(...)// that is pseudo-code
{
EnterCriticalSection(&cs)
i++;
LeaveCriticalSection(&cs)
}
void Thread2(...)
{
EnterCriticalSection(&cs)// we will have to wait until Thread1 finishes
i++;
LeaveCriticalSection(&cs)
}

void Thread3(...)
{
i++;// can we access i here or not (assuming Thread1 uses it)?
}

Thank you!

Boris Karadjov
11-04-2001, 08:30 AM
A critical section does not prevent a thread that does not call
EnterCriticalSection from accessing a resource at the same time another
thread owning the critical section is accessing the same resource. In other
words, synchronization works for threads that call
EnterCriticalSection/LeaveCriticalSection only.
--
Boris Karadjov
Brainbench MVP for Visual C++
http://www.brainbench.com


"Giuseppe" <Giuseppe@hotmail.com> wrote in message
news:3be51645$1@147.208.176.211...
>
> Hi all!
>
> I have a question about critical sections. Do they protect a section in
the
> code so that no thread can access it or they just mark that section as
accessed
> and another thread will not access that same section only if it checks for
> this (using EnterCriticalSection)?
>
> CRITICAL_SECTION cs;
> int i;
> void Thread1(...)// that is pseudo-code
> {
> EnterCriticalSection(&cs)
> i++;
> LeaveCriticalSection(&cs)
> }
> void Thread2(...)
> {
> EnterCriticalSection(&cs)// we will have to wait until Thread1 finishes
> i++;
> LeaveCriticalSection(&cs)
> }
>
> void Thread3(...)
> {
> i++;// can we access i here or not (assuming Thread1 uses it)?
> }
>
> Thank you!
>
>
>
>
>

Danny Kalev
11-04-2001, 08:31 AM
critical sections ensure that a block of code is accessed only by a
single thread at a time. In other words, i can be accessed from every
thread but not simultaneously -- each thread has to wait for its turn to
access it.

Danny

Giuseppe wrote:
>
> Hi all!
>
> I have a question about critical sections. Do they protect a section in the
> code so that no thread can access it or they just mark that section as accessed
> and another thread will not access that same section only if it checks for
> this (using EnterCriticalSection)?
>
> CRITICAL_SECTION cs;
> int i;
> void Thread1(...)// that is pseudo-code
> {
> EnterCriticalSection(&cs)
> i++;
> LeaveCriticalSection(&cs)
> }
> void Thread2(...)
> {
> EnterCriticalSection(&cs)// we will have to wait until Thread1 finishes
> i++;
> LeaveCriticalSection(&cs)
> }
>
> void Thread3(...)
> {
> i++;// can we access i here or not (assuming Thread1 uses it)?
> }
>
> Thank you!

Giuseppe
11-04-2001, 05:26 PM
So, in other words, a thread has to be polite enough to call EnterCriticalSection
in order for that protection mechanism to work. If it does not, two things
can happen:
1. Another thread (which may or may not use EnterCriticalSection) can access
"i".
2. This same thread can access "i" even while "i" is being used and protected
by EnterCriticalSection in another thread. Am I right?
Sorry for that repetition, but I am under attack by coleagues :(!

Thank you again!


"Boris Karadjov" <bbmvp@cpppp.virtualave.net> wrote:
>A critical section does not prevent a thread that does not call
>EnterCriticalSection from accessing a resource at the same time another
>thread owning the critical section is accessing the same resource. In other
>words, synchronization works for threads that call
>EnterCriticalSection/LeaveCriticalSection only.
>--
>Boris Karadjov
>Brainbench MVP for Visual C++
>http://www.brainbench.com
>
>
>"Giuseppe" <Giuseppe@hotmail.com> wrote in message
>news:3be51645$1@147.208.176.211...
>>
>> Hi all!
>>
>> I have a question about critical sections. Do they protect a section in
>the
>> code so that no thread can access it or they just mark that section as
>accessed
>> and another thread will not access that same section only if it checks
for
>> this (using EnterCriticalSection)?
>>
>> CRITICAL_SECTION cs;
>> int i;
>> void Thread1(...)// that is pseudo-code
>> {
>> EnterCriticalSection(&cs)
>> i++;
>> LeaveCriticalSection(&cs)
>> }
>> void Thread2(...)
>> {
>> EnterCriticalSection(&cs)// we will have to wait until Thread1 finishes
>> i++;
>> LeaveCriticalSection(&cs)
>> }
>>
>> void Thread3(...)
>> {
>> i++;// can we access i here or not (assuming Thread1 uses it)?
>> }
>>
>> Thank you!
>>
>>
>>
>>
>>
>
>

Boris Karadjov
11-04-2001, 10:33 PM
Yes, you are right. Think of critical sections as a mechanism for
synchronization (cooperative) rather than protection (automatic). Also note
that typically you use critical sections with multiple threads running the
SAME code. Thus if the code uses critical sections, all the threads do.
--
Boris Karadjov
Brainbench MVP for Visual C++
http://www.brainbench.com


"Giuseppe" <Giuseppe@hotmail.com> wrote in message
news:3be5c08e$1@147.208.176.211...
>
> So, in other words, a thread has to be polite enough to call
EnterCriticalSection
> in order for that protection mechanism to work. If it does not, two things
> can happen:
> 1. Another thread (which may or may not use EnterCriticalSection) can
access
> "i".
> 2. This same thread can access "i" even while "i" is being used and
protected
> by EnterCriticalSection in another thread. Am I right?
> Sorry for that repetition, but I am under attack by coleagues :(!
>
> Thank you again!
>
>
> "Boris Karadjov" <bbmvp@cpppp.virtualave.net> wrote:
> >A critical section does not prevent a thread that does not call
> >EnterCriticalSection from accessing a resource at the same time another
> >thread owning the critical section is accessing the same resource. In
other
> >words, synchronization works for threads that call
> >EnterCriticalSection/LeaveCriticalSection only.
> >--
> >Boris Karadjov
> >Brainbench MVP for Visual C++
> >http://www.brainbench.com
> >
> >
> >"Giuseppe" <Giuseppe@hotmail.com> wrote in message
> >news:3be51645$1@147.208.176.211...
> >>
> >> Hi all!
> >>
> >> I have a question about critical sections. Do they protect a section in
> >the
> >> code so that no thread can access it or they just mark that section as
> >accessed
> >> and another thread will not access that same section only if it checks
> for
> >> this (using EnterCriticalSection)?
> >>
> >> CRITICAL_SECTION cs;
> >> int i;
> >> void Thread1(...)// that is pseudo-code
> >> {
> >> EnterCriticalSection(&cs)
> >> i++;
> >> LeaveCriticalSection(&cs)
> >> }
> >> void Thread2(...)
> >> {
> >> EnterCriticalSection(&cs)// we will have to wait until Thread1
finishes
> >> i++;
> >> LeaveCriticalSection(&cs)
> >> }
> >>
> >> void Thread3(...)
> >> {
> >> i++;// can we access i here or not (assuming Thread1 uses it)?
> >> }
> >>
> >> Thank you!
> >>
> >>
> >>
> >>
> >>
> >
> >
>

julien
11-05-2001, 04:02 AM
If your purpose is to protect data access, use mutex. For each bloc of data
to be protected, associate a mutex to it.

Julien Hamaide

"Boris Karadjov" <bbmvp@cpppp.virtualave.net> wrote:
>Yes, you are right. Think of critical sections as a mechanism for
>synchronization (cooperative) rather than protection (automatic). Also note
>that typically you use critical sections with multiple threads running the
>SAME code. Thus if the code uses critical sections, all the threads do.
>--
>Boris Karadjov
>Brainbench MVP for Visual C++
>http://www.brainbench.com
>
>
>"Giuseppe" <Giuseppe@hotmail.com> wrote in message
>news:3be5c08e$1@147.208.176.211...
>>
>> So, in other words, a thread has to be polite enough to call
>EnterCriticalSection
>> in order for that protection mechanism to work. If it does not, two things
>> can happen:
>> 1. Another thread (which may or may not use EnterCriticalSection) can
>access
>> "i".
>> 2. This same thread can access "i" even while "i" is being used and
>protected
>> by EnterCriticalSection in another thread. Am I right?
>> Sorry for that repetition, but I am under attack by coleagues :(!
>>
>> Thank you again!
>>
>>
>> "Boris Karadjov" <bbmvp@cpppp.virtualave.net> wrote:
>> >A critical section does not prevent a thread that does not call
>> >EnterCriticalSection from accessing a resource at the same time another
>> >thread owning the critical section is accessing the same resource. In
>other
>> >words, synchronization works for threads that call
>> >EnterCriticalSection/LeaveCriticalSection only.
>> >--
>> >Boris Karadjov
>> >Brainbench MVP for Visual C++
>> >http://www.brainbench.com
>> >
>> >
>> >"Giuseppe" <Giuseppe@hotmail.com> wrote in message
>> >news:3be51645$1@147.208.176.211...
>> >>
>> >> Hi all!
>> >>
>> >> I have a question about critical sections. Do they protect a section
in
>> >the
>> >> code so that no thread can access it or they just mark that section
as
>> >accessed
>> >> and another thread will not access that same section only if it checks
>> for
>> >> this (using EnterCriticalSection)?
>> >>
>> >> CRITICAL_SECTION cs;
>> >> int i;
>> >> void Thread1(...)// that is pseudo-code
>> >> {
>> >> EnterCriticalSection(&cs)
>> >> i++;
>> >> LeaveCriticalSection(&cs)
>> >> }
>> >> void Thread2(...)
>> >> {
>> >> EnterCriticalSection(&cs)// we will have to wait until Thread1
>finishes
>> >> i++;
>> >> LeaveCriticalSection(&cs)
>> >> }
>> >>
>> >> void Thread3(...)
>> >> {
>> >> i++;// can we access i here or not (assuming Thread1 uses it)?
>> >> }
>> >>
>> >> Thank you!
>> >>
>> >>
>> >>
>> >>
>> >>
>> >
>> >
>>
>
>

Boris Karadjov
11-05-2001, 07:43 AM
There is no significant difference between mutexes and critical sections except that a mutex can be used across processes and is
capable of detecting if the thread that owns it terminates without releasing it.

If you want to guarantee synchronized access to a resource, wrap it in a class and make all public methods that access the resource
enter a critical section before doing so (and of course leave it after that).
--
Boris Karadjov
Brainbench MVP for Visual C++
http://www.brainbench.com
"julien" <julien_hamaide@hotmail.com> wrote in message news:3be655a1$1@147.208.176.211...
>
>
> If your purpose is to protect data access, use mutex. For each bloc of data
> to be protected, associate a mutex to it.
>
> Julien Hamaide
>
> "Boris Karadjov" <bbmvp@cpppp.virtualave.net> wrote:
> >Yes, you are right. Think of critical sections as a mechanism for
> >synchronization (cooperative) rather than protection (automatic). Also note
> >that typically you use critical sections with multiple threads running the
> >SAME code. Thus if the code uses critical sections, all the threads do.
> >--
> >Boris Karadjov
> >Brainbench MVP for Visual C++
> >http://www.brainbench.com
> >
> >
> >"Giuseppe" <Giuseppe@hotmail.com> wrote in message
> >news:3be5c08e$1@147.208.176.211...
> >>
> >> So, in other words, a thread has to be polite enough to call
> >EnterCriticalSection
> >> in order for that protection mechanism to work. If it does not, two things
> >> can happen:
> >> 1. Another thread (which may or may not use EnterCriticalSection) can
> >access
> >> "i".
> >> 2. This same thread can access "i" even while "i" is being used and
> >protected
> >> by EnterCriticalSection in another thread. Am I right?
> >> Sorry for that repetition, but I am under attack by coleagues :(!
> >>
> >> Thank you again!
> >>
> >>
> >> "Boris Karadjov" <bbmvp@cpppp.virtualave.net> wrote:
> >> >A critical section does not prevent a thread that does not call
> >> >EnterCriticalSection from accessing a resource at the same time another
> >> >thread owning the critical section is accessing the same resource. In
> >other
> >> >words, synchronization works for threads that call
> >> >EnterCriticalSection/LeaveCriticalSection only.
> >> >--
> >> >Boris Karadjov
> >> >Brainbench MVP for Visual C++
> >> >http://www.brainbench.com
> >> >
> >> >
> >> >"Giuseppe" <Giuseppe@hotmail.com> wrote in message
> >> >news:3be51645$1@147.208.176.211...
> >> >>
> >> >> Hi all!
> >> >>
> >> >> I have a question about critical sections. Do they protect a section
> in
> >> >the
> >> >> code so that no thread can access it or they just mark that section
> as
> >> >accessed
> >> >> and another thread will not access that same section only if it checks
> >> for
> >> >> this (using EnterCriticalSection)?
> >> >>
> >> >> CRITICAL_SECTION cs;
> >> >> int i;
> >> >> void Thread1(...)// that is pseudo-code
> >> >> {
> >> >> EnterCriticalSection(&cs)
> >> >> i++;
> >> >> LeaveCriticalSection(&cs)
> >> >> }
> >> >> void Thread2(...)
> >> >> {
> >> >> EnterCriticalSection(&cs)// we will have to wait until Thread1
> >finishes
> >> >> i++;
> >> >> LeaveCriticalSection(&cs)
> >> >> }
> >> >>
> >> >> void Thread3(...)
> >> >> {
> >> >> i++;// can we access i here or not (assuming Thread1 uses it)?
> >> >> }
> >> >>
> >> >> Thank you!
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >
> >> >
> >>
> >
> >
>

Ted
11-05-2001, 12:36 PM
I hate to disagree but I must. There is a signicant difference between mutexes
and critical sections. Mutexes can be used cross process because they are
kernel objects. There is overhead in the usage of mutexes and critical sections
perform slightly better in process. If you are dealing with code that needs
to perform you need to rethink your usage of Mutexes vs. Critical sections.
So if you are only talking of interprocess communication, use a Critical
Section.


"Boris Karadjov" <bbmvp@cpppp.virtualave.net> wrote:
>There is no significant difference between mutexes and critical sections
except that
>a mutex can be used across processes and is
>capable of detecting if the thread that owns it terminates without releasing
it.
>
>If you want to guarantee synchronized access to a resource, wrap it in a
class and make
>all public methods that access the resource
>enter a critical section before doing so (and of course leave it after that).
>--
>Boris Karadjov
>Brainbench MVP for Visual C++
>http://www.brainbench.com
>"julien" <julien_hamaide@hotmail.com> wrote in message news:3be655a1$1@147.208.176.211...
>>
>>
>> If your purpose is to protect data access, use mutex. For each bloc of
data
>> to be protected, associate a mutex to it.
>>
>> Julien Hamaide
>>
>> "Boris Karadjov" <bbmvp@cpppp.virtualave.net> wrote:
>> >Yes, you are right. Think of critical sections as a mechanism for
>> >synchronization (cooperative) rather than protection (automatic). Also
note
>> >that typically you use critical sections with multiple threads running
the
>> >SAME code. Thus if the code uses critical sections, all the threads do.
>> >--
>> >Boris Karadjov
>> >Brainbench MVP for Visual C++
>> >http://www.brainbench.com
>> >
>> >
>> >"Giuseppe" <Giuseppe@hotmail.com> wrote in message
>> >news:3be5c08e$1@147.208.176.211...
>> >>
>> >> So, in other words, a thread has to be polite enough to call
>> >EnterCriticalSection
>> >> in order for that protection mechanism to work. If it does not, two
things
>> >> can happen:
>> >> 1. Another thread (which may or may not use EnterCriticalSection) can
>> >access
>> >> "i".
>> >> 2. This same thread can access "i" even while "i" is being used and
>> >protected
>> >> by EnterCriticalSection in another thread. Am I right?
>> >> Sorry for that repetition, but I am under attack by coleagues :(!
>> >>
>> >> Thank you again!
>> >>
>> >>
>> >> "Boris Karadjov" <bbmvp@cpppp.virtualave.net> wrote:
>> >> >A critical section does not prevent a thread that does not call
>> >> >EnterCriticalSection from accessing a resource at the same time another
>> >> >thread owning the critical section is accessing the same resource.
In
>> >other
>> >> >words, synchronization works for threads that call
>> >> >EnterCriticalSection/LeaveCriticalSection only.
>> >> >--
>> >> >Boris Karadjov
>> >> >Brainbench MVP for Visual C++
>> >> >http://www.brainbench.com
>> >> >
>> >> >
>> >> >"Giuseppe" <Giuseppe@hotmail.com> wrote in message
>> >> >news:3be51645$1@147.208.176.211...
>> >> >>
>> >> >> Hi all!
>> >> >>
>> >> >> I have a question about critical sections. Do they protect a section
>> in
>> >> >the
>> >> >> code so that no thread can access it or they just mark that section
>> as
>> >> >accessed
>> >> >> and another thread will not access that same section only if it
checks
>> >> for
>> >> >> this (using EnterCriticalSection)?
>> >> >>
>> >> >> CRITICAL_SECTION cs;
>> >> >> int i;
>> >> >> void Thread1(...)// that is pseudo-code
>> >> >> {
>> >> >> EnterCriticalSection(&cs)
>> >> >> i++;
>> >> >> LeaveCriticalSection(&cs)
>> >> >> }
>> >> >> void Thread2(...)
>> >> >> {
>> >> >> EnterCriticalSection(&cs)// we will have to wait until Thread1
>> >finishes
>> >> >> i++;
>> >> >> LeaveCriticalSection(&cs)
>> >> >> }
>> >> >>
>> >> >> void Thread3(...)
>> >> >> {
>> >> >> i++;// can we access i here or not (assuming Thread1 uses it)?
>> >> >> }
>> >> >>
>> >> >> Thank you!
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >>
>> >
>> >
>>
>
>

Giuseppe
11-05-2001, 04:18 PM
Thank you very much all!

Dick Adams
11-07-2001, 02:53 PM
Some clarifications:

1. Because mutexes involve kernel access, there is a large performance hit,
and you should use them it only for inter-process synchronization, NOT within
a single program. For synchronization inside a program, a critial section
is a much better choice.

2. Threads don't HAVE to necessarily cooperate to access a piece of data,
if you structure it right. I usually embed the protected data within a class
that provides a member function, and the MEMBER function uses criticalsection
to access the data. That way, threads don't have any choice: they go thru
the criticalsection object whether they want to or not. The protected resource
isn't available any other way.

"julien" <julien_hamaide@hotmail.com> wrote:
>
>
>If your purpose is to protect data access, use mutex. For each bloc of
data
>to be protected, associate a mutex to it.
>
>Julien Hamaide
>
>"Boris Karadjov" <bbmvp@cpppp.virtualave.net> wrote:
>>Yes, you are right. Think of critical sections as a mechanism for
>>synchronization (cooperative) rather than protection (automatic). Also
note
>>that typically you use critical sections with multiple threads running
the
>>SAME code. Thus if the code uses critical sections, all the threads do.
>>--
>>Boris Karadjov
>>Brainbench MVP for Visual C++
>>http://www.brainbench.com
>>
>>
>>"Giuseppe" <Giuseppe@hotmail.com> wrote in message
>>news:3be5c08e$1@147.208.176.211...
>>>
>>> So, in other words, a thread has to be polite enough to call
>>EnterCriticalSection
>>> in order for that protection mechanism to work. If it does not, two things
>>> can happen:
>>> 1. Another thread (which may or may not use EnterCriticalSection) can
>>access
>>> "i".
>>> 2. This same thread can access "i" even while "i" is being used and
>>protected
>>> by EnterCriticalSection in another thread. Am I right?
>>> Sorry for that repetition, but I am under attack by coleagues :(!
>>>
>>> Thank you again!
>>>
>>>
>>> "Boris Karadjov" <bbmvp@cpppp.virtualave.net> wrote:
>>> >A critical section does not prevent a thread that does not call
>>> >EnterCriticalSection from accessing a resource at the same time another
>>> >thread owning the critical section is accessing the same resource. In
>>other
>>> >words, synchronization works for threads that call
>>> >EnterCriticalSection/LeaveCriticalSection only.
>>> >--
>>> >Boris Karadjov
>>> >Brainbench MVP for Visual C++
>>> >http://www.brainbench.com
>>> >
>>> >
>>> >"Giuseppe" <Giuseppe@hotmail.com> wrote in message
>>> >news:3be51645$1@147.208.176.211...
>>> >>
>>> >> Hi all!
>>> >>
>>> >> I have a question about critical sections. Do they protect a section
>in
>>> >the
>>> >> code so that no thread can access it or they just mark that section
>as
>>> >accessed
>>> >> and another thread will not access that same section only if it checks
>>> for
>>> >> this (using EnterCriticalSection)?
>>> >>
>>> >> CRITICAL_SECTION cs;
>>> >> int i;
>>> >> void Thread1(...)// that is pseudo-code
>>> >> {
>>> >> EnterCriticalSection(&cs)
>>> >> i++;
>>> >> LeaveCriticalSection(&cs)
>>> >> }
>>> >> void Thread2(...)
>>> >> {
>>> >> EnterCriticalSection(&cs)// we will have to wait until Thread1
>>finishes
>>> >> i++;
>>> >> LeaveCriticalSection(&cs)
>>> >> }
>>> >>
>>> >> void Thread3(...)
>>> >> {
>>> >> i++;// can we access i here or not (assuming Thread1 uses it)?
>>> >> }
>>> >>
>>> >> Thank you!
>>> >>
>>> >>
>>> >>
>>> >>
>>> >>
>>> >
>>> >
>>>
>>
>>
>