473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Problem with volatile modifier

Hi all,

I have a problem with 'volatile' use in C++.

The function get_clocks() below tries to use a 16 bit hardware counter to
count time. The counter overflows very often. But an interrupt is generated
everytime the counter overflows, and function handler() counts these
overflows in variable n_overflows.

The problem is that during calculation of time value in get_clocks()
function (marked by *** in the code), an overflow interrupt can happen
modifying n_overflows value. This will render the calculation useless
because value read from timer and n_overflows would not correspond to the
same point of time. To prevent that I use another variable called flag. The
flag is set by the interrupt handler to inform get_clocks function that
overflow occured. The function then retries the time calculation by
rereading values from the timer (the hardware timer counter is mapped to
memory address 0x10000000).

The problem is that it does not work. Sometimes time value returned from
get_clocks() is less than the previously acquired (by roughly 2^16 what
indicates a problem with n_overflow counter). I suspect there might be
something wrong with my use of volatile modifier.

Here's the relevant code:

volatile unsigned long n_overflows;
volatile bool flag;

// Overflow interrupt handler
void handler() {
++n_overflows;
flag = true;
}

// Function to get the number of clock ticks passed
unsigned long get_clocks() {
unsigned long r;
do {
flag = false;
r = n_overflows << 16 +
*(volatile unsigned short *)0x10000000; // ***
} while (flag);
return r;
}

thanks,
Marcin
Jul 22 '05 #1
6 1907
> r = n_overflows << 16 +
*(volatile unsigned short *)0x10000000; // ***


This piece of code is actually:

r = (n_overflows << 16) + *(volatile unsigned short *)0x10000000;

so there is no problem with operator precedence. I have removed the
parentheses accidentally when formatting the code.

Best regards,
Marcin
Jul 22 '05 #2
Marcin Kalicinski wrote:

Hi all,
The problem is that it does not work. Sometimes time value returned from
get_clocks() is less than the previously acquired (by roughly 2^16 what
indicates a problem with n_overflow counter). I suspect there might be
something wrong with my use of volatile modifier.


volatile has nothing to do with it.
Your problem is that there are 2 tasks accessing the same variables
variables. You need some synchronization mechanism to control that.
Standard C++ has no provisions for that. BUt youcan check your
development environment for methods of thread synchonization.
Keywords would be: critical section, semaphore, mutex
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3
Karl Heinz Buchegger wrote:

Marcin Kalicinski wrote:

Hi all,
The problem is that it does not work. Sometimes time value returned from
get_clocks() is less than the previously acquired (by roughly 2^16 what
indicates a problem with n_overflow counter). I suspect there might be
something wrong with my use of volatile modifier.


volatile has nothing to do with it.
Your problem is that there are 2 tasks accessing the same variables
variables. You need some synchronization mechanism to control that.
Standard C++ has no provisions for that. But you can check your
development environment for methods of thread synchonization.
Keywords would be: critical section, semaphore, mutex


On a rethink I no longer think this will lead you somewhere.
I expect your problem to be a hardware and/or operating system
problem. The counter is reset to 0, the interrupt gets generated
but is pending. So you sometimes end up with: the clock has
already been reset to 0, but the interrupt hasn't been processed
right now to increment no_overflows. What you need to achive
is to make the hardware increment paired with interrupt
handling and an atomic operation.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:cj**********@korweta.task.gda.pl...
Hi all,

I have a problem with 'volatile' use in C++.

The function get_clocks() below tries to use a 16 bit hardware counter to
count time. The counter overflows very often. But an interrupt is generated everytime the counter overflows, and function handler() counts these
overflows in variable n_overflows.

The problem is that during calculation of time value in get_clocks()
function (marked by *** in the code), an overflow interrupt can happen
modifying n_overflows value. This will render the calculation useless
because value read from timer and n_overflows would not correspond to the
same point of time. To prevent that I use another variable called flag. The flag is set by the interrupt handler to inform get_clocks function that
overflow occured. The function then retries the time calculation by
rereading values from the timer (the hardware timer counter is mapped to
memory address 0x10000000).

