man vthread (Fonctions bibliothèques) - portable threads
NAME
Vthread - portable threads
SYNOPSIS
#include <vthread.h>
VTHREAD TYPES
Void_t*; Vthread_t; Vtmutex_t; Vtonce_t;
THREAD MANAGEMENT
Vthread_t* vtopen(Vthread_t* vt, int flags); int vtclose(Vthread_t* vt); int vtset(Vthread_t* vt, int type, Void_t* val); Vthread_t* vtself(); int vtrun(Vthread_t* vt, Void_t*(*startf)(Void_t*), Void_t* arg); int vtwait(Vthread_t* vt); int vtkill(Vthread_t* vt);
MUTEX MANAGEMENT
Vtmutex_t* vtmtxopen(Vtmutex_t* mtx, int flags); int vtmtxclose(Vtmutex_t* mtx); int vtmtxlock(Vtmutex_t* mtx); int vtmtxtrylock(Vtmutex_t* mtx); int vtmtxunlock(Vtmutex_t* mtx); int vtmtxclrlock(Vtmutex_t* mtx);
EXECUTING EXACTLY ONCE
#define VTONCE_INITDATA int vtonce(Vtonce_t* once, void(*func)() );
GETTING STATUS
int vtstatus(Vthread_t* vt); int vterror(Vthread_t* vt); int vtmtxerror(Vtmutex_t* mtx); int vtonceerror(Vtonce_t* once);
DESCRIPTION
Vthread provides a portable set of functions to create and manage threads within a single process. The restriction to a single process allows portable and efficient implementations of mutexes on top of native primitives available on different platforms. The mutex interface is kept simple by restricting mutexes to be of the recursive or counting type.
VTHREAD TYPES
Void_t*
This type is used to pass objects with unknown type between different code variety. Void_t is defined as void for ANSI-C and C++ and char for other compilation environments.
Vthread_t
This is the type of a thread handle.
Vtmutex_t
This is the type of a mutex.
Vtonce_t
This is the type of a structure used to execute certain actions exactly once per process no matter how many attempts to do so are made from different threads.
THREAD MANAGEMENT
Vthread_t* vtopen(Vthread_t* vt, int flags)
This function creates a new thread handle or renews an existing one. It returns the resulting thread handle or NULL on error. vt: If vt is NULL, a new thread handle is created. In this case, the bits VT_INIT and VT_FREE will be added to flags (below). If vt is not NULL, vt is an existing thread to be renewed. In this case, if the thread is running, it will be waited until execution finishes before renewing. flags: The bit VT_INIT indicates that the handle should be initialized. Otherwise, the handle is assumed to have been initialized already. The bit VT_FREE indicates that the space occupied by the handle was obtained via malloc() and can be freed when the handle is closed.
int vtclose(Vthread_t* vt)
This function closes the handle vt and also frees it if appropriate (see vtopen()). If currently running, closing is done only after execution finishes. This function returns 0 on success and -1 on error.
int vtset(Vthread_t* vt, int type, Void_t* val);
This function sets certain attributes of the thread vt. It returns 0 on success and -1 on error. type: This currently can be only VT_STACK to set the execution stack size for the thread vt. In the case, the stack size is (size_t)val.
Vthread_t* vtself();
This function returns the handle of the calling thread.
int vtrun(Vthread_t* vt, Void_t*(*startf)(Void_t*), Void_t* arg);
This function starts the thread running with the call (*startf)(arg). It returns 0 on success and -1 on error.
int vtwait(Vthread_t* vt)
This function waits for the termination of the execution of the thread vt if currently running. It returns 0 on success and -1 on error.
int vtkill(Vthread_t* vt)
This function cancels the execution of the thread vt if currently running. It returns 0 on success and -1 on error.
MUTEX MANAGEMENT
Mutexes are recursive. That is, each mutex maintains a lock count. A thread that successfully locks a mutex can relock it as often as needed. Each successful locking attempt increases the lock count by one. Only the thread that owns the lock on a mutex can unlock it. Each successful unlocking attempt decreases the lock count by one.
Vtmutex_t* vtmtxopen(Vtmutex_t* mtx, int flags);
This function creates a new mutex or renews an existing mutex structure. In the latter case, the mutex structure should not be currently locked or some undefined behavior will result. The function returns the resulting mutex or NULL on error. mtx: If mtx is NULL, a new mutex is created. In this case, the bits VT_INIT and VT_FREE will be added to flags (below). If mtx is not NULL, mtx is an existing mutex to be renewed. flags: The bit VT_INIT indicates that the mutex should be initialized. Otherwise, the mutex is assumed to have been initialized already. The bit VT_FREE indicates that the space occupied by the mutex was obtained via malloc() and can be freed when the mutex is closed.
int vtmtxclose(Vtmutex_t* mtx);
This function clears the lock count of the mutex mtx, then closes it. It returns 0 on success and -1 on error.
int vtmtxlock(Vtmutex_t* mtx);
This function attempts to lock the mutex mtx. If the lock is currently clear, the current thread becomes its owner. Then, only this thread can relock the mutex as many times as desired. Any attempt to lock mtx from a different thread will block. This function returns 0 on success and -1 on error.
int vtmtxtrylock(Vtmutex_t* mtx);
This function attempts to lock the mutex mtx without blocking. It returns 0 on success and -1 on error.
int vtmtxunlock(Vtmutex_t* mtx);
This function attempts to decrease the lock count on the mutex mtx by one. Only the thread owning mtx can unlock it. Any other attempt will result in failure. The function returns 0 on success and -1 on error.
int vtmtxclrlock(Vtmutex_t* mtx);
This function attempts to clear the lock on the mutex mtx, i.e., to reset the lock count to zero. Only the thread owning mtx can clear it. Any other attempt will result in failure. The function returns 0 on success and -1 on error.
EXECUTING EXACTLY ONCE
VTONCE_INITDATA
This macro is used to initialize a Vtonce_t structure in the following manner:
Vtonce_t once = VTONCE_INITDATA;
int vtonce(Vtonce_t* once, void(*func)() );
This function ensures that the call (*func)() is made exactly once per process and once structure regardless of how many attempts are made. It is useful for initialization of shared data across threads. It returns 0P on success and -1 on error.
GETTING STATUS
Void_t* vtstatus(Vthread_t* vt);
This function returns the exit status of vt after execution finishes.
int vterror(Vthread_t* vt);
This function returns the error status of vt after a failed operation.
int vtmtxerror(Vtmutex_t* mtx);
This function returns the error status of mtx after a failed operation.
int vtonceerror(Vtonce_t* once);
This function returns the error status of once after a failed operation.
AUTHOR
Kiem-Phong Vo, kpv@research.att.com