Writing to Folder current ms problem

Hello!
I have mbed NXP LPC1768.
My program is:

There is a variable that keeps milliseconds which started with mbed.
In While loop I saved it every 4 milliseconds in a buffer.
at the same time, a thread writes this buffer variables to the sd card file.

-Every 4ms ( Must be a multiple of 4 ) I saved it into a buffer.
-When buffer size is up to 50 (and multiple of 50) flag is opened and Thread writing that 50 variables to the sd card file.

-When buffer size is reach 2500 program is over. OK

My problem is when I read variables:
1- First 1000 ms variables normal. (4-8-12-16-20-…-1000) …OK
2- After 1000 It is changing. (1000-1008-1016-1020-1028-1036-1040-1048-1056-1060-…)
It is like pattern (+4 , + 8, +8 ,+4 ,+8 ,+8 , +4…)

Do you have an idea for this reason? It Must be a multiple of 4 from start until end.

THANK YOU!

My code is :

#include “mbed.h”
#include <stdio.h>
#include “SDBlockDevice.h”
#include “FATFileSystem.h”

// Physical block device, can be any device that supports the BlockDevice API xx
SDBlockDevice blockDevice(p5, p6, p7, p10); // mosi, miso, sck, cs

// File system declaration
FATFileSystem fileSystem(“fs”); // fs is root of the SD Card Folder

using namespace std::chrono;

Timer t;
int timer_time;
int err;
FILE *f;
volatile bool running = false;

int first_buffer[2500];
Thread thread;

void file_active()
{
printf(“Opening /fs/tdgdeneme.txt \n”);
f = fopen(“/fs/tdgdeneme.txt”, “r+”);

if (!f) 
{
    printf("No file found, creating a new file...\n ");
    fflush(stdout);
    f = fopen("/fs/tdgdeneme.txt", "a+");
}

fseek(f, 0, SEEK_END);    

}

void Inside_Root() // Display Root of the SD Card
{
int err;
// Display the root directory
printf(“Opening the root directory… “);
fflush(stdout);
DIR *d = opendir(”/fs/”);
printf(“%s\n”, (!d ? “Fail :(” : “OK”));

if (!d) {
    error("error: %s (%d)\n", strerror(errno), -errno);
}
printf("Inside of the root directory:\n");
while (true) {
    struct dirent *e = readdir(d);
    if (!e) {
        break;
    }

    printf("    %s\n", e->d_name);
}
printf("\nClosing the root directory... ");
fflush(stdout);
err = closedir(d);
printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
if (err < 0) {
    error("error: %s (%d)\n", strerror(errno), -errno);
}

}

void Display_Folder()
{
// Display the tdg file
printf(“Opening "/fs/tdg.txt"… “);
fflush(stdout);
f = fopen(”/fs/tdg.txt”, “r”);
printf(“%s\n”, (!f ? “Fail :(” : “OK”));
if (!f) {
error(“error: %s (%d)\n”, strerror(errno), -errno);
}

printf("Inside of the Tdg.txt :\n\n");
while (!feof(f)) {
    int c = fgetc(f);
    printf("%c", c);
}
printf("\rClosing \"/fs/tdg.txt\"... ");
fflush(stdout);
err = fclose(f);
printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
if (err < 0) {
    error("error: %s (%d)\n", strerror(errno), -errno);
}

}

void Write_to_SD()
{ // Open file
printf(“Opening /fs/tdgdeneme.txt \n”);
FILE *f = fopen(“/fs/tdgdeneme.txt”, “r+”);
if (!f) {
printf(“No file found, creating a new file…\n “);
fflush(stdout);
f = fopen(”/fs/tdgdeneme.txt”, “a+”);
}

fseek(f, 0, SEEK_END);


fprintf(f, "Hello World! \n"); // Writing Data to the SD Card

fclose(f); // Close the file which also flushes any cached writes

}

int MountSd()
{
// Try to mount the filesystem
fflush(stdout);
err = fileSystem.mount(&blockDevice);
if (err) {
printf(“Failed to recognize SD card\n”);
return 0;
}
else{
return 1 ;
}

}

void TidyUp()
{
// Tidy up
fflush(stdout);
fileSystem.unmount(); //unmount device
fflush(stdout);
blockDevice.init();
fflush(stdout);
blockDevice.erase(0, blockDevice.size()); //erase block device
fflush(stdout);
blockDevice.deinit(); //deinitialize block device
}

void Threadprint(){
thread_sleep_for(1000);
// Try to mount the filesystem
MountSd();
printf(“Mount successful !\n”);

file_active();

int i = 0;
int b = 0;
while(true)
{
    if(running)
    {
        for(i = 0 ; i<50 ; i++)
        {

        fprintf(f, "%d \n",first_buffer[b]); // Writing Data to the SD Card

        b= b+1;
        } 
        
        running = false;
    }


}

}

int main() {

printf(" --------  TDG --------  \n");


int old = 0;
int counter = 0;
running = false;
bool whilecond = true;
thread.start(Threadprint);


t.start();

while (whilecond)
{


   timer_time = duration_cast<milliseconds>(t.elapsed_time()).count();


        if(timer_time %4 == 0 && timer_time>old)
        {
            first_buffer[counter] = timer_time;
            old = timer_time;
            counter++;

                if(counter %50 == 0)
                {
                    running= true;
                }

        }

        else if(counter >= 2500)
        {
            whilecond = false;
        }

}

t.stop();

fclose(f); // Close the file which also flushes any cached writes
TidyUp();
for(int i=2400 ; i<2500 ; i++)
{
    printf("%d\n",first_buffer[i]);
}
printf("\n --------  End of The Program --------  \n");
return 0;

}