﻿<?xml version="1.0" encoding="utf-8" ?> 
<?xml-stylesheet type="text/xsl" href="../../webpages/courses.xsl" ?>
<!--
/////////////////////////////////////////////////////////////////////////////
//  ReviewThreads.xml - Resources for Threads and Synchronization          //
//                                                                         //                                                                         //
//  Jim Fawcett, CST 2-187, Syracuse University, jfawcett@twcny.rr.com     //
/////////////////////////////////////////////////////////////////////////////
-->

<courses name="Fawcett">

  <btitle>Win32 Processes and Threads</btitle>
  <theme src="../../webpages/HomeTheme.css" />

  <visible>
  <title>Win32 Programming: Processes and Threads</title>

  <revised>03 March 2005</revised>
  
  <hilight>
    <ifile title="Windows Programming">../windowsProgramming.xml</ifile>&#32;
    <html><a href="#contents">Contents</a></html>&#32;
  </hilight>
  
  <section title="Web Resources:">
    <ilink title="Code Project">www.codeproject.com/library/tutorials.asp</ilink>&#32;
    <ilink title="Windows Forms">windowsforms.net/</ilink>&#32;
    <ilink title="Code Guru">www.codeguru.com</ilink>&#32;
    <ilink title="Win32, COM, MFC">devCentral.iftech.com</ilink>&#32;
    <ilink title="Windows Developer's Journal">www.wdj.com</ilink>&#32;
    <ilink title="GotDotNet">www.gotdotnet.com</ilink>&#32;
  </section>

  <html><a name="Contents"/></html>
  <p/>
  <br/><br/>
  <html><img src="process.png" width="75%" align="right" hspace="50" vspace="50"/></html>

  A process is just a container for threads and their resources.  These include:
  <div margin-left="50">
  <olist>
    <litem>process code, including that for Dynamic Link Libraries, loaded at run-time</litem>
    <litem>global memory</litem>
    <litem>stack frames for each thread, containing local variables declared in their thread processing functions, and all the functions they call</litem>
    <litem>thread local storage</litem>
    <litem>process heaps</litem>
  </olist>
  </div>
  The process, its DLL modules, threads, heaps, and windows the threads create, are all referenced by handles.  A handle is just an index into a table
  of addresses of each of the kernel objects the process has created.
  <p/>
  Threads are either UI or worker threads.  UI threads are those that create one or more windows, and each window has a message loop, responsible for extracting and
  processing messages enqueued by the operating system or code within the process.  Each message represents some kind of event - perhaps a mouse movement or key press - that
  is relevant to the receiving window.  When a UI thread creates a window, its thread local storage is used to hold state for that window.  Many of
  the Win32 API functions devoted to windows processing use that information to carry out their assigned tasks.
  <p/>
  For this reason, a worker thread (which does not hold the relevant window state) must never directly call Win32 API functions that take a window handle.
  That function will probably expect the calling thread to have the appropriate state for the window, which the worker thread does not have, and the resulting
  behavior is undefined.  We say that: 
  <ind>Windows have thread afinity</ind>
  That is, they should only be called by the thread that created them.  When a worker thread needs to communicate with the processing function for a window,
  it does this by sending a message to the window using ::SendMessage(hWnd,Msg,wParm,lParm) or posting a message to its message queue using ::PostMessage(hWnd,Msg,wParm,lParm);
  </visible>
</courses>
