This function starts a new thread based on the function fn. The new thread stops either when it executes the function thread_stop, or when fn returns.
The new thread uses the 8-byte-aligned area ws as its workspace (stack), which should be wssize octets (eight-bit bytes) long. It may be allocated from the heap using malloc, or it could be a static or auto array variable.
The function returns a (non-zero) handle to the created thread on success. If wssize is less than THREAD_MIN_STACK the function fails and returns 0.
The priority argument defines the priority at which the new thread should run. It is an integer in the range 0–7, where 0 represents the highest priority and 7 the lowest. The header defines the literals THREAD_URGENT and THREAD_NOTURG, corresponding to priority levels 0 and 1 respectively. New threads are often started at the same priority as the thread creating them. You can do this by using the function thread_priority (see below) to provide a value for this argument.
The argument arg is passed to the new thread's function, fn. It could be, for example, a pointer to a simple variable, or to a struct variable containing a number of parameter values. When you pass a pointer to a variable into a thread in this way, you must make sure that the variable does not change before the thread reads it.
By using a cast, it is also possible to pass a value argument to the thread function:
thread_launch(func, ws, 1000, 1, (void *)150);
The func function could take up its argument like this:
void func(void *value) {
int param = (int)value;
}
In this example, param would take the value 150. Note that this technique does not work with float or double value parameters; they must be passed by reference.
The value returned by the function may be used to refer to the thread (see thread_wait).
See also the description of thread_new, which simplifies thread creation by starting a thread at the current priority and allocating the thread's workspace from the heap.
|