#include <pthread.h>
Go to the source code of this file.
Defines | |
#define | CBTHRFUNCTYPE void* |
OS dependend return type of a thread function. | |
#define | CBTHREND { pthread_exit(NULL); return NULL; } |
Typedefs | |
typedef pthread_t | t_cbthr |
OS dependend type of the thread handle. | |
typedef pthread_mutex_t | t_cbthrcs |
OS dependend type for a critical section. | |
typedef CBTHRFUNCTYPE(*) | t_cbthrfunc (void *) |
OS dependend type of the thread function pointer. | |
Functions | |
int | cbthr_create (t_cbthr *theThread, t_cbthrfunc aThrFunc, void *thrFuncParms) |
Creates a new thread. | |
int | cbthr_join (t_cbthr aThread) |
Joins the thread. | |
int | cbthr_init_cs (t_cbthrcs *aCritSec) |
Initializes the given critical section. | |
int | cbthr_enter_cs (t_cbthrcs *aCritSec) |
Enters a critical section. | |
int | cbthr_leave_cs (t_cbthrcs *aCritSec) |
Leaves a critical section. | |
int | cbthr_delete_cs (t_cbthrcs *aCritSec) |
Releases resources occupied by the critical section. | |
Variables | |
const t_cbthr | CBINVALIDTHREAD |
Uses posix threads for UNIX and Windows Threads for Win32. Example of use
#include "cbthr.h" #include <stdio.h> static CBTHRFUNCTYPE _func(void *params) { unsigned int thr_id; int *iparam; if (params != NULL) { iparam = (int *) params; printf("Thread param %d\n", *iparam); } CBTHREND; } int main() { t_cbthr thr_handle[4]; int thr_param[4]; int i, j; for(i=0; i<4; i++) thr_param[i] = 10 + i; for(i=0; i<4; i++) { if (cbthr_create(&thr_handle[i], _func, &thr_param[i]) == 0) fprintf(stderr, "Thread create error\n"); } for(j=0; j<i; j++) if (cbthr_join(thr_handle[j]) == 0) fprintf(stderr, "Thread join error\n"); return 0; }
#define CBTHREND { pthread_exit(NULL); return NULL; } |
#define CBTHRFUNCTYPE void* |
OS dependend return type of a thread function.
OS dependend type of the thread handle.
OS dependend type for a critical section.
Example of use:
#include "cbthr.h" static t_cbthrcs cs; static CBTHRFUNCTYPE _func(void *params) { ... cbthr_enter_cs(&cs); ... cbthr_leave_cs(&cs); } ... int main() { cbthr_init_cs(&cs); ... }
OS dependend type of the thread function pointer.
int cbthr_create | ( | t_cbthr * | theThread, | |
t_cbthrfunc | aThrFunc, | |||
void * | thrFuncParms | |||
) |
Creates a new thread.
theThread | out, the created thread handle. | |
aThrFunc | in, pointer to the thread function. | |
thrFuncParms | in, parameter for the thread function. |
int cbthr_delete_cs | ( | t_cbthrcs * | aCritSec | ) |
Releases resources occupied by the critical section.
aCritSec | pointer to the critical section to release |
int cbthr_enter_cs | ( | t_cbthrcs * | aCritSec | ) |
Enters a critical section.
aCritSec | pointer to the critical section variable. |
int cbthr_init_cs | ( | t_cbthrcs * | aCritSec | ) |
Initializes the given critical section.
An instance of a critical section variable should be defined globally. This function is called once before subsequent calls to NC_thread_enter_cs() and NC_thread_leave_cs().
The function does nothing, if the given pointer is NULL.
aCritSec | pointer fo the critical section variable. |
int cbthr_join | ( | t_cbthr | aThread | ) |
Joins the thread.
aThread | handle of the thread to join. |
int cbthr_leave_cs | ( | t_cbthrcs * | aCritSec | ) |
Leaves a critical section.
Releases the critical section for other threads.
aCritSec | pointer to the critical section variable. |
const t_cbthr CBINVALIDTHREAD |