The problem is that it does not work. Sometimes time value returned from
get_clocks() is less than the previously acquired (by roughly 2^16 what
indicates a problem with n_overflow counter). I suspect there might be
something wrong with my use of volatile modifier.

Here's the relevant code:

volatile unsigned long n_overflows;
volatile bool flag;

// Overflow interrupt handler
void handler() {
++n_overflows;
flag = true;
}

// Function to get the number of clock ticks passed
unsigned long get_clocks() {
unsigned long r;
do {
flag = false;
r = n_overflows << 16 +
*(volatile unsigned short *)0x10000000; // ***
} while (flag);
return r;
}

thanks,
Marcin


This is a common problem (trying to read a clock and its roll over count
atomically_, and your approach is flawed, as you have two threads of
execution trying to update flag. What you have to do is read both in a
loop, exiting when two consective reads are the same for both, then you know
that reads are not out of sync. with each other (one didn't change while you
read the other).

Jul 22 '05 #5

"Xenos" <do**********@spamhate.com> wrote in message
news:cj*********@cui1.lmms.lmco.com...
This is a common problem (trying to read a clock and its roll over count
atomically_, and your approach is flawed, as you have two threads of
execution trying to update flag. What you have to do is read both in a
loop, exiting when two consective reads are the same for both, then you know that reads are not out of sync. with each other (one didn't change while you read the other).

Here is an example:

int clock, rolls;
do
{
clock = read_clock();
rolls = read_rolls();
} while (clock != read_clock());

Jul 22 '05 #6
> int clock, rolls;
do
{
clock = read_clock();
rolls = read_rolls();
} while (clock != read_clock());


This approach will create an infinite loop because clock is updated by
hardware very often (roughly at the speed of CPU clock) and will certainly
change between two read_clock() calls in the above loop. So, the condition
in while will always be met.
atomically_, and your approach is flawed, as you have two threads of
execution trying to update flag. What you have to do is read both in a
loop, exiting when two consective reads are the same for both, then


No, I don't have threads. The situation is different because interrupts are
synchronous unlike threads. If interrupt handler is executing, the main
program is stopped until the handler finishes. Handler cannot be interrupted
in the middle (at least not to return control back to main program).

What I think now is that my approach to use this little hardware clock as a
long-term timer is doomed, because from time to time other interrupts with
greater priority happen, and prevent calling of handler() on clock overflow.
Thus, the clock misses some overflows.

Best regards,
Marcin
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andrew | last post by:
Section 17.4.3 of the ECMA-334 C# Language Specification says 1 When a field-declaration includes a volatile modifier, the fields introduced by that declaration are volatile fields. 2 For...
5
by: ben | last post by:
Hello All, I am trying to make sense of a bit of syntax, is there a guru out there that can clear this up for me. I have a buffer declared as static volatile u8 buffer; and I have a...
11
by: Ian Bell | last post by:
Apologies for the last post; got the topic wrong. What is the current thinking on the best way to solve the shared data problem i.e. accesssing a shared variable in a non atomic way can give...
14
by: Ian Pilcher | last post by:
It's pretty common to see declarations such as: static volatile sig_atomic_t caught_signal = 0; C99 defines sig_atomic_t as a "... (possibly volatile-qualified) integer type of an object that...
14
by: google-newsgroups | last post by:
Hello, even (or because?) reading the standard (ISO/IEC 9899/1999) I do not understand some issues with volatile. The background is embedded programming where data is exchanged between main...
22
by: Assaf | last post by:
hi all i know that i should not cross-post, but i am not sure to which group to post this question. 2 quesions about volatile: 1. i use volatile when 2 threads access the same variable...
14
by: Pierre | last post by:
Using the "volatile" keyword, creates a problem if I intend to use any of the interlocked APIs. The compiler generates an error if I use the following line, for example: ...
13
by: yaron | last post by:
Hi all, let be focus on sigle processor machine 32 bits. 1. with multi-threaded on single processor machine 32bit do i have to sync access to atomic get/set properties of type less then 32 bits...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.