.


:




:

































 

 

 

 


Using System.Runtime.CompilerServices;




// TickTock, // MethodlmplOptions.Synchronized, class TickTock {

/* Tick(). */ [MethodlmplAttribute(MethodlmplOptions.Synchronized)] public void Tick (bool running) { if(!running) { //

Monitor.Pulse(this); // return;

}

Console.Write(" ");

Monitor.Pulse(this); // Tock()

Monitor.Wait(this); // Tock()

}

/* (). */ [MethodlmplAttribute(MethodlmplOptions.Synchronized)] public void Tock(bool running) { if (!running) { //

Monitor.Pulse(this); // return;

}'

Console.WriteLine("");

Monitor.Pulse(this); // Tick()

Monitor.Wait(this); // Tick()

}

}

class MyThread {

Public Thread Thrd;

TickTock ttOb;

// .

public MyThread(string name, TickTock tt) {

Thrd = new Thread(this.Run); ttOb = tt;

Thrd.Name = name;

Thrd.Start();

}

// , void Run() {

if(Thrd.Name == "Tick") {

for(int i=0; i<5; i++) ttOb.Tick(true); ttOb.Tick(false);

}

else {

for(int i=0; i<5; i++) ttOb.Tock(true); ttOb.Tock(false);

}

}

}

class TickingClock { static void Main() {

TickTock tt = new TickTock();

MyThread mtl = new MyThread("Tick", tt);

MyThread mt2 = new MyThread("Tock", tt);

Mtl.Thrd.Join(); mt2.Thrd.Join();

Console.WriteLine(" ");

}

}

, . , lock MethodlmplAttribute . . lock #, , , .

MethodlmplAttribute . lock, , .

, , lock. , , , , , .NET Framework. : .

. , . , . , , . .

System. Threading.Mutex. . .

Public Mutex()

public Mutex(bool initiallyOwned)

, . , ini tiallyOwned true. .

, WaitOne () . WaitOne () Mutex Thread. WaitHandle. .

Public bool WaitOne ();

WaitOne () , , . , , . true.





:


: 2016-12-03; !; : 376 |


:

:

, .
==> ...

833 - | 667 -


© 2015-2024 lektsii.org - -

: 0.012 .