|
Listing 5-11 extracted from chapter
Processes, threads and synchronization
Listing 5-10< > Listing 5-12
This listing can be compiled with the command line: csc.exe /target:exe Example_5_11.cs Errors: 0 Warnings: 0
Example_5_11.cs
using System.Threading;
class Program {
private static object staticSyncRoot = new object();
static void Main() {
// Comment this line to test the case where the
// 'TryEnter()' method returns true.
Monitor.Enter( staticSyncRoot );
Thread t1 = new Thread(f1);
t1.Start(); t1.Join();
}
static void f1() {
if( ! Monitor.TryEnter( staticSyncRoot ) )
return;
try {
// ...
}
finally {
Monitor.Exit( staticSyncRoot );
}
}
}
Copyright Patrick Smacchia 2006 2007
|