i want use temporary storage in function , not have reallocate @ each call. works fine single thread, how extend idea multi-threaded programs (a subject i'm familiar in abstract).
for example,
int *foo_array; int foo_array_size;//global initalises 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 understand it, foo_array points same place in memory every thread, if foo() called @ same time in 2 threads, end unexpected behaviour (correct?).
so how i, @ abstract level, perhaps, ensure foo_array points different memory use in each thread?
you don't thread library using. c11 thread library has qualifier _thread_local
creates variable has different incarnation per thread.
if don't have c11 threads, compiler extensions may provide similar feature. e.g gcc
has __thread
.
according thread library using there may set of functions simulate such thing. c11 threads have tss_t
type (thread specific storage) , functions tss_create
etc deal them.
Comments
Post a Comment