📄 libs7.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Quadralay WebWorks Publisher Professional Edition 6.0.5">
<meta name="TEMPLATEBASE" content="book_html">
<meta name="LASTUPDATED" content="12/04/03 16:06:54">
<title>15.3.2 How to use the concurrency support </title>
<STYLE TYPE="text/css">
<!--
span.Signal { text-transform: uppercase; font-family: Verdana }
-->
</STYLE>
</head>
<body link="#3366CC" vlink="#9999CC" text="#000000" alink="#0000CC" bgcolor="#FFFFFF"
background="images/backgrnd.gif">
<p><img src="images/stlogo.gif" width="106" height="83" align="left"
alt="logo here!"> </p>
<table width="331" border="0" align="right" cellpadding="0" cellspacing="0">
<tr>
<td><a href="a_refman.htm"><img src="images/navtoc.gif" width="84" height="23"
border="0" alt="TOC"> </a></td>
<td><a href="libs6.htm"><img src="images/navprev.gif" width="80" height="23"
border="0" alt="PREV"> </a></td>
<td><a href="libs8.htm"><img src="images/navnext.gif" width="83" height="23"
border="0" alt="NEXT"> </a></td>
<td><a href="a_refmaa.htm"><img src="images/navidx.gif" width="84" height="23"
border="0" alt="INDEX"> </a></td>
</tr>
</table>
<p><br clear="all">
</p>
<hr align="left">
<blockquote>
<h3>
<a name="1205083"> </a><font color="#003366" face="Verdana, Arial, Helvetica, sans-serif">15.3.2 How to use the concurrency support</font>
</h3>
<p>
<a name="1205084"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">The concurrency support in the <font size=2 face=Courier><strong>stdio</strong></font> library requires the user to supply two functions, one of which is called for operations on the file table, and the other is called for operations on individual files.</font>
</p>
<p>
<a name="1205085"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">The protection function for the <font size=2 face=Courier><strong>stdio</strong></font> file table is installed using the library function <font size=2 face=Courier><strong>setstdiofiletablemutexfn()</strong></font>. The function which this installs is called by the <font size=2 face=Courier><strong>stdio</strong></font> library before any operations on the file table, with a parameter of <font size=2 face=Courier><strong>stdiofiletablemutexop_lock</strong></font>. Once the operation has been completed, the function is called again with a parameter of <font size=2 face=Courier><strong>stdiofiletablemutexop_unlock</strong></font>.</font>
</p>
<p>
<a name="1205086"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">Typically this is used to claim and release a semaphore, to ensure that only one thread is manipulating the file table at any given time.</font>
</p>
<p>
<a name="1205087"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">The protection facility for individual files is a little more complex. As well as a function which is called by the <font size=2 face=Courier><strong>stdio</strong></font> library before and after any operations on the file, it is also possible to associate a mutex with each <font size=2 face=Courier><strong>FILE</strong></font>. The mutex is represented within the <font size=2 face=Courier><strong>stdio</strong></font> library as a <font size=2 face=Courier><strong>void*</strong></font>, which the user can then cast to a pointer to whatever mutex is required.</font>
</p>
<p>
<a name="1205089"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">The mutex function for file operations is called with the argument:</font>
</p>
<ul>
<p> <font size=2 face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1205090"> </a><font size=2 face=Courier><strong>stdiomutexop_init</strong></font> when a <font size=2 face=Courier><strong>FILE*</strong></font> should have a new mutex associated with it,</font></p>
<p> <font size=2 face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1205091"> </a><font size=2 face=Courier><strong>stdiomutexop_deinit</strong></font> when a <font size=2 face=Courier><strong>FILE*</strong></font> should have its mutex disassociated with it and possibly deleted.</font></p>
</ul>
<p>
<a name="1205092"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">The mutex to be used with each <font size=2 face=Courier><strong>FILE</strong></font> is set using the <font size=2 face=Courier><strong>setstdiomutex()</strong></font> library function, and can be retrieved using <font size=2 face=Courier><strong>getstdiomutex()</strong></font>.</font>
</p>
<p>
<a name="1205093"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">The mutex function for file operations is installed using the <font size=2 face=Courier><strong>setstdiomutexfn()</strong></font> library function. This is called by the <font size=2 face=Courier><strong>stdio</strong></font> library before a file operation with an <font size=2 face=Courier><strong>op</strong></font> parameter of <font size=2 face=Courier><strong>stdiomutexop_lock</strong></font>. Once the operation has been completed, the function is called a second time with a parameter of <font size=2 face=Courier><strong>stdiomutexop_unlock</strong></font>.</font>
</p>
<p>
<a name="1205094"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">The file mutex function simply claims the mutex when called with an <font size=2 face=Courier><strong>op</strong></font> parameter of <font size=2 face=Courier><strong>stdiomutexop_lock</strong></font>, and releases the mutex when called with a parameter of <font size=2 face=Courier><strong>stdiomutexop_unlock</strong></font>.</font>
</p>
<p>
<a name="1205095"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">The per <font size=2 face=Courier><strong>FILE*</strong></font> mutex callback may be called for each open <font size=2 face=Courier><strong>FILE*</strong></font> with the operation <font size=2 face=Courier><strong>stdiomutexop_init</strong></font> by calling the function <font size=2 face=Courier><strong>_ST_ApplyStdioFileMutex</strong></font>.</font>
</p>
<h5>
<a name="1213957"> </a><i><font color="#003366" face="Verdana, Arial, Helvetica, sans-serif">Example</font></i>
</h5>
<p>
<a name="1213958"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">This example shows how <font size=2 face=Courier><strong>stdio</strong></font> concurrency support with OS20 would be initialized if this were not automatically done. If applied to another OS, replace the OS20-specific semaphore type and functions with equivalent constructs for the OS being used.</font>
</p>
<p>
<a name="1213962"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif"><em>Note: Protection for the device-independent and debug layers is also required.</em></font>
</p>
<a name="1213968"> </a><font size=2 face=Courier><strong><i>/*<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -