Reputation: 584
The title is the question: when a thread exits, does its cached memory get flushed to the main memory?
I am wondering because cases are common where the main thread creates some threads, they do some work on independent parts of the array (no data dependencies between each other), the main thread joins all the worker threads, then does more calculations with the array values that result from the worker threads computations. Do the arrays need to be declared volatile
for the main thread to see the side-effects on it?
Upvotes: 2
Views: 851
Reputation: 239011
The pthreads specification requires that pthread_join()
is one of the functions that "synchronizes memory with respect to other threads", so in the case of pthreads you are OK - after pthread_join()
has returned, the main thread will see all updates to shared memory made by the joined thread.
Upvotes: 1
Reputation: 379
Assuming you are doing this in C, and if the array is global or you have passed a structure to the threads which contains the indices on which the threads need to do the computation on and a pointer to the array, then the array need not be volatile for the main thread to see the changes since the array memory is shared between the worker threads and the main thread.
Upvotes: 0