Say you have a function that counts the occurences of a value in a large array. You want to multithread this function by having each thread count the occurences in its own part of the array, then adding the results together. We can assume that each thread has a unique rank (from 0 to N-1), and that each thread will call the function at about the same time. E.g.:
int count[N]; // global array
int countOccurences()
{
count[rank] = /* count occurences in one part of the array */
// wait until all other threads reached this point
int total = 0;
for (int i=0; i<N; i++)
total += count[i];
return total;
}
The question is, in the above example, could I move the count array into the body of countOccurences() as a static variable? I don’t need it to exist anywhere outside the function body: will a static array be shared among threads?
The answer to your question is yes, threads have access to global data and static data in the same compilation unit, but there is more to this topic about “sharing data between threads” that is important to understand.
For each thread, there is a corresponding function (the “thread function”) that a thread will execute in parallel with other threads. A thread has access to whatever that function can access, either through pointer or reference parameters, global data, static data in the same compilation unit as the thread function, or global and/or static data that is readable/modifiable via other functions that the thread function can call. You should be able to determine every memory region that a given function can read or modify. Those memory regions of a given thread function are exactly the memory regions that the thread has access to.
It is easy to see that global data and static data in the same compilation unit are accessible to a thread function, so that’s why the answer to your question is “yes”.
One thing that you might want to look into is OpenMP, which has built-in constructs for parallelizing a
countOccurrencesoperation (a so-called “reduction”):