When are writes that are performed by one thread visible to a different thread when there are no synchronized blocks and no volatile variables? Here is a simplified quicksort example:
int middle = partitionForTheFirstTime(array);
Thread t = new Thread(new Quicksorter(array, 0, middle - 1));
Thread u = new Thread(new Quicksorter(array, middle + 1, array.size - 1));
t.start()
u.start();
t.join();
u.join();
(For the sake of simplicity, assume that the two "worker threads" do not spawn any additional threads.)
Does joining with the two threads guarantee that the current thread sees all their side effects?
On a related note, what would happen if I created the threads before the initial partitioning?
Quicksorter a = new Quicksorter();
Quicksorter b = new Quicksorter();
Thread t = new Thread(a);
Thread u = new Thread(b);
int middle = partitionForTheFirstTime(array);
a.setParameters(array, 0, middle - 1);
b.setParameters(array, middle + 1, array.size - 1);
t.start()
u.start();
t.join();
u.join();
Will the two threads be able to see the side effects caused by partitionForTheFirstTime()
? In other words, does creating a Thread incur a happens-before relationship, or does starting a Thread?