Tuesday, 27 October 2015

tugas 4



Threads
          An execution state (running, ready, etc.)
          Saved thread context when not running
          Has an execution stack
          Some per-thread static storage for local variables
          Access to the memory and resources of its process
        all threads of a process share this

-   Threads allow multiple executions to take place in the same process environment
-    Lightweight process Þ because threads have some properties of processes
-    Multithreading Þ allowing multiple threads in the same process

Thread  State
• Block:
       When a thread needs to wait for an event, it will block (saving its user registers, program counter, and stack pointers).
       The processor may now turn to the execution of another ready thread in the same or a different process.
• Unblock:
       When the event for which a thread is blocked occurs, the thread iscmoved to the Ready queue.
• Finish:
       When a thread completes, its register context and stacks are deallocated.

Threading granularity
       Coarse Threading
       Individual modules, called systems, are assigned to individual processors.
       In the Source engine case, this would mean putting rendering on one processor, AI (artificial intelligence) on another, physics on another, and so on.
       each major module is single threaded and the principal coordination involves synchronizing all the thread with a timeline thread.
       Fine-grained threading
       Many similar or identical tasks are spread across multiple processors.
       E.g. a loop that iterates over an array of data can be split up into a number of smaller parallel loops in individual threads that can be scheduled in parallel.
       Hybrid threading
       This involves the selective use of fine-grained threading for some systems and single threading for other systems

Thread programming
       pthread_self()
      To obain its ID
       pthread_join()
      To join or rejoin various flows of control
      Wen called, the calling thread is suspended untl the execution of the target thread is termnated
      Releases resources (i.e. prevents zombie threads)

Setting thread attributes
Some properties:
       Contention Scope
      Used to set scheduling properties either within the process or on the global system.
       Stack Size & Stack Address
      Allows a programmer to set how big the stack should be and where in memory it should be.
       Detach State
      You can create a thread as detached or joinable.
       Schedule Policy & Schedule Parameters.
      If your operating system supports it you can outline what scheduling should be done within the process between the Light Weight Processes (LWP’s). You can even give it parameters such as priority.

       When threads are created with the appropriate attribute object they will have the specified behavior.
       In order to set the attribute object we must invoke
                                pthread_attr_init().

Detached threads
       A detached thread is  the one that is not waited upon by any other thread
       When the thread terminated, it will automatically be reclaimed by the operating system

Terminating thread
       pthread_exit()
      The function takes a pointer to data which is returned when the thread is joined

When to use threads?
       Threads are best suited to programs which require to  do things concurrently OR have a number of tasks which can be solved in parallel to yield a result.
       When writing your program using threads you must organize the program into discrete tasks which can be executed concurrently.
       Examples:
      Computer graphics problems e.g. Ray tracing
      Matrix manipulations









                                         




No comments:

Post a Comment