1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

. , , . , , . , .

, . ( ) . . , . , . , , . , , .

, , . , .

System: :Threading: :Thread (:: -::) . Thread (), ,

Thread: -.CurrentThread.

, - ,

-, . , , , . . - - , .

, Threading ( )14, Customer () Hotel (). , . , 0.

.NET , ThreadStart. (void) .

public_gc_delegate void ThreadStart ();

// ࠠ - ThreadStart;);

14 (DLL) Customer () Hotel () Debug () TestThreading. NewReservation (public) MakeReservation, . ,

NewReservation.

, Thread (). start () Thread () , . , , . : , , , .

public_gc class NewReservation

// NewReservation {

public:

void MakeReservation{} {

Console::WriteLine(

"Thread {0} starting.", // (0). Thread::CurrentThreach> // GetHashCode().ToString() ) ;

ReservationResu.lt *result =

hotelBroker->MakeReservation(

customerld, city, hotel, date, numberDays);

// customerId, , , , numberDays

)

);

Main () , :

NewReservation *reservel =

new NewReservation(customers, hotelBroker); // reservel->customer!d = 1;

reservel->city = "Boston"; // = "";

reservel->hotel - "Presidential"; // = ""; reservel->sdate = "12/12/2001"; reservel->numberDays = 3;

// ThreadStart *threadStartl = new ThreadStart ( reservel,

reservel->MakeReservation) ;

Thread *threadl = new Thread(threadStartl); // Console::WriteLine(

"Thread {0} starting a new thread.",

// " {0} . " Thread::CurrentThread-> // GetHashCode().ToString()); threadl->Start();

, Join () Thread (). ( ), , . .

// , threadl->Joint); // Console::WriteLine("Done!"); //

. 1 Threading ( ). .

NewReservation 'reserve 1 =

new //

=

new NewReservation (customers, hotelBroker); //

// ThreadStart *threadStartl = new ThreadStart( reservel,

reservel->MakeReservation);

ThreadStart *threadStart2 = new ThreadStart(

reserve2,

reserve2->MakeReservation);

Thread *threadl = new Thread (threadStartl) ; // Thread *thread2 = new Thread (threadStart2) ; //

