//Sample code file: var/ndk/webBuildengine/tmp/viewable_samples/7999585e-fdab-4e7b-b86c-67d55e862138/nks/ThrJoin.c

//Warning: This code has been marked up for HTML

/***************************************************************************
$name: ThrJoin.c 
$version: 1.0 
$date_modified: 082499 
$description: Demonstrates joining a 'joinable' thread from a 'detached' one.
$owner: NKS Product Manager 
Copyright (c) 1999 Novell, Inc. All Rights Reserved.

THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT
ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS THIS WORK.
PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A
ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS
PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET,
DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S
PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S
CUSTOMERS WITH RESPECT TO THIS CODE.
****************************************************************************/
#include <stdio.h>
#include <nks/thread.h>

int   gExitStatus = 0;

void ThreadFunc
(
   int   *arg
)
{
   printf("Thread %d running on context: %08x\n", NXThreadGetId(), *(int *)arg);
    NXThreadExit(&gExitStatus);
}

void main( void )
{
   int            err, *status;
   NXContext_t      *myContext;
   NXThreadId_t   myThread, departedThread;

   myContext = NXContextAlloc(ThreadFunc, &myContext, NX_PRIO_MED, 0,
                                                         NX_CTX_NORMAL, &err);
   if (myContext)
   {
      // create a thread to join...

      err = NXThreadCreate(myContext, NX_THR_DETACHED, &myThread);

      if (!err)
      {
         // join ourselves to the newly created thread...

         err = NXThreadJoin(myThread, &departedThread, &status);

         if (!err)
            printf("Thread succesfully joined.\n");
      }
   }
}