Main Page Class Hierarchy Compound List File List Compound Members File Members

# threads_ew.c

Go to the documentation of this file.

    00001
    00002 /*
    00003  *   THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE
    00004  *   CHECKED IT OUT USING THE COMMAND CHECKOUT.
    00005  *
    00006  *    $Id: threads__ew_8c-source.html,v 1.1 2006/05/19 16:55:02 paulf Exp $
    00007  *
    00008  *    Revision history:
    00009  *     $Log: threads__ew_8c-source.html,v $
    00009  *     Revision 1.1  2006/05/19 16:55:02  paulf
    00009  *     first inclusion
    00009  *
    00010  *     Revision 1.1  2000/02/14 18:53:30  lucky
    00011  *     Initial revision
    00012  *
    00013  *
    00014  */
    00015
    00016               /********************************************
    00017               *               threads_ew.c                *
    00018               *            Windows NT version             *
    00019               * This file contains functions StartThread, *
    00020               * WaitThread, and KillThread                *
    00021               *********************************************/
    00022
    00023 #include <windows.h>
    00024 #include <process.h>
    00025
    00026
    00027    /********************************************************************
    00028     *                           StartThread                            *
    00029     *                                                                  *
    00030     *  Arguments:                                                      *
    00031     *     fun:        Name of thread function. Must take (void *)      *
    00032     *                 as an argument and return void                   *
    00033     *     stack_size: Stack size of new thread in bytes                *
    00034     *                 If 0, stack size is set to 8192.                 *
    00035     *                 In OS2, 4096 or 8192 is recommended.             *
    00036     *                 In SOLARIS, this argument is ignored             *
    00037     *                 In Windows NT, if stack_size=0, use the stack    *
    00038     *                 size of the calling thread.                      *
    00039     *     thread_id:  Thread identification number returned to         *
    00040     *                 calling program.                                 *
    00041     *                                                                  *
    00042     *  The function <fun> is not passed any arguments.                 *
    00043     *                                                                  *
    00044     *  Returns:                                                        *
    00045     *    -1 if error                                                   *
    00046     *     0 if ok                                                      *
    00047     ********************************************************************/
    00048
    00049 int StartThread( void fun(void *), unsigned stack_size, unsigned *thread_id )
    00050 {
    00051    unsigned long tid;
    00052
    00053    tid = _beginthread( fun, stack_size, NULL );
    00054
    00055    if ( tid == -1 )                /* Couldn't create thread */
    00056       return -1;
    00057
    00058    *thread_id = (unsigned)tid;     /* Return the thread id */
    00059    return 0;
    00060 }
    00061
    00062    /********************************************************************
    00063     *                       StartThreadWithArg                         *
    00064     *                                                                  *
    00065     *  Arguments:                                                      *
    00066     *     fun:        Name of thread function. Must take (void *)      *
    00067     *                 as an argument and return void                   *
    00068     *     arg:        an unsigned long (void*) passed to the thread.   *
    00069     *     stack_size: Stack size of new thread in bytes                *
    00070     *                 If 0, stack size is set to 8192.                 *
    00071     *                 In OS2, 4096 or 8192 is recommended.             *
    00072     *                 In SOLARIS, this argument is ignored             *
    00073     *                 In Windows NT, if stack_size=0, use the stack    *
    00074     *                 size of the calling thread.                      *
    00075     *     thread_id:  Thread identification number returned to         *
    00076     *                 calling program.                                 *
    00077     *                                                                  *
    00078     *  Returns:                                                        *
    00079     *    -1 if error                                                   *
    00080     *     0 if ok                                                      *
    00081     ********************************************************************/
    00082
    00083 int StartThreadWithArg( void fun(void *), void* arg, unsigned stack_size,
    00084                         unsigned *thread_id )
    00085 {
    00086    unsigned long tid;
    00087
    00088    tid = _beginthread( fun, stack_size, (HANDLE)arg );
    00089
    00090    if ( tid == -1 )                /* Couldn't create thread */
    00091       return -1;
    00092
    00093    *thread_id = (unsigned)tid;     /* Return the thread id */
    00094    return 0;
    00095 }
    00096
    00097
    00098    /*************************************************************
    00099     *                        KillSelfThread                     *
    00100     *             For a thread exit without affecting           *
    00101     *                        other threads                      *
    00102     *************************************************************/
    00103
    00104 int KillSelfThread( void )
    00105 {
    00106     _endthread();
    00107     return 0;
    00108 }
    00109
    00110
    00111   /*************************************************************
    00112    *                          WaitThread                       *
    00113    *                   Wait for thread to die.                 *
    00114    *                                                           *
    00115    *  This function is used in coaxtoring.c                    *
    00116    *                                                           *
    00117    *    thread_id = Pointer to thread id                       *
    00118    *                                                           *
    00119    *  Returns:                                                 *
    00120    *    -1 if error                                            *
    00121    *     0 if ok                                               *
    00122    *************************************************************/
    00123
    00124 int WaitThread( unsigned *thread_id )
    00125 {
    00126    if ( WaitForSingleObject( (HANDLE)(*thread_id), INFINITE )
    00127                                              == WAIT_FAILED )
    00128       return -1;
    00129    return 0;
    00130 }
    00131
    00132
    00133    /************************************************************
    00134     *                         KillThread                       *
    00135     *                Force a thread to exit now!               *
    00136     *                                                          *
    00137     *  Windows NT documentation gives a strong warning against *
    00138     *  using TerminateThread(), since no stack cleanup, etc,   *
    00139     *  is done.                                                *
    00140     *                                                          *
    00141     * Argument:                                                *
    00142     *    tid = id of thread to kill                            *
    00143     *                                                          *
    00144     * Returns:                                                 *
    00145     *     0 if ok                                              *
    00146     *     non-zero value indicates an error                    *
    00147     ************************************************************/
    00148
    00149 int KillThread( unsigned int tid )
    00150 {
    00151     const DWORD exit_code = 0;
    00152
    00153     if ( TerminateThread( (HANDLE)tid, exit_code ) == 0 )
    00154        return -1;
    00155
    00156     return 0;
    00157 }
    00158
    00159