Console::WriteLine(

"Thread {0} starting a new thread.",

// " {0} . "

Thread::CurrentThread-> // GetHashCode() .ToString ()); threadl->Start(); // thread2->Start() ;//

// , threadl~> Join ( ) ; // thread2->Join ; //

, , . , . , , . , . , , . (, , , ).

, , . Broker: : Reserve (::) Broker.h. , , . , , . Thread: : Sleep (:: ) , , . , .

// , for (int i = day; i < day + numDays; i+ + )

{

if (numCust[i, unitid) >= unit->capacity)

{

result->ReservationId = -1; // result->Comment = "Room not available"; // -> = " "; return result; //

)

}

Console::WriteLine (

"Thread {0} finds the room is available in

Broker::Reserve", // " {0} ,

// :: ",

Thread::CurrentThread-> //

GetHashCode (} .ToString ()) ;

Thread: : Sleep (0) ; // ::

// for (int i = day; i < day + nuraDays; numCust[i, unitid] += 1;

!

,

, , . , , . , , , .

, ( 1) Threading ( ) Thread: : Sleep

(:: ) , , . Sleep( ) .

,- 15, ! , . Main (), : hotelBroker->AddHotel(

"Boston", // "",

"Presidential", // "",

1, // !

(Decimal) 1GGGG); // ( )

=

new //

reservel->customerId = 1;

reservel->city = "Boston"; // = "";

reservel->hotel = "Presidential"; // = ""; reservel->sdate = "12/12/2001"; reservel->numberDays = 3;

NewReservation *reserve2 =

new NewReservation(customers, hotelBroker); // reserve2~>customer!d = 2;

reserve2~>city = "Boston"; // = "";

reserve2->hotel = "Presidential"; // = ""; reserve2->sdate - "12/13/2001";

reserve2->numberDays = 1;

:

Added Boston Presidential Hotel with one room. Thread 3 starting a new thread. Thread starting. Thread starting.

Reserving for Customer 2 at the Boston Presidential Hotel on

12/13/2GG1 12:GG:GG AM for 1 days

Reserving for Customer 1 at the Boston Presidential Hotel on

12/12/2001 12:00:00 AM for 3 days Thread 6 entered Broker::Reserve

Thread 6 finds the room is available in Broker: .-Reserve Thread 5 entered Broker::Reserve

Thread b finds the room is available in Broker::Reserve

Thread left Broker::Reserve

Reservation for Customer 2 has been booked

Reservationld = 1

Thread 5 left Broker::Reserve

Reservation for Customer 1 has been booked

^ , ! , . , , .ReservationId = 2 ReservationRate = 10000 ReservationCost = 30000 Comment = OK

ReservationRate = 10000 ReservationCost = 10000 Comment = OK Done !

16;

. 3 . 5 . .

2 12/13/2001 12:00:00 AM 1

1

12/12/2001 12:00:00 AM 3 ::

, :: 5 ::

5 , :: 6 ::

2 Reservationld = 1

5

1 Reservationld = 2 ReservationRate = 10000 ReservationCost = 30000

=

ReservationRate = 10000 ReservationCost = 10000

=. !

, () 13 ! , Reserve () , , . Reserve () , . .

, . .NET Framework .

.NETFramework ( ). .

. . . , , , ( ) , .

System: .-Threading: .-Monitor (:: -::) , . 2 Threading ( ) Monitor () this HotelBroker.

ReservationResult *Reserve(Reservation *res) // {

Console ::WriteLine(

"Thread {0} trying to enter Broker::Reserve", // " {0} :: ", Thread::CurrentThread-> // OetHashCode () .ToString ()) ; Monitor::Enter(this); // : : .. . { ) Monitor::Exit(this) ; // return result; //

}

, Monitor: : Enter (this), Reserve (), Monitor () this. , , , Monitor:: Exit (this). Monitor: : Enter (thi s) .

Monitor: : Enter (:: ) , Monitor: :Exit (::). , , Monitor : : TryEnter.

, , , 1, . , Reserve () , , , . , .

Added Boston Presidential Hotel with one room. Thread 3 starting a new thread. Thread 5 starting. Thread 6 starting.

Reserving for Customer 2 at the Boston Presidential Hotel on

12/13/2001 12:00:00 AM for 1 days Thread trying to enter Broker: :Reserve Thread 6 entered Broker : : ReserveThread 6 finds the room is available in Broker::Reserve Thread 6 left Broker::Reserve

Reservation for Customer 2 has been booked Reservationld = 1

Reserving for Customer 1 at the Boston Presidential Hotel on 12/12/2001 12:00:00 AM for 3 days Thread 5 trying to enter Broker::Reserve Thread 5 entered Broker::Reserve

Reservation for Customer 1 could not be booked Room not available

ReservationRate = 10000

ReservationCost = 1GGGG

Comment = OK

Done !

. 3 . . .

2

12/13/2001 12:00:00 AM 1 6 :: 6 ::

, :: 6 ::

2

Reservationld = 1

1

12/12/2001 12:00:00 AM 3 5 :: 5 ::

1

ReservationRate = 10000

ReservationCost = 1GGGG = OK

!

, Monitor (), , , , . Monitor: :Wait

(::) . ,

, .

, Monitor (), , Pulse (, ) PulseAll. , . , , , . , . , Pulse (, ) . PulseAll .

, , Monitor

(), ; .

, , , ()

, ( Monitor: : Enter (::) Wait ()), .

PulseAll Pulse (, )

PulseAll. :

First thread: 2 started. Thread: 5 started. Thread: 5 waiting.

Thread: 6 started. Thread: 6 waiting.

Thread 5 sleeping. Done.

Thread 5 awake.

Thread: 5 exited. Thread 6 sleeping.

Thread 6 awake. Thread: 6 exited.

18:

: 2 .

: 5 .

: 5 .

: 6 .

: 6 .

5 .

.

5 . : £ . 6 . 6 . : f .

X "" Ob j ect (), .

Test (), . , . , ,

.

main () , X: :Test (::)

. . ., . 2 , . main () PulseAl 1 . . , , , . _gc class X

// X

{

private: //

Object *; // public:

X(Object *) //

{

= ;

}

void Test() //

{

try

(

long threadld =

Thread::CurrentThread->GetHashCode (); // Console::WriteLine(

"Thread: {0} started.", threadld.ToString());

// ": {0} ." Monitor::Enter (); // :: Console::WriteLine(

"Thread: {0} waiting.", threadld.ToString());

// ": {0} ." Monitor::Wait(); // :: Console::WriteLine(

"Thread {0} sleeping.", threadld.ToStringO);

// " {0} ." Thread::Sleep(500); // :: Console::WriteLine(

"Thread {0} awake.", threadld.ToString()i;

// " {0} . " Monitor::Exit(); // Console: :V3riteLine (

"Thread: {0} exited.", threadld.ToString());

// ": {0} ."

}

catch(Exception *e) //

{

long threadld =

Thread::CurrentThread->GetHashCode(); Console: :VJriteLine (

"Thread: {0} Exception: (1)",

// ": {0} : {1} "

threadld.ToStringO , e->Message); // ^ Monitor::Exit(o); //

}

};

gc class Classl// 1

{

public:

static Object *o = new Object; // *o = ; static void Main()

{

Console::WriteLine(

"First thread: {0} started.", // " : {0} . ", Thread::CurrentThread->GetHashCode(].ToString());

X *a = new X(o) ; X *b = new X(o);

ThreadStart *ats = new ThreadStart (a, X::Test);

//

ThreadStart *bts = new ThreadStart (b, X: :Test) ; //

Thread *at - new Thread (ats) ; // Thread *bt = new Thread (bts); //

at->Start(); // bt->Start(); //

// , // (Pulse) Thread::Sleep(2000); // Monitor::Enter(); // ::

Monitor: :PulseAll(o) ; //Monitor::Pulse();

Monitor :: Exit () ; //

Console::WriteLine("Done."); //

}

};

,

PulseAll Pulse (, ),

. Sleep (2000) main (), , , (), , , . Wait (), Pulse (, ) PulseAll .

Thread: : Sleep (:: ) . Thread: : Suspend

: Resume

(::) . , (Thread: : Join (::)).

Threading ( ) , . ( ).

Thread: : Interrupt (::) . ThreadlnterruptedException. ,

, , , Thread: : Abort (;:). Thread:

, finally (). , Thread: :Abort (:: ) .

finally () , , ,

, . , ,

, , Thread: : Join (::).

Framework ,

Win32 . WaitAny, -

( ) . WaitOne, . , WaitHandle.

Mutex ( ) . .

, -

, - . AutoResetEvent

, . ManualResetEvent , Reset (). , . , , , .

. Interlocked(Copoa) . , , . , . ThreadPool .

() . . , System: :ContextBoundObject Synchronization (). . System: : Runtime:: Remoting: : Contexts (:: :: Remoting:;KoHTeKCTbi). , . SynchronizationAttribute , SynchronizationAttribute: NONSUPPORTED, SUPPORTED (Э), REQUIRED (), REQUJRES_NEW. Threading ( ) 3 , .

using namespace System::Runtime::Remoting::Contexts; //

// :: ::Remoting::;

// " SynchronizationAttribute: : REQUIRED () - 4 [Synchronization^) ] // [ (4) ]

public_gc_abstract class Broker :

// - Broker:

public ContextBoundObject

(

\;

CLR, , , , , . . , , , . , , , . CLR , ,

, .

, , . , , , . -

, .

, 堠 ⠠ 蠠 Synchronization ().

, . REQUIRED () , . , , . REQUIRES NEW , , . SUPPORTED() , ; .

, , . (.. ), . .

Synchronization () Brocker: : Reserve (::), Monitor: : Enter (;:) Monitor::

Exit (::).

, 2, . ;

Added Boston Presidential Hotel with one room. Thread 13 starting a new thread. Thread 28 starting. Thread 29 starting.

Reserving for Customer 1 at the Boston Presidential Hotel on

12/12/2001 12:00:00 AM for 3 days Thread 28 entered Broker::Reserve

Thread 28 finds the room is available in Broker; :Reserve

Thread 28 left Broker::Reserve

Reservation for Customer 1 has been booked

Reservationld = 1

ReservationRate = 10000

ReservationCost = 30000

Comment = OK

Reserving for Customer 2 at the Boston Presidential Hotel on 12/13/2001 12:00:00 AM for 1 days

Thread 29 entered Broker::Reserve

Thread 29 left Reserve.

Reservation for Customer 2 could not be booked Room not available

Done !

'9:

. 13 . 28 . '29 .

1

12/12/2001 12:00:00 3 28 ::

28 , :: 2.8 ::

1 Reservationld = 1 ReservationRate = 10000 ReservationCost = 30000 = OK

. .. 2

12/13/2001 12:00:00 AM 1 29 :: 29 .

2 !

, Reserve (), , , . .

, , . . .

, - . ,

-

CancelReservation, , ,

, , , , MakeReservation . , , - . , , .

, . . , , . , .