Answered by:
Critical Section

Question
-
Hi,
We are using WindowsCE 5.0.
- In one thread we are reading the variable. In another thread the variable is modified.
I am aware that when the variable is modified in two threads then it is required to use the critical section.
Is it required to use the critical section in step 1 also?
If you have any resources please point me to that. I am happy to read that.
Thank You & Regards,
GSR
Wednesday, January 22, 2014 4:03 PM
Answers
-
The issue is related to the variable type and data alignment in memory.
Tipically atomicity is guarateed for aligned data. If a CPU has 32 bit registers, it can access a 32 bit value in memory to put it inside a register in atomic manner. If you have a 64 bit value, it is across two 32 bit memory location boundaries and CPU need two cycles to get it and put inside register; in this case the thread that is reading/writing the 64 bit value could be interrupted (a context switch occur) and don't complete the operation.
In this case if the thread that read the value reads the first 32 bit and it is interrupted, the second thread start and write the 64 bit value. When the first read thread is rescheduled, it will read the next 32 bit but from the last updated value and not the initial value. So you will have a 64 bit composed of : first 32 bit precedent value, second 32 new updated value...garbage !! not the real actual value of the variable.
It can work if your variable is 32 bit (not 64) but tipically it is better use Always critical section.
I hope I explained well... :-)
Paolo Patierno
- Proposed as answer by Paolo PatiernoMVP Thursday, January 23, 2014 5:14 PM
- Marked as answer by GSRid Thursday, January 23, 2014 6:54 PM
Thursday, January 23, 2014 3:41 PM
All replies
-
Tipically if you have a resource (a variable, ...) that can be accessed from two or more threads simultaniously, if you want to avoid incosistent read and write operation, you have to use a critical section around the resource.
It could be possibile that while the write thread is updating the variable, it is paused from scheduler to switch another thread. The thread could update partially the variable and in this case the read thread could read an inconsistent value inside it.
Paolo.
Paolo Patierno
- Proposed as answer by Paolo PatiernoMVP Thursday, January 23, 2014 7:00 AM
Wednesday, January 22, 2014 4:36 PM -
Hi Paolo Patierno,
Thank You for the reply and your valuable time.
Regards,
GSR
Wednesday, January 22, 2014 7:14 PM -
You are welcome !
If you think that my reply solved your problem, mark my reply as answer so that it can be useful for other people with the same question.
Paolo.
Paolo Patierno
Wednesday, January 22, 2014 7:32 PM -
Hi Paolo,
I understand what you are telling, but I am trying to see at assembly level and understand is it really requires?
Let's say in one thread I have like
x = y;
In another thread Y is updated.
as per my understanding the chances are that x might be uploaded with a previous value instead of a latest value. Which we refer as a garbage is that correct?
Any other issues will occur in this scenario when we do not use critical section.
Regards,
GSR
Thursday, January 23, 2014 3:23 PM -
The issue is related to the variable type and data alignment in memory.
Tipically atomicity is guarateed for aligned data. If a CPU has 32 bit registers, it can access a 32 bit value in memory to put it inside a register in atomic manner. If you have a 64 bit value, it is across two 32 bit memory location boundaries and CPU need two cycles to get it and put inside register; in this case the thread that is reading/writing the 64 bit value could be interrupted (a context switch occur) and don't complete the operation.
In this case if the thread that read the value reads the first 32 bit and it is interrupted, the second thread start and write the 64 bit value. When the first read thread is rescheduled, it will read the next 32 bit but from the last updated value and not the initial value. So you will have a 64 bit composed of : first 32 bit precedent value, second 32 new updated value...garbage !! not the real actual value of the variable.
It can work if your variable is 32 bit (not 64) but tipically it is better use Always critical section.
I hope I explained well... :-)
Paolo Patierno
- Proposed as answer by Paolo PatiernoMVP Thursday, January 23, 2014 5:14 PM
- Marked as answer by GSRid Thursday, January 23, 2014 6:54 PM
Thursday, January 23, 2014 3:41 PM -
Hi Paolo,
Very well explained.
Thank You Very Much for the reply and your time.
Regards,
GSR
Thursday, January 23, 2014 5:13 PM -
You are welcome !
If you think that my reply solved your problem, mark my reply as answer (not only useful) so that it can be useful for other people with the same question.
Paolo.
Paolo Patierno
- Edited by Paolo PatiernoMVP Thursday, January 23, 2014 5:15 PM
Thursday, January 23, 2014 5:15 PM