Page 321 - DCAP404 _Object Oriented Programming
P. 321

Object-oriented Programming




                    Notes                 }
                                   };
                                   int  main()
                                   {
                                                 Console::WriteLine(  “From  Main  Thread:  Start  a  second  thread.”
                                   );
                                   //  Create  the  thread,  passing  a  ThreadStart  delegate  that  represents  the
                                   //  ThreadExample::ThreadProc  method.  For  a  delegate  representing  a
                                   //  static  method,  no  object  is  required.
                                   Thread^oThread=gcnew  Thread(gcnew  ThreadStart(&ExampleThread::ThreadProc
                                   )  );
                                   //  Start  ThreadProc.  Note  that  on  a  uniprocessor,  the  new  thread  does  not
                                   get  any
                                   //  processor  time  until  the  main  thread  is  preempted  or  yields.  Uncomment
                                   the

                                   //  Thread.Sleep  that  follows  Start()  to  see  the  difference.
                                          Thread->Start();
                                                 //Thread::Sleep(0);
                                                 for  (  int  i  = 0;  i  <  4;  i++  )
                                                 {
                                                 Console::WriteLine(  “From  Main  Thread:  Work  Work  Work.”  );
                                                        Thread::Sleep(  0  );

                                                 }
                                   Console::WriteLine(  “From  Main  Thread:  Call  Join(),  to  wait  until  ThreadProc
                                   ends.”  );
                                                 oThread->Join();
                                                 Console::WriteLine(  “From  Main  thread:  Press  Enter  to  end
                                   program.”  );
                                                 Console::ReadLine();
                                                 return  0;
                                   }
                                   Here is the result of a run of this simple example.
                                   From Main Thread: Start a second thread.

                                          From Main Thread: Work Work Work.
                                          Thread Procedure: 0
                                          From Main Thread: Work Work Work.
                                   Thread Procedure: 1
                                          From Main Thread: Work Work Work.

                                          Thread Procedure: 2



          314                               LOVELY PROFESSIONAL UNIVERSITY
   316   317   318   319   320   321   322   323   324   325   326