Static variable in C have multiple individual copies via DPI-C

Hi,

Can someone help to solve a issue that keeping multiple individual copies for static variable in C through DPI-C calls?
Basically, for static variable is stored in the statically allocated memory, any method try to modify the copy will reflect in the memory. Is there a way to allow user to maintain multiple copies for static variable?

In the following code, how to keep static variable - miles as is, and function runner1 modify one copy, function runner2 modify another copy? The intention is trying to reuse the C code as much as possible(not to change is the best), and solve this issue.

Here is the code:

In C side:
test.h

static int miles;
extern int get_miles();

test.c

#include "test.h"

int get_miles() {
   miles = miles + 1;
   return miles;
}

user.c

#include <stdio.h>
#include "test.h"

extern void runner1();
extern void runner2();

void runner1() {
   int i;
   for(i=0;i<5;i++) {
      printf("runner1 runs %0d miles\n", get_miles());
   }
}

void runner2() {
   int j;
   for(j=0;j<5;j++) {
      printf("runner2 runs %0d miles\n", get_miles());
   }
}

In SV side:

module tb;

import "DPI-C" context function void runner1();
import "DPI-C" context function void runner2();

initial begin
   fork
      runner1();
      runner2();
   join
   $finish();
end
  
endmodule

In reply to mlsxdx:

A static variable is global. If you do not want that behavior, then do not use a static variable. You need to change your code to

int get_miles(const int *miles) {
   *miles = *miles + 1;
   return *miles;
}

void runner1() {
   int i, miles;
   for(i=0;i<5;i++) {
      printf("runner1 runs %0d miles\n", get_miles(&miles));
   }
}

void runner2() {
   int j, miles;
   for(j=0;j<5;j++) {
      printf("runner2 runs %0d miles\n", get_miles(&miles));
   }
}

In reply to dave_59:

Thanks for your reply, dave.
That is one way to fix this. It seems that all the places using that static valuable need to use its own copy.

Because the posted code is an simplified example, so we can easily to change it in two functions where it is called. In real case, a lot of dependencies in C side on that static variable. If it is changed, it means a lot of work need to be done for that change. Is there a mechanism that allow to dynamically change static to dynamic in run-time?

In reply to mlsxdx:

As far as I know, there is no feature in C (or any other widely available language) that lets you change the declaration of a variable at run time.