c - Thread-safe global sqlca struct for Oracle database access -


i have multi-threaded (posix thread or pthread based) c application uses oracle pro c precompiler. application uses global sqlca struct. in 1 .c file, includes global sqlca struct definition oracle database access as:

   #include <sqlca.h> 

and in other ,c files, uses follows:

   #define sqlca_storage_class extern    #include <sqlca.h> 

my question if more 1 thread tries access database table query, insert or update , uses global sqlca object, how guarantee mutual exclusion or make thread-safe access? also, when query vs insert / update, use sqlca struct?

you have global function grabs mutex object, , global function returns mutex object.

the thread needs use database attempts call function grabs mutex. if mutex in use, function returns 0. if mutex available, grab mutex function marks mutex unavailable, , returns 1 success.

any subsequent calls grab mutex fail, until thread grabbed mutex calls return mutex function.

any thread attempts , fails grab mutex can put loop until mutex grabbed (ie thread wait mutex). time out can instituted here.

the mutex object can simple bool, or can use other more complex mutexes (windows.h has mutex objects).

if create own mutex, vitally important volatile.

no thread may access database if doesn't have possession of mutex.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -