Page 320 - DCAP404 _Object Oriented Programming
P. 320

Unit 14: Advanced Concept in C++




          Thread                                                                                Notes

          A thread is a sequence of instructions to be executed within a program. Each thread has its own
          set of resources such  as instruction pointer, set of registers and stack memory. The  virtual
          address space is common to all threads within a process. This enables all the threads to access
          data on the heap.
          Normal processes consist of a single thread of execution that starts in a single method (main() in
          UNIX). Each line of the code is executed exactly one line at a time. Earlier the normal way to
          achieve concurrency in a program was to use the fork() and exec() system calls (or equivalent
          system calls in other operating systems) to create several processes. Each of these processes used
          to e executed as a single thread of execution.

          Since there are a lot of similarities between a process and a thread, a thread is often referred to
          as lightweight process.
          Threads can be created as instances of the Thread class. It has attributes and methods that create
          and control a thread, set its priority, and get its status. The namespace of the Thread class is
          System.Threading assembled in mscorlib.dll. Given below is its syntax.

                 [ComVisibleAttribute(true)]
                 [ClassInterfaceAttribute(ClassInterfaceType::None)]
                 public ref class Thread sealed : public CriticalFinalizerObject, _Thread
          A single process can create as many threads as required to execute different portions of the code.
          The program code to be executed by a thread is specified using ThreadStart delegate or  the
          ParameterizedThreadStart delegate. The ParameterizedThreadStart delegate allows one to pass
          data to the thread procedure.
          The following program demonstrates simple threading functionality offered by C++. The code
          must be compiled using /clr option.
          using namespace System;
          using namespace System::Threading;
          public ref class ExampleThread

          {
          public:
              //  The  ThreadProc  method  is  called  when  the  thread  starts.  It  loops  five
          times,  writing  to
          //  the  console  and  yielding  the  rest  of  its  time  slice  each  time,  and  then
          ends.
                 static  void  ThreadProc()
                 {
                         for  (  int  i  = 0;  i  <  5;  i++  )
                         {
                                Console::Write(  “Thread  Procedure:  “  );

                                Console::WriteLine(  i  );
                                Thread::Sleep(0);
                         }



                                           LOVELY PROFESSIONAL UNIVERSITY                                   313
   315   316   317   318   319   320   321   322   323   324   325