📄 group__g__threads.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>OCILIB (C Driver for Oracle): Threads and mutexes</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.4 --><div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li><a href="files.html"><span>Files</span></a></li> </ul></div><h1>Threads and mutexes</h1><hr><a name="_details"></a><h2>Detailed Description</h2>Oracle proposes a portable implementation of Mutex and Thread objects<p>OCILIB implements these OCI features for portable multithreading support.<p>Mutexes are designed for mutual exclusion between thread in order to lock resources temporarily<p>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.<p>OCILIB exposes the types <a class="el" href="struct_o_c_i___mutex.html" title="OCILIB encapsulation of OCI mutexes.">OCI_Mutex</a>, <a class="el" href="struct_o_c_i___thread.html" title="OCILIB encapsulation of OCI Threads.">OCI_Thread</a><p><dl class="warning" compact><dt><b>Warning:</b></dt><dd>OCILIB MUST be initialized with OCI_ENV_THREADED to enable threads support<p><a class="el" href="struct_o_c_i___thread.html" title="OCILIB encapsulation of OCI Threads.">OCI_Thread</a> relies on Oracle API which uses natives threading capabilities of the supported platform<p>Using <a class="el" href="struct_o_c_i___mutex.html" title="OCILIB encapsulation of OCI mutexes.">OCI_Mutex</a> :<ul><li>On Microsoft Windows, a thread can call <a class="el" href="group__g__threads.html#g88a05b41e24b3bd1789688702cc6f4f4" title="Acquire a mutex lock.">OCI_MutexAcquire()</a> more than once wihtout any blocking. Just be sure that there is an <a class="el" href="group__g__threads.html#g366f287cf828533617faf0785181de7f" title="Release a mutex lock.">OCI_MutexRelease()</a> for every <a class="el" href="group__g__threads.html#g88a05b41e24b3bd1789688702cc6f4f4" title="Acquire a mutex lock.">OCI_MutexAcquire()</a> call</li><li>On Unixes, a thread MUST call <a class="el" href="group__g__threads.html#g366f287cf828533617faf0785181de7f" title="Release a mutex lock.">OCI_MutexRelease()</a> after every call to <a class="el" href="group__g__threads.html#g88a05b41e24b3bd1789688702cc6f4f4" title="Acquire a mutex lock.">OCI_MutexAcquire()</a> in order to be able to call <a class="el" href="group__g__threads.html#g88a05b41e24b3bd1789688702cc6f4f4" title="Acquire a mutex lock.">OCI_MutexAcquire()</a> again. If not, it will be blocked...</li></ul></dd></dl><dl class="user" compact><dt><b>Example</b></dt><dd><div class="fragment"><pre class="fragment"><span class="preprocessor">#include "ocilib.h"</span><span class="preprocessor">#define MAX_THREADS 50</span><span class="preprocessor"></span><span class="keywordtype">void</span> key_cleanup(<span class="keywordtype">void</span> *str){ free(str);}<span class="keywordtype">void</span> worker(<a class="code" href="struct_o_c_i___thread.html" title="OCILIB encapsulation of OCI Threads.">OCI_Thread</a> *thread, <span class="keywordtype">void</span> *data){ <span class="keyword">const</span> <span class="keywordtype">void</span> *<span class="keywordtype">id</span> = <a class="code" href="group__g__handles.html#g94df890665f1f2ea39c3f4f4a00f9987" title="Return OCI Thread ID (OCIThreadId *) of an OCILIB OCI_Thread object.">OCI_HandleGetThreadID</a>(thread); <span class="keywordtype">char</span> *str = malloc(50); sprintf(str, <span class="stringliteral">"thread %p !\n"</span>, <span class="keywordtype">id</span>); <a class="code" href="group__g__threads.html#g50e666be8c5d062a80d81969bbd1fc67" title="Set a thread key value.">OCI_ThreadKeySetValue</a>(<span class="stringliteral">"ID"</span>, str); <span class="comment">/* ... do some more processing here... */</span> str = <a class="code" href="group__g__threads.html#g368b5e7ed998dc36f3ab8bdfb32c1c0a" title="Get a thread key value.">OCI_ThreadKeyGetValue</a>(<span class="stringliteral">"ID"</span>); printf(str);}<span class="keywordtype">int</span> main(<span class="keywordtype">void</span>){ <a class="code" href="struct_o_c_i___thread.html" title="OCILIB encapsulation of OCI Threads.">OCI_Thread</a> *th[MAX_THREADS]; <span class="keywordtype">int</span> i; <span class="keywordflow">if</span> (!<a class="code" href="group__g__init.html#gcdb642d75f7c8478e083634144bc813c" title="Initializes the library.">OCI_Initialize</a>(NULL, NULL, OCI_ENV_DEFAULT | OCI_ENV_THREADED)) <span class="keywordflow">return</span> EXIT_FAILURE; <a class="code" href="group__g__threads.html#gf0e4d19a364f990075f2718b80a05bd5" title="Create a thread key object.">OCI_ThreadKeyCreate</a>(<span class="stringliteral">"ID"</span>, key_cleanup); <span class="comment">/* create threads */</span> <span class="keywordflow">for</span> (i = 0; i < MAX_THREADS; i++) { th[i] = <a class="code" href="group__g__threads.html#g1a2474e874215e0b42109ba042de5e3a" title="Create a Thread object.">OCI_ThreadCreate</a>(); <a class="code" href="group__g__threads.html#g1076c9942cc395f298ca350021031bfd" title="Execute the given routine within the given thread object.">OCI_ThreadRun</a>(th[i], worker, NULL); } <span class="comment">/* wait for threads and cleanup */</span> <span class="keywordflow">for</span> (i = 0; i < MAX_THREADS; i++) { <a class="code" href="group__g__threads.html#gdfec0263c6b8d508126a806c85898fb9" title="Join the given thread.">OCI_ThreadJoin</a>(th[i]); <a class="code" href="group__g__threads.html#gda7fa41aeedc7be2ce4f40c93ece524d" title="Destroy a thread object.">OCI_ThreadFree</a>(th[i]); } <a class="code" href="group__g__init.html#g639706aa8e9689c7ebffc018fac6d3ae" title="Clean up all resources allocated by the library.">OCI_Cleanup</a>(); <span class="keywordflow">return</span> EXIT_SUCCESS;}</pre></div> </dd></dl><p><table border="0" cellpadding="0" cellspacing="0"><tr><td></td></tr><tr><td colspan="2"><br><h2>Functions</h2></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT <a class="el" href="struct_o_c_i___mutex.html">OCI_Mutex</a> *OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__threads.html#gb0580aafc0e70358742e7c1e269895d5">OCI_MutexCreate</a> (void)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Create a Mutex object. <a href="#gb0580aafc0e70358742e7c1e269895d5"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT boolean OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__threads.html#g90c5cb68c0144b7bda9b75906c752ab5">OCI_MutexFree</a> (<a class="el" href="struct_o_c_i___mutex.html">OCI_Mutex</a> *mutex)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Destroy a mutex object. <a href="#g90c5cb68c0144b7bda9b75906c752ab5"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT boolean OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__threads.html#g88a05b41e24b3bd1789688702cc6f4f4">OCI_MutexAcquire</a> (<a class="el" href="struct_o_c_i___mutex.html">OCI_Mutex</a> *mutex)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Acquire a mutex lock. <a href="#g88a05b41e24b3bd1789688702cc6f4f4"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT boolean OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__threads.html#g366f287cf828533617faf0785181de7f">OCI_MutexRelease</a> (<a class="el" href="struct_o_c_i___mutex.html">OCI_Mutex</a> *mutex)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Release a mutex lock. <a href="#g366f287cf828533617faf0785181de7f"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT <a class="el" href="struct_o_c_i___thread.html">OCI_Thread</a> *OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__threads.html#g1a2474e874215e0b42109ba042de5e3a">OCI_ThreadCreate</a> (void)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Create a Thread object. <a href="#g1a2474e874215e0b42109ba042de5e3a"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT boolean OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__threads.html#gda7fa41aeedc7be2ce4f40c93ece524d">OCI_ThreadFree</a> (<a class="el" href="struct_o_c_i___thread.html">OCI_Thread</a> *thread)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Destroy a thread object. <a href="#gda7fa41aeedc7be2ce4f40c93ece524d"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT boolean OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__threads.html#g1076c9942cc395f298ca350021031bfd">OCI_ThreadRun</a> (<a class="el" href="struct_o_c_i___thread.html">OCI_Thread</a> *thread, POCI_THREAD proc, void *arg)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Execute the given routine within the given thread object. <a href="#g1076c9942cc395f298ca350021031bfd"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT boolean OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__threads.html#gdfec0263c6b8d508126a806c85898fb9">OCI_ThreadJoin</a> (<a class="el" href="struct_o_c_i___thread.html">OCI_Thread</a> *thread)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Join the given thread. <a href="#gdfec0263c6b8d508126a806c85898fb9"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT boolean OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__threads.html#gf0e4d19a364f990075f2718b80a05bd5">OCI_ThreadKeyCreate</a> (const mtext *name, POCI_THREADKEYDEST destfunc)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Create a thread key object. <a href="#gf0e4d19a364f990075f2718b80a05bd5"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT boolean OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__threads.html#g50e666be8c5d062a80d81969bbd1fc67">OCI_ThreadKeySetValue</a> (const mtext *name, void *value)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Set a thread key value. <a href="#g50e666be8c5d062a80d81969bbd1fc67"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT void *OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__threads.html#g368b5e7ed998dc36f3ab8bdfb32c1c0a">OCI_ThreadKeyGetValue</a> (const mtext *name)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Get a thread key value. <a href="#g368b5e7ed998dc36f3ab8bdfb32c1c0a"></a><br></td></tr></table><hr><h2>Function Documentation</h2><a class="anchor" name="g88a05b41e24b3bd1789688702cc6f4f4"></a><!-- doxytag: member="ocilib.h::OCI_MutexAcquire" ref="g88a05b41e24b3bd1789688702cc6f4f4" args="(OCI_Mutex *mutex)" --><div class="memitem"><div class="memproto"> <table class="memname"> <tr> <td class="memname">OCI_EXPORT boolean OCI_API OCI_MutexAcquire </td> <td>(</td> <td class="paramtype"><a class="el" href="struct_o_c_i___mutex.html">OCI_Mutex</a> * </td> <td class="paramname"> <em>mutex</em> </td> <td> ) </td> <td width="100%"></td> </tr> </table></div><div class="memdoc"><p>Acquire a mutex lock. <p><dl compact><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td valign="top"></td><td valign="top"><em>mutex</em> </td><td>- Mutex handle</td></tr> </table></dl><dl class="return" compact><dt><b>Returns:</b></dt><dd>TRUE on success otherwise FALSE </dd></dl><p>Definition at line <a class="el" href="mutex_8c-source.html#l00147">147</a> of file <a class="el" href="mutex_8c-source.html">mutex.c</a>.</p><p>References <a class="el" href="ocilib__types_8h-source.html#l00151">OCI_Mutex::err</a>, and <a class="el" href="ocilib__types_8h-source.html#l00150">OCI_Mutex::handle</a>.</p><p>Referenced by <a class="el" href="group__g__connpool.html#g774ce156deb3477c83237a633b6538f6">OCI_ConnPoolGetConnection()</a>.</p></div></div><p><a class="anchor" name="gb0580aafc0e70358742e7c1e269895d5"></a><!-- doxytag: member="ocilib.h::OCI_MutexCreate" ref="gb0580aafc0e70358742e7c1e269895d5" args="(void)" --><div class="memitem"><div class="memproto"> <table class="memname"> <tr> <td class="memname">OCI_EXPORT <a class="el" href="struct_o_c_i___mutex.html">OCI_Mutex</a>* OCI_API OCI_MutexCreate </td> <td>(</td> <td class="paramtype">void </td> <td class="paramname"> </td> <td> ) </td> <td width="100%"></td> </tr> </table></div><div class="memdoc"><p>Create a Mutex object. <p><dl class="return" compact><dt><b>Returns:</b></dt><dd>Mutex handle on success or NULL on failure </dd></dl><p>Definition at line <a class="el" href="mutex_8c-source.html#l00092">92</a> of file <a class="el" href="mutex_8c-source.html">mutex.c</a>.</p></div></div><p><a class="anchor" name="g90c5cb68c0144b7bda9b75906c752ab5"></a><!-- doxytag: member="ocilib.h::OCI_MutexFree" ref="g90c5cb68c0144b7bda9b75906c752ab5" args="(OCI_Mutex *mutex)" --><div class="memitem"><div class="memproto"> <table class="memname"> <tr> <td class="memname">OCI_EXPORT boolean OCI_API OCI_MutexFree </td> <td>(</td> <td class="paramtype"><a class="el" href="struct_o_c_i___mutex.html">OCI_Mutex</a> * </td> <td class="paramname"> <em>mutex</em> </td> <td> ) </td> <td width="100%"></td> </tr> </table></div><div class="memdoc"><p>Destroy a mutex object. <p><dl compact><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td valign="top"></td><td valign="top"><em>mutex</em> </td><td>- Mutex handle</td></tr> </table></dl><dl class="return" compact><dt><b>Returns:</b></dt><dd>TRUE on success otherwise FALSE </dd></dl><p>Definition at line <a class="el" href="mutex_8c-source.html#l00109">109</a> of file <a class="el" href="mutex_8c-source.html">mutex.c</a>.</p><p>References <a class="el" href="ocilib__types_8h-source.html#l00151">OCI_Mutex::err</a>, and <a class="el" href="ocilib__types_8h-source.html#l00150">OCI_Mutex::handle</a>.</p></div></div><p><a class="anchor" name="g366f287cf828533617faf0785181de7f"></a><!-- doxytag: member="ocilib.h::OCI_MutexRelease" ref="g366f287cf828533617faf0785181de7f" args="(OCI_Mutex *mutex)" --><div class="memitem"><div class="memproto"> <table class="memname"> <tr> <td class="memname">OCI_EXPORT boolean OCI_API OCI_MutexRelease </td> <td>(</td> <td class="paramtype"><a class="el" href="struct_o_c_i___mutex.html">OCI_Mutex</a> * </td> <td class="paramname"> <em>mutex</em> </td> <td> ) </td> <td width="100%"></td> </tr> </table></div><div class="memdoc"><p>Release a mutex lock. <p><dl compact><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td valign="top"></td><td valign="top"><em>mutex</em> </td><td>- Mutex handle</td></tr> </table></dl><dl class="return" compact><dt><b>Returns:</b></dt><dd>TRUE on success otherwise FALSE </dd></dl><p>Definition at line <a class="el" href="mutex_8c-source.html#l00169">169</a> of file <a class="el" href="mutex_8c-source.html">mutex.c</a>.</p><p>References <a class="el" href="ocilib__types_8h-source.html#l00151">OCI_Mutex::err</a>, and <a class="el" href="ocilib__types_8h-source.html#l00150">OCI_Mutex::handle</a>.</p><p>Referenced by <a class="el" href="group__g__connpool.html#g774ce156deb3477c83237a633b6538f6">OCI_ConnPoolGetConnection()</a>.</p></div></div><p><a class="anchor" name="g1a2474e874215e0b42109ba042de5e3a"></a><!-- doxytag: member="ocilib.h::OCI_ThreadCreate" ref="g1a2474e874215e0b42109ba042de5e3a" args="(void)" --><div class="memitem"><div class="memproto"> <table class="memname"> <tr> <td class="memname">OCI_EXPORT <a class="el" href="struct_o_c_i___thread.html">OCI_Thread</a>* OCI_API OCI_ThreadCreate </td> <td>(</td> <td class="paramtype">void </td> <td class="paramname"> </td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -