(Assuming you’re referring to C#.)
If you mean Thread
literally, then your answer depends on whether Thread.Join
implicitly generates memory barriers (which, per the answers already given, it probably does).
However, if you mean that Alpha
and Beta
are user-defined tasks executed on a background thread (possibly from the thread pool), and that “waiting” refers to user-level synchronization between the two tasks, then it’s more likely that the data will not be fresh, unless a signaling construct or an explicit barrier is introduced.
Here is a trivial example:
public class Program
{
static string A = "foo";
static volatile bool isAlphaReady = false;
static void Alpha()
{
A = "bar";
isAlphaReady = true;
}
static void Beta()
{
while (!isAlphaReady)
; // Wait by polling
Console.WriteLine(A);
}
static void Main(string[] args)
{
new Thread(Alpha).Start();
new Thread(Beta).Start();
Console.ReadKey();
}
}
Although it appears (intuitively) that Beta
will always output "bar"
as the value of A
, this is not guaranteed on a multiprocessor system with weak synchronization behaviour (such as Itanium), in which case, "foo"
may be output instead.