cbthr.h File Reference

OS independend thread implementation. More...

#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


Detailed Description

OS independend thread implementation.

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;
 }
Author:
Daniel Stoinski, Nagler & Company
Version:
Revision
48

Define Documentation

#define CBTHREND   { pthread_exit(NULL); return NULL; }

#define CBTHRFUNCTYPE   void*

OS dependend return type of a thread function.


Typedef Documentation

t_cbthr

OS dependend type of the thread handle.

t_cbthrcs

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);
   ...
 }

t_cbthrfunc

OS dependend type of the thread function pointer.


Function Documentation

int cbthr_create ( t_cbthr theThread,
t_cbthrfunc  aThrFunc,
void *  thrFuncParms 
)

Creates a new thread.

Parameters:
theThread out, the created thread handle.
aThrFunc in, pointer to the thread function.
thrFuncParms in, parameter for the thread function.
Returns:
!= 0 for OK, 0 for error.

int cbthr_delete_cs ( t_cbthrcs aCritSec  ) 

Releases resources occupied by the critical section.

Parameters:
aCritSec pointer to the critical section to release
Returns:
0 on success, != 0 on error.

int cbthr_enter_cs ( t_cbthrcs aCritSec  ) 

Enters a critical section.

Parameters:
aCritSec pointer to the critical section variable.
Returns:
0 on success, != 0 on error.

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.

Parameters:
aCritSec pointer fo the critical section variable.
Returns:
0 on success, != 0 on error.
See also:
cbthr_enter_cs()

cbthr_leave_cs()

t_cbthrcs

int cbthr_join ( t_cbthr  aThread  ) 

Joins the thread.

Parameters:
aThread handle of the thread to join.
Returns:
!= 0 for OK, 0 for error.

int cbthr_leave_cs ( t_cbthrcs aCritSec  ) 

Leaves a critical section.

Releases the critical section for other threads.

Parameters:
aCritSec pointer to the critical section variable.
Returns:
0 on success, != 0 on error.


Variable Documentation

const t_cbthr CBINVALIDTHREAD


Generated on Thu Feb 12 11:09:26 2009 for cb by  doxygen 1.4.7