OCILIB (C Driver for Oracle) 3.9.2
|
Oracle proposes a portable implementation of Mutex and Thread objects
OCILIB implements these OCI features for portable multithreading support.
Mutexes are designed for mutual exclusion between thread in order to lock resources temporarily
Thread keys can be seen as process-wide variables that have a thread-specific values. It allows to create a unique key identified by a name (string) that can store values specific to each thread.
OCILIB exposes the types OCI_Mutex, OCI_Thread
#include "ocilib.h" #define MAX_THREADS 50 void key_cleanup(void *str) { free(str); } void worker(OCI_Thread *thread, void *data) { const void *id = OCI_HandleGetThreadID(thread); char *str = malloc(50); sprintf(str, "thread %p !\n", id); OCI_ThreadKeySetValue("ID", str); /* ... do some more processing here... */ str = OCI_ThreadKeyGetValue("ID"); printf(str); } int main(void) { OCI_Thread *th[MAX_THREADS]; int i; if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT | OCI_ENV_THREADED)) return EXIT_FAILURE; OCI_ThreadKeyCreate("ID", key_cleanup); /* create threads */ for (i = 0; i < MAX_THREADS; i++) { th[i] = OCI_ThreadCreate(); OCI_ThreadRun(th[i], worker, NULL); } /* wait for threads and cleanup */ for (i = 0; i < MAX_THREADS; i++) { OCI_ThreadJoin(th[i]); OCI_ThreadFree(th[i]); } OCI_Cleanup(); return EXIT_SUCCESS; }
Functions | |
OCI_EXPORT OCI_Mutex *OCI_API | OCI_MutexCreate (void) |
Create a Mutex object. | |
OCI_EXPORT boolean OCI_API | OCI_MutexFree (OCI_Mutex *mutex) |
Destroy a mutex object. | |
OCI_EXPORT boolean OCI_API | OCI_MutexAcquire (OCI_Mutex *mutex) |
Acquire a mutex lock. | |
OCI_EXPORT boolean OCI_API | OCI_MutexRelease (OCI_Mutex *mutex) |
Release a mutex lock. | |
OCI_EXPORT OCI_Thread *OCI_API | OCI_ThreadCreate (void) |
Create a Thread object. | |
OCI_EXPORT boolean OCI_API | OCI_ThreadFree (OCI_Thread *thread) |
Destroy a thread object. | |
OCI_EXPORT boolean OCI_API | OCI_ThreadRun (OCI_Thread *thread, POCI_THREAD proc, void *arg) |
Execute the given routine within the given thread object. | |
OCI_EXPORT boolean OCI_API | OCI_ThreadJoin (OCI_Thread *thread) |
Join the given thread. | |
OCI_EXPORT boolean OCI_API | OCI_ThreadKeyCreate (const mtext *name, POCI_THREADKEYDEST destfunc) |
Create a thread key object. | |
OCI_EXPORT boolean OCI_API | OCI_ThreadKeySetValue (const mtext *name, void *value) |
Set a thread key value. | |
OCI_EXPORT void *OCI_API | OCI_ThreadKeyGetValue (const mtext *name) |
Get a thread key value. |
OCI_EXPORT OCI_Mutex* OCI_API OCI_MutexCreate | ( | void | ) |
OCI_EXPORT boolean OCI_API OCI_MutexFree | ( | OCI_Mutex * | mutex | ) |
OCI_EXPORT boolean OCI_API OCI_MutexAcquire | ( | OCI_Mutex * | mutex | ) |
Acquire a mutex lock.
mutex | - Mutex handle |
Definition at line 157 of file mutex.c.
Referenced by OCI_PoolGetConnection().
OCI_EXPORT boolean OCI_API OCI_MutexRelease | ( | OCI_Mutex * | mutex | ) |
Release a mutex lock.
mutex | - Mutex handle |
Definition at line 182 of file mutex.c.
Referenced by OCI_PoolGetConnection().
OCI_EXPORT OCI_Thread* OCI_API OCI_ThreadCreate | ( | void | ) |
Create a Thread object.
Definition at line 67 of file thread.c.
References OCI_ThreadFree().
OCI_EXPORT boolean OCI_API OCI_ThreadFree | ( | OCI_Thread * | thread | ) |
Destroy a thread object.
thread | - Thread handle |
Definition at line 130 of file thread.c.
Referenced by OCI_ThreadCreate().
OCI_EXPORT boolean OCI_API OCI_ThreadRun | ( | OCI_Thread * | thread, |
POCI_THREAD | proc, | ||
void * | arg | ||
) |
OCI_EXPORT boolean OCI_API OCI_ThreadJoin | ( | OCI_Thread * | thread | ) |
OCI_EXPORT boolean OCI_API OCI_ThreadKeyCreate | ( | const mtext * | name, |
POCI_THREADKEYDEST | destfunc | ||
) |
Create a thread key object.
name | - Thread key name |
destfunc | - Thread key value destructor function |
Definition at line 189 of file threadkey.c.
References OCI_HashAddPointer(), and OCI_HashCreate().
OCI_EXPORT boolean OCI_API OCI_ThreadKeySetValue | ( | const mtext * | name, |
void * | value | ||
) |
Set a thread key value.
name | - Thread key name |
value | - user value to set |
Definition at line 247 of file threadkey.c.
References OCI_HashGetPointer().
OCI_EXPORT void* OCI_API OCI_ThreadKeyGetValue | ( | const mtext * | name | ) |
Get a thread key value.
name | - Thread key name |
Definition at line 271 of file threadkey.c.
References OCI_HashGetPointer().