_beginthread

start a new thread of execution

Synopsis:

#include <process.h>
int _beginthread(
    register void (*start_address)(void *),
    void *stack_bottom,
    unsigned stack_size,
    void *arglist );

Description:

The _beginthread() function is used to start a new thread of execution at the function identified by start_address with a single parameter given by arglist.

The QNX libraries currently aren't completely thread-safe. If you need to use this functionality, contact Technical Support to see if newer versions of the libraries are available.

For each operating environment under which _beginthread() is supported, the _beginthread() function uses the appropriate system call to begin a new thread of execution.

The new thread uses the memory identified by stack_bottom and stack_size for its stack.

The application doesn't need to provide memory for a stack; the stack_bottom may be NULL, in which case the run-time system provides a stack. You must specify a non-zero stack_size for this stack.

The thread ends when it exits from its main function or calls exit(), _exit() or _endthread().

The variable/function _threadid, which is defined in stddef.h, may be used by the executing thread to obtain its thread ID. It's a far pointer to an int.

Under QNX, the number of threads an application can create is limited by the number of processes.

Returns:

the thread ID for the new thread, if successful, or -1 on an error, indicating that the thread couldn't be started. The global variable errno is set to indicate the type of error that occurred.

Errors:

EAGAIN
There are too many threads.
EINVAL
The argument is invalid, or the stack size is incorrect.
ENOMEM
There isn't enough memory available.

See also:

_endthread()

Examples:

#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <stddef.h>

#define  STACK_SIZE   4096
#if defined(__386__)
  #define FAR
#else
  #define FAR __far
#endif

void FAR child( void FAR *parm )
  {
    char * FAR *argv = (char * FAR *) parm;
    int  i;

    printf( "My thread ID = %d\n", *_threadid );
    for( i = 0; argv[i]; i++ ) {
      printf( "argv[%d] = %s\n", i, argv[i] );
    }
    _endthread();
  }

void main()
  {
    char *stack;
    char *args[3];
    int   tid;

    args[0] = "child";
    args[1] = "parm";
    args[2] = NULL;
    stack = (char *) malloc( STACK_SIZE );
    tid = _beginthread( child, stack, STACK_SIZE, args );
  }

Classification:

WATCOM

Systems:

NT, OS2 (DL), OS2 (MT), OS/2-32, Netware, QNX