# OS_CreateThread

## C Specification

      #include <nitro/os.h>
      void OS_CreateThread(
               OSThread*  thread ,
               void       (*func)(void*) ,
               void*      arg ,
               void*      stack ,
               u32        stackSize ,
               u32        prio );

## Description

Creates threads

With NitroSDK, you are limited to the creation of 16 threads. This value is defined in `OS_THREAD_MAX_NUM` of `nitro/os/thread.h`. By changing its value, you can increase the maximum number of threads up to 32. However, you will need to rebuild the library in addition to changing the header file if you make any such changes.

When calling `OS_InitThread()`, note that the program that call the function is threaded. The Idle thread is also created by the `OS_InitThread()` function. These are included in the total number of threads.

The threads created by `OS_CreateThread()` go immediately into sleep mode, so they need to be explicitly reactivated with `OS_WakeupThreadDirect()`.

> (Example)

>     #define THREAD_PRIO 10
>     #define STACK_SIZE 1024
>     void proc( void* arg);
>     OS_Thread thread;
>     u64 stack[ STACK_SIZE / sizeof(u64) ];

>     void nitroMain()
>     {
>         :
>         OS_InitThread();
>         OS_CreateThread(
>                 &thread ,
>                 proc ,
>                 NULL ,
>                 stack+STACK_SIZE/sizeof(u64) ,
>                 STACK_SIZE,THREAD_PRIO );
>         OS_WakeupThreadDirect( &thread );
>         :
>     }

>     void proc( void *arg)
>     {
>         :
>     }

## Arguments

`thread` | Points to the thread structure to be initialized
`func` | Points to the function that starts execution
`arg` | The argument to pass to the function that begins execution
`stack` | The starting address of the stack pointer. Items are put into the stack from the top down, so be aware that the highest position of the stack must be specified. The address must be given a 4-byte alignment.
`stackSize` | Stack size (in bytes) Must be in multiples of 4.
`prio` | Thread priority `0` is the highest priority and `31` is the lowest. A thread called with `OSInitThread` will have a priority of `16`. When threads are created with the same priority, the latest one created will have priority.

## Return Value

None

## See Also

`OS_InitThread`, `OS_SleepThread`, `OS_WakeupThread`, `OS_WakeupThreadDirect`

## Revision History

11/01/2004 Added description of change to number of threads
04/27/2004 Added description of idle thread
02/26/2004 `arg` added. Description of thread priority 0-31 added
01/12/2003 Initial version