C# 8.0 in a Nutshell by Joseph Albahari

C# 8.0 in a Nutshell by Joseph Albahari

Author:Joseph Albahari
Language: eng
Format: epub
Publisher: O'Reilly Media
Published: 2020-05-12T00:00:00+00:00


static void Main() { bool done = false; ThreadStart action = () => { if (!done) { done = true; Console.WriteLine ("Done"); } }; new Thread (action).Start(); action(); }

Static fields offer another way to share data between threads:

class ThreadTest { static bool _done; // Static fields are shared between all threads // in the same application domain. static void Main() { new Thread (Go).Start(); Go(); } static void Go() { if (!_done) { _done = true; Console.WriteLine ("Done"); } } }

All three examples illustrate another key concept: that of thread safety (or rather, lack of it!). The output is actually indeterminate: it’s possible (though unlikely) that “Done” could be printed twice. If, however, we swap the order of statements in the Go method, the odds of “Done” being printed twice go up dramatically:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.