Use thr_create() to add a new thread of control to the current process.
#include <thread.h> int thr_create(void *stack_base, size_t stack_size, void *(*start_routine) (void *), void *arg, long flags, thread_t *new_thread); size_t thr_min_stack(void);
stack_size--Contains the size, in number of bytes, for the stack that the new thread uses. If stack_size is zero, a default size is used. In most cases, a zero value works best. If stack_size is not zero, it must be greater than the value returned by thr_min_stack().
There is no general need to allocate stack space for threads. The threads library allocates one megabyte of virtual memory for each thread's stack with no swap space reserved. (The library uses the -MAP_NORESERVE option of mmap() to make the allocations.)
start_routine--Contains the function with which the new thread begins execution. When start_routine() returns, the thread exits with the exit status set to the value returned by start_routine (see "thr_exit(3T)";).
arg--Can be anything that is described by void, which is typically any 4-byte value. Anything larger must be passed indirectly by having the argument point to it.
Note that you can supply only one argument. To get your procedure to take multiple arguments, encode them as one (such as by putting them in a structure).
The value in flags is constructed from the bitwise inclusive OR of the following:
THR_SUSPENDED--Suspends the new thread and does not execute start_routine until the thread is started by thr_continue(). Use this to operate on the thread (such as changing its priority) before you run it. The termination of a detached thread is ignored.
THR_NEW_LWP--Increases the concurrency level for unbound threads by one. The effect is similar to incrementing concurrency by one with thr_setconcurrency(3T), although THR_NEW_LWP does not affect the level set through the thr_setconcurrency() function. Typically, THR_NEW_LWP adds a new LWP to the pool of LWPs running unbound threads.
THR_DAEMON--Marks the new thread as a daemon. The process exits when all nondaemon threads exit. Daemon threads do not affect the process exit status and are ignored when counting the number of thread exits.
A process can exit either by calling exit() or by having every thread in the process that was not created with the THR_DAEMON flag call thr_exit(3T). An application, or a library it calls, can create one or more threads that should be ignored (not counted) in the decision of whether to exit. The THR_DAEMON flag identifies threads that are not counted in the process exit criterion.
new_thread--Points to a location (when new_thread is not NULL) where the ID of the new thread is stored when thr_create() is successful. The caller is responsible for supplying the storage this argument points to. The ID is valid only within the calling process.
If you are not interested in this identifier, supply a zero value to new_thread.
Returns a zero and exits when it completes successfully. Any other returned value indicates that an error occurred. When any of the following conditions are detected, thr_create() fails and returns the corresponding value.
Stack behavior in Solaris threads is generally the same as that in pthreads. For more information about stack setup and operation, see "About Stacks".
You can get the absolute minimum on stack size by calling thr_min_stack(), which returns the amount of stack space required for a thread that executes a null procedure. Useful threads need more than this, so be very careful when reducing the stack size.
You can specify a custom stack in two ways. The first is to supply a NULL for the stack location, thereby asking the runtime library to allocate the space for the stack, but to supply the desired size in the stacksize parameter to thr_create().
The other approach is to take overall aspects of stack management and supply a pointer to the stack to thr_create(). This means that you are responsible not only for stack allocation but also for stack deallocation--when the thread terminates, you must arrange for the disposal of its stack.
Use thr_min_stack(3T) to get the minimum stack size for a thread.
#include <thread.h> size_t thr_min_stack(void);
A thread that does more than execute a null procedure should allocate a stack size greater than the size of thr_min_stack().
When a thread is created with a user-supplied stack, the user must reserve enough space to run the thread. In a dynamically linked execution environment, it is difficult to know what the thread minimal stack requirements are.
Most users should not create threads with user-supplied stacks. User-supplied stacks exist only to support applications that require complete control over their execution environments.
Instead, users should let the threads library manage stack allocation. The threads library provides default stacks that should meet the requirements of any created thread.
Use thr_self(3T) to get the ID of the calling thread.
#include <thread.h> thread_t thr_self(void);
thr_yield() causes the current thread to yield its execution in favor of another thread with the same or greater priority; otherwise it has no effect. There is no guarantee that a thread calling thr_yield() will do so.
#include <thread.h> void thr_yield(void);
thr_kill() sends a signal to a thread.
#include <thread.h> #include <signal.h> int thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset);
Use thr_exit() to terminate a thread.
#include <thread.h> void thr_exit(void *status);
Use the thr_join() function to wait for a thread to terminate.
#include <thread.h> int thr_join(thread_t tid, thread_t *departedid, void **status);
#include <thread.h> thread_t tid; thread_t departedid; int ret; int status; /* waiting to join thread "tid" with status */ ret = thr_join(tid, &departedid, (void**)&status); /* waiting to join thread "tid" without status */ ret = thr_join(tid, &departedid, NULL); /* waiting to join thread "tid" without return id and status */ ret = thr_join(tid, NULL, NULL);
When the tid is
(thread_t)0, then thread_join() waits for any undetached thread in the process to terminate.
In other words, when no thread identifier is specified, any undetached thread
that exits causes thread_join() to return.
#include <thread.h> thread_t tid; thread_t departedid; int ret; int status; /* waiting to join thread "tid" with status */ ret = thr_join(NULL, &departedid, (void **)&status);
By indicating NULL as thread id in the Solaris thr_join(), a join will take place when any non detached thread in the process exits. The departedid will indicate the thread ID of exiting thread.
Except for the function names and arguments, thread specific data is the same for Solaris as it is for POSIX. The synopses for the Solaris functions are given in this section. The functions are explained in "Create a Thread-Specific Data Key".
thr_keycreate() allocates a key that is used to identify thread-specific data in a process.
#include <thread.h> int thr_keycreate(thread_key_t *keyp, void (*destructor) (void *value));
#include <thread.h> int thr_setspecific(thread_key_t key, void *value);
#include <thread.h> int thr_getspecific(thread_key_t key, void **valuep);
In Solaris threads, if a thread is to be created with a priority other than that of its parent's, it is created in SUSPEND mode. While suspended, the threads priority is modified using the thr_setprio(3T) function call; then it is continued.
An unbound thread is usually scheduled only with respect to other threads in the process using simple priority levels with no adjustments and no kernel involvement. Its system priority is usually uniform and is inherited from the creating process.
The function thr_setprio() changes the priority of the thread, specified by tid, within the current process to the priority specified by newprio.
#include <thread.h> int thr_setprio(thread_t tid, int newprio)
By default, threads are scheduled based on fixed priorities that range from zero, the least significant, to the largest integer. The tid will preempt lower priority threads, and will yield to higher priority threads.
thread_t tid; int ret; int newprio = 20; /* suspended thread creation */ ret = thr_create(NULL, NULL, func, arg, THR_SUSPEND, &tid); /* set the new priority of suspended child thread */ ret = thr_setprio(tid, newprio); /* suspended child thread starts executing with new priority */ ret = thr_continue(tid);
#include <thread.h> int thr_getprio(thread_t tid, int *newprio)