timer0 in CTC mode, custom code, ISR question

Hi All, there is a bunch of posts on net about using timer0 in CTC mode and they all helping but there are couple things still unclear to me and I cannot quite figure out, can you help understanding please, here is whatsup:

I studied wiring.c from arduino code and if I am not wrong, in the nutshell, the way its done it is that timer0 is running in a mode where it just counts to 256 via prescaler, then overflows and starts over and as it overflows it triggers interrupt inside which essentially global timer0_milliseconds_counter variable gets updated +1 and also every few updates it gets +2 to catch up for some difference because interrupt is happening not exactly at 1ms intervals.
And then there is also "millis()" function that essentially when called returns the value of timer0_millisecond_timer variable

I am writing bare and lean code for my project that uses atmega328p (8mhz external crystal) on breadboard and want to do most of code from scratch, so in order to make my own "millis()" analog so I can measure time passed, I am attempting this:

Manipulate registers so that timer0 is running (via 64prescaler) in CTC mode counting up to the 249 value of compare register, then resets to zero and continues so on and on and on.

Every time compare event happens, the interrupt is triggered and interrupt function will do only one thing, add +1 to the global_milliseconds_counter. And if I ever need to know time passed since beginning I will simply read that global_milliseconds_counter value.

This seem to be working just fine but I currently do no have any other code so I am thinking about long term unexpected surprises that may happen, is there anything I am doing wrong?
Is it okay to directly read state of volatile variable that is updated by timer0's interrupt or I need to also develop analog of Millis function to do so?

I hope it makes sense, any info is welcome, thank s in advance.

Is it okay to directly read state of volatile variable that is updated by timer0's interrupt or I need to also develop analog of Millis function to do so?

If your variable is more than 1 byte in size then it needs to be read in a critical section. You don't want it being updated while you read it so you turn the interrupt off temporarily while reading the value. You can either do that each and every time you need to access it or you can write a function like millis once. I know which sounds easier to me.

This seem to be working just fine but I currently do no have any other code so I am thinking about long term unexpected surprises that may happen, is there anything I am doing wrong?

By switching to CTC mode and changing the TOP value you are also changing how PWM works on pins 5 and 6. So analogWrite isn't going to work as expected anymore.

Delta_G:
If your variable is more than 1 byte in size then it needs to be read in a critical section. You don't want it being updated while you read it so you turn the interrupt off temporarily while reading the value.

Just to confirm, is my understand correct, see below:

If variable (like unsigned long for example) takes 4 bytes in memory then it takes 4 CPU steps to write data into it byte-by-byte and thus it also takes 4 CPU steps to read data from it, so without disabling interrupts while reading variable while from memory while CPU for example got only 2 out of 4 bytes and interrupt takes over and overwrite those 4 bytes in memory, when CPU gets command back and reads last 2 out of 4 bytes, the value will be currupted?

Somehow I was under impression that any variable in memory once started being read by CPU need to finish reading before it can be updated.

alexmg2:
Somehow I was under impression that any variable in memory once started being read by CPU need to finish reading before it can be updated.

That would be an erroneous impression at least for the AVR micros in an Arduino. Only single byte reads are atomic. Everything else needs to be protected.