I want to use some temporary storage in a function and not have to reallocate it at each call. This works fine for a single thread, but how do I extend the idea to multi-threaded programs (a subject I'm only familiar with in the abstract).
For example,
int *foo_array;
int foo_array_size;//global initalises to 0
void foo(int *A, int n){
int i;
if(n>foo_array_size && foo_array) free(foo_array);
if(n>foo_array_size){
foo_array=malloc(2*n*sizeof(int));
foo_array_size=2*n;
}
for(i=0;i<n;i++){
foo_array[i]=A[i];
}
for(i=0;i<n;i++){
A[i]=foo_array[i];
}
}
As I understand it, foo_array points to the same place in memory for every thread, so if foo() is called at the same time in two threads, we could end up with unexpected behaviour (correct?).
So how would I, at an abstract level, perhaps, ensure that foo_array points to different memory for use in each thread?