1

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?

Alejandro
  • 623
  • 6
  • 16
  • Have you looked at this question: http://stackoverflow.com/questions/21015738/ ? – Mats Aug 01 '16 at 13:49
  • 1
    You have to design your code with "private pointers" one per thread. Just off the top of my head: using array of pointers, one element per thread. Same thing for size variable – LPs Aug 01 '16 at 13:49
  • Use an array of `int *` (`int *foo_array[]`). Pass the index that the thread should use on creation: `int pthread_create(..., ..., ..., &index)`. – Fiddling Bits Aug 01 '16 at 13:50
  • Side note: you probably can avoid to continuously `free` and `malloc` new memory using `realloc` instead. Useful if data `A` array @ t-1 still the same @ t. If so pointers must be init to `NULL`; – LPs Aug 01 '16 at 14:04
  • 1
    Do you need an array for every thread or do you want multiple threads to operate on a single array safely? Former is quite simple using the strategy @FiddlingBits indicated. The second option is slightly more difficult for fixed sized arrays and much more difficult, if you want to incorporate the reallocation of the array. – exilit Aug 05 '16 at 12:20
  • One array for each thread, that is populated and used within each call to foo. I think I understand the answers and comments now, thanks. (actually, if the comments were combined with the answer, I would be able to accept) – Alejandro Aug 05 '16 at 12:23

1 Answers1

0

You don't say with thread library you are using. The C11 thread library has the qualifier _Thread_local that creates a variable that has a different incarnation per thread.

If you don't have C11 threads, some compiler extensions may provide a similar feature. E.g gcc has __thread.

According to the thread library that you are using there may also be a set of functions that simulate such a thing. C11 threads have the tss_t type (thread specific storage) and functions tss_create etc to deal with them.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • I don't know anything about specific thread libraries, but if you expand on the concepts I might be able to select your answer. LPs and Fiddling Bits' comments are understandable, but are they the same idea as using the thread library you are referring to? – Alejandro Aug 03 '16 at 14:01
  • @Alejandro, no their ideas are merely on how to implement such things yourself. You still don't tell us what your platform, OS, compiler ... is. Please amend your question accordingly. – Jens Gustedt Aug 03 '16 at 14:24
  • There is no OS, platform, compiler or anything. It's just a conceptual question :) – Alejandro Aug 03 '16 at 15:17
  • @Alejandro, in fact at least part of it is covered in the SO documentation for the "tag" C: https://stackoverflow.com/documentation/c/topics – Jens Gustedt Aug 03 '16 at 20:17