﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-记录代码点滴-随笔分类-C++学习</title><link>http://www.cppblog.com/tuantuan/category/7444.html</link><description>WEB开发,高性能服务器开发,网络编程,游戏开发 跌跌撞撞中寻找未来</description><language>zh-cn</language><lastBuildDate>Sun, 16 Aug 2009 12:41:41 GMT</lastBuildDate><pubDate>Sun, 16 Aug 2009 12:41:41 GMT</pubDate><ttl>60</ttl><item><title>pthread_create用法</title><link>http://www.cppblog.com/tuantuan/archive/2009/08/02/91970.html</link><dc:creator>谢岱唛</dc:creator><author>谢岱唛</author><pubDate>Sun, 02 Aug 2009 09:36:00 GMT</pubDate><guid>http://www.cppblog.com/tuantuan/archive/2009/08/02/91970.html</guid><wfw:comment>http://www.cppblog.com/tuantuan/comments/91970.html</wfw:comment><comments>http://www.cppblog.com/tuantuan/archive/2009/08/02/91970.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tuantuan/comments/commentRss/91970.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tuantuan/services/trackbacks/91970.html</trackback:ping><description><![CDATA[今天开始学习linux下用C开发多线程程序，Linux系统下的多线程遵循POSIX线程接口，称为pthread。<br>
<p>
<table class=allBorders cellSpacing=0 cellPadding=5 rules=none border=1>
    <tbody>
        <tr>
            <td class=docTableCell vAlign=top align=left>
            <pre>#include &lt;pthread.h&gt;<br><br>int pthread_create(pthread_t *restrict <span class=docEmphItalicAlt>tidp</span>,<br>                   const pthread_attr_t *restrict <span class=docEmphItalicAlt>attr</span>,<br>                   void *(*<span class=docEmphItalicAlt>start_rtn</span>)(void), <br>                   void *restrict <span class=docEmphItalicAlt>arg</span>);<br></pre>
            </td>
        </tr>
        <tr>
            <td class=docTableCell vAlign=top align=right>
            <p class=docText>Returns: 0 if OK, error number on failure</p>
            </td>
        </tr>
    </tbody>
</table>
</p>
<p>C99 中新增加了 restrict 修饰的指针： 由 restrict 修饰的指针是最初唯一对指针所指向的对象进行存取的方法，仅当第二个指针基于第一个时，才能对对象进行存取。对对象的存取都限定于基于由 restrict 修饰的指针表达式中。 由 restrict 修饰的指针主要用于函数形参，或指向由 malloc() 分配的内存空间。restrict 数据类型不改变程序的语义。 编译器能通过作出 restrict 修饰的指针是存取对象的唯一方法的假设，更好地优化某些类型的例程。 </p>
第一个参数为指向线程标识符的指针。<br>第二个参数用来设置线程属性。<br>第三个参数是线程运行函数的起始地址。<br>最后一个参数是运行函数的参数。<br><br>下面这个程序中，我们的函数<code><span style="COLOR: rgb(0,0,0)">thr_fn<span style="COLOR: rgb(0,0,204)"></span></span></code>不需要参数，所以最后一个参数设为空指针。第二个参数我们也设为空指针，这样将生成默认属性的线程。当创建线程成功时，函数返回0，若不为0则说明创建线程失败，常见的错误返回代码为EAGAIN和EINVAL。前者表示系统限制创建新的线程，例如线程数目过多了；后者表示第二个参数代表的线程属性值非法。创建线程成功后，新创建的线程则运行参数三和参数四确定的函数，原来的线程则继续运行下一行代码。
<table style="BORDER-COLLAPSE: collapse" borderColor=#e99999 cellSpacing=0 cellPadding=0 width="95%" bgColor=#f1f1f1 border=1>
    <tbody>
        <tr>
            <td>
            <p style="MARGIN: 5px; LINE-HEIGHT: 150%"><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,204)">#</span><span style="COLOR: rgb(255,0,0)">include</span><span style="COLOR: rgb(0,0,204)">&lt;</span>stdio<span style="COLOR: rgb(0,0,204)">.</span>h<span style="COLOR: rgb(0,0,204)">&gt;</span><br><span style="COLOR: rgb(0,0,204)">#</span><span style="COLOR: rgb(255,0,0)">include</span><span style="COLOR: rgb(0,0,204)">&lt;</span>pthread<span style="COLOR: rgb(0,0,204)">.</span>h<span style="COLOR: rgb(0,0,204)">&gt;<br></span></span></code><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,204)">#</span><span style="COLOR: rgb(255,0,0)">include</span><span style="COLOR: rgb(0,0,204)">&lt;</span>string<span style="COLOR: rgb(0,0,204)">.</span>h<span style="COLOR: rgb(0,0,204)">&gt;<br>#</span><span style="COLOR: rgb(255,0,0)">include</span><span style="COLOR: rgb(0,0,204)">&lt;</span>sys/types<span style="COLOR: rgb(0,0,204)">.</span>h<span style="COLOR: rgb(0,0,204)">&gt;<br>#</span><span style="COLOR: rgb(255,0,0)">include</span><span style="COLOR: rgb(0,0,204)">&lt;</span>unistd<span style="COLOR: rgb(0,0,204)">.</span>h<span style="COLOR: rgb(0,0,204)">&gt;</span></span></code><br><code><span style="COLOR: rgb(0,0,0)"><br><span style="COLOR: rgb(255,0,0)">pthread_t</span> ntid<span style="COLOR: rgb(0,0,204)">;</span><br><br><span style="COLOR: rgb(0,0,255)">void</span> printids<span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(0,0,255)">const</span> <span style="COLOR: rgb(0,0,255)">char</span> <span style="COLOR: rgb(0,0,204)">*</span>s<span style="COLOR: rgb(0,0,204)">)</span><span style="COLOR: rgb(0,0,204)">{</span><br><span style="COLOR: rgb(255,0,0)">pid_t</span> pid<span style="COLOR: rgb(0,0,204)">;</span><br><span style="COLOR: rgb(255,0,0)"></span></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(255,0,0)"> pthread_t</span> tid<span style="COLOR: rgb(0,0,204)">;</span><br><br></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"> pid <span style="COLOR: rgb(0,0,204)">=</span> getpid<span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(0,0,204)">)</span><span style="COLOR: rgb(0,0,204)">;</span><br></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"> tid <span style="COLOR: rgb(0,0,204)">=</span> pthread_self<span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(0,0,204)">)</span><span style="COLOR: rgb(0,0,204)">;</span><br><span style="COLOR: rgb(255,0,0)"></span></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(255,0,0)"> printf</span><span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(255,0,255)">"%s pid %u tid %u (0x%x)\n"</span><span style="COLOR: rgb(0,0,204)">,</span>s<span style="COLOR: rgb(0,0,204)">,</span><span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(0,0,255)">unsigned</span> <span style="COLOR: rgb(0,0,255)">int</span><span style="COLOR: rgb(0,0,204)">)</span>pid<span style="COLOR: rgb(0,0,204)">,</span><span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(0,0,255)">unsigned</span> <span style="COLOR: rgb(0,0,255)">int</span><span style="COLOR: rgb(0,0,204)">)</span>tid<span style="COLOR: rgb(0,0,204)">,</span><span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(0,0,255)">unsigned</span> <span style="COLOR: rgb(0,0,255)"><br>int</span><span style="COLOR: rgb(0,0,204)">)</span>tid<span style="COLOR: rgb(0,0,204)">)</span><span style="COLOR: rgb(0,0,204)">;</span><br><span style="COLOR: rgb(0,0,204)">}</span><br><br><span style="COLOR: rgb(0,0,255)">void</span> <span style="COLOR: rgb(0,0,204)">*</span>thr_fn<span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(0,0,255)">void</span> <span style="COLOR: rgb(0,0,204)">*</span><span style="COLOR: rgb(255,0,0)">arg</span><span style="COLOR: rgb(0,0,204)">)</span><span style="COLOR: rgb(0,0,204)">{</span><br></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"> printids<span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(255,0,255)">"new thread:"</span><span style="COLOR: rgb(0,0,204)">)</span><span style="COLOR: rgb(0,0,204)">;</span><br><span style="COLOR: rgb(0,0,255)"></span></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,255)"> return</span> <span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(0,0,255)">void</span> <span style="COLOR: rgb(0,0,204)">*</span><span style="COLOR: rgb(0,0,204)">)</span>0<span style="COLOR: rgb(0,0,204)">)</span><span style="COLOR: rgb(0,0,204)">;</span><br><span style="COLOR: rgb(0,0,204)">}</span><br><br><span style="COLOR: rgb(0,0,255)">int</span> main<span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(0,0,204)">)</span><span style="COLOR: rgb(0,0,204)">{</span><br><span style="COLOR: rgb(0,0,255)"></span></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,255)"> int</span> err<span style="COLOR: rgb(0,0,204)">;</span><br><br></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"> err <span style="COLOR: rgb(0,0,204)">=</span> <span style="COLOR: rgb(255,0,0)">pthread_create</span><span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(0,0,204)">&amp;</span>ntid<span style="COLOR: rgb(0,0,204)">,</span><span style="COLOR: rgb(255,0,0)">NULL</span><span style="COLOR: rgb(0,0,204)">,</span>thr_fn<span style="COLOR: rgb(0,0,204)">,</span><span style="COLOR: rgb(255,0,0)">NULL</span><span style="COLOR: rgb(0,0,204)">)</span><span style="COLOR: rgb(0,0,204)">;</span><br><span style="COLOR: rgb(0,0,255)"></span></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,255)"> if</span><span style="COLOR: rgb(0,0,204)">(</span>err <span style="COLOR: rgb(0,0,204)">!</span><span style="COLOR: rgb(0,0,204)">=</span> 0<span style="COLOR: rgb(0,0,204)">)</span></span></code><code><span style="COLOR: rgb(0,0,0)"></span></code><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,204)">{</span><br><span style="COLOR: rgb(255,0,0)"></span></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(255,0,0)"> printf</span><span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(255,0,255)">"can't create thread: %s\n"</span><span style="COLOR: rgb(0,0,204)">,</span>strerror(err<span style="COLOR: rgb(0,0,204)">))</span><span style="COLOR: rgb(0,0,204)">;</span><br><span style="COLOR: rgb(0,0,255)"></span></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,255)"> return</span> 1<span style="COLOR: rgb(0,0,204)">;</span><br><span style="COLOR: rgb(0,0,204)"></span></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,204)"> }</span><br><br></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"> printids<span style="COLOR: rgb(0,0,204)">(</span><span style="COLOR: rgb(255,0,255)">"main thread:"</span><span style="COLOR: rgb(0,0,204)">)</span><span style="COLOR: rgb(0,0,204)">;</span><br><span style="COLOR: rgb(255,0,0)"></span></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(255,0,0)"> sleep</span><span style="COLOR: rgb(0,0,204)">(</span>1<span style="COLOR: rgb(0,0,204)">)</span><span style="COLOR: rgb(0,0,204)">;</span><br><span style="COLOR: rgb(0,0,255)"></span></span></code><code><span style="COLOR: rgb(0,0,0)">&nbsp;</span></code><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,255)"> return</span> 0<span style="COLOR: rgb(0,0,204)">;</span><br><span style="COLOR: rgb(0,0,204)">}</span><br></span></code></p>
            </td>
        </tr>
    </tbody>
</table>
把APUE2上的一个程序修改一下，然后编译。<br>结果报错:<code><span style="COLOR: rgb(0,0,0)"><br>pthread.c:(.text+0x85)：对&#8216;pthread_create&#8217;未定义的引用</span></code><br><br>由于pthread库不是Linux系统默认的库，连接时需要使用库libpthread.a,所以在使用pthread_create创建线程时，在编译中要加-lpthread参数:<br><code><span style="COLOR: rgb(0,0,0)">gcc -o pthread -lpthread pthread.c</span></code><br>
<img src ="http://www.cppblog.com/tuantuan/aggbug/91970.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tuantuan/" target="_blank">谢岱唛</a> 2009-08-02 17:36 <a href="http://www.cppblog.com/tuantuan/archive/2009/08/02/91970.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>void指针 </title><link>http://www.cppblog.com/tuantuan/archive/2009/08/02/91968.html</link><dc:creator>谢岱唛</dc:creator><author>谢岱唛</author><pubDate>Sun, 02 Aug 2009 09:17:00 GMT</pubDate><guid>http://www.cppblog.com/tuantuan/archive/2009/08/02/91968.html</guid><wfw:comment>http://www.cppblog.com/tuantuan/comments/91968.html</wfw:comment><comments>http://www.cppblog.com/tuantuan/archive/2009/08/02/91968.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tuantuan/comments/commentRss/91968.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tuantuan/services/trackbacks/91968.html</trackback:ping><description><![CDATA[指针有两个属性:指向变量/对象的<font style="LINE-HEIGHT: 1.3em" color=#ff0000>地址</font><wbr>和<font style="LINE-HEIGHT: 1.3em" color=#ff0000>长度</font><wbr> <br>但是指针只存储地址,长度则取决于指针的类型 <br>编译器根据指针的类型从指针指向的地址向后寻址 <br>指针类型不同则寻址范围也不同,比如: <br>int*从指定地址向后寻找4字节作为变量的存储单元 <br>double*从指定地址向后寻找8字节作为变量的存储单元 <br><br>1.void指针是一种特别的指针 <br>&nbsp;&nbsp; void *vp <br>&nbsp;&nbsp;//说它特别是因为它没有类型 <br>&nbsp;&nbsp;//或者说这个类型不能判断出指向对象的长度 <br><br>2.任何指针都可以赋值给void指针 <br>&nbsp;&nbsp;type *p; <br>&nbsp;&nbsp;vp=p; <br>&nbsp;&nbsp;//不需转换 <br>&nbsp;&nbsp;//只获得变量/对象地址而不获得大小 <br><br>3.void指针赋值给其他类型的指针时都要进行转换 <br>&nbsp;&nbsp; type *p=(type*)vp; <br>&nbsp;&nbsp; //转换类型也就是获得指向变量/对象大小 <br>转:http://icoding.spaces.live.com/blog/cns!209684E38D520BA6!130.entry <br><br>4.void指针不能复引用 <br>&nbsp;&nbsp;*vp//错误 <br>&nbsp;&nbsp;因为void指针只知道,指向变量/对象的起始地址 <br>&nbsp;&nbsp;而不知道指向变量/对象的大小(占几个字节)所以无法正确引用 <br><br>5.void指针不能参与指针运算,除非进行转换 <br>&nbsp;&nbsp; (type*)vp++; <br>&nbsp;&nbsp;//vp==vp+sizeof(type)<br><br><br><br>#include&lt;iostream&gt;<br>#include&lt;stdlib.h&gt;<br>#include&lt;string&gt;<br>using namespace std;<br>typedef struct tag_st <br>{ <br>char id[10];<br>float fa[2];<br>}ST; <br>//我在程序里面这样使用的 <br>int main()<br>{<br>ST * P=(ST *)malloc(sizeof(ST));<br>strcpy(P-&gt;id,"hello!");<br>P-&gt;fa[0]=1.1;<br>P-&gt;fa[1]=2.1;<br><br>ST * Q=(ST *)malloc(sizeof(ST));<br>strcpy(Q-&gt;id,"world!");<br>Q-&gt;fa[0]=3.1;<br>Q-&gt;fa[1]=4.1;<br>void ** plink=(void **)P;<br>*((ST *)(plink)) = * Q; //<font style="LINE-HEIGHT: 1.3em" color=#ff0000>plink要先强制转换一下,目的是为了让它先知道要覆盖的大小.</font><wbr><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //<font style="LINE-HEIGHT: 1.3em" color=#ff0000>P的内容竟然给Q的内容覆盖掉了.</font><wbr><br>cout&lt;&lt;P-&gt;id&lt;&lt;" "&lt;&lt;P-&gt;fa[0]&lt;&lt;" "&lt;&lt;P-&gt;fa[1]&lt;&lt;endl;<br>return 0;<br>} 
<img src ="http://www.cppblog.com/tuantuan/aggbug/91968.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tuantuan/" target="_blank">谢岱唛</a> 2009-08-02 17:17 <a href="http://www.cppblog.com/tuantuan/archive/2009/08/02/91968.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux操作系统线程同步：互斥量（mutex）</title><link>http://www.cppblog.com/tuantuan/archive/2009/08/02/91964.html</link><dc:creator>谢岱唛</dc:creator><author>谢岱唛</author><pubDate>Sun, 02 Aug 2009 08:57:00 GMT</pubDate><guid>http://www.cppblog.com/tuantuan/archive/2009/08/02/91964.html</guid><wfw:comment>http://www.cppblog.com/tuantuan/comments/91964.html</wfw:comment><comments>http://www.cppblog.com/tuantuan/archive/2009/08/02/91964.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tuantuan/comments/commentRss/91964.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tuantuan/services/trackbacks/91964.html</trackback:ping><description><![CDATA[互斥量从本质上说就是一把锁, 提供对共享资源的保护访问。 <br>　　1. 初始化: <br>　　在Linux下, 线程的互斥量数据类型是pthread_mutex_t. 在使用前, 要对它进行初始化: <br>　　对于静态分配的互斥量, 可以把它设置为PTHREAD_MUTEX_INITIALIZER, 或者调用pthread_mutex_init. <br>　　对于动态分配的互斥量, 在申请内存(malloc)之后, 通过pthread_mutex_init进行初始化, 并且在释放内存(free)前需要调用pthread_mutex_destroy. <br>　　原型: <br>　　int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restric attr); <br>　　int pthread_mutex_destroy(pthread_mutex_t *mutex); <br>　　头文件: <br>　　返回值: 成功则返回0, 出错则返回错误编号. <br>　　说明: 如果使用默认的属性初始化互斥量, 只需把attr设为NULL. 其他值在以后讲解。 <br>　　2. 互斥操作: <br highlighted="2">　　对共享资源的访问, 要对互斥量进行<zmkey class=zoomino-searchword offset="19" path="body > div:eq(0) > div:eq(3) > table:eq(2) > tbody:eq(0) > tr:eq(0) > td:eq(0) > div:eq(0) > #content:eq(0) > br:eq(11)" anchorType="previous" jQuery1249203190531="6">加锁<span class=zoominoBgImage></span></zmkey>, 如果互斥量已经上了锁, 调用线程会阻塞, 直到互斥量被<zmkey class=zoomino-searchword offset="29" path="body > div:eq(0) > div:eq(3) > table:eq(2) > tbody:eq(0) > tr:eq(0) > td:eq(0) > div:eq(0) > #content:eq(0) > br:eq(11)" anchorType="previous" jQuery1249203190531="7">解锁<span class=zoominoBgImage></span></zmkey>. 在完成了对共享资源的访问后, 要对互斥量进行解锁。 <br>　　首先说一下加锁函数: <br>　　头文件: <br>　　原型: <br>　　int pthread_mutex_lock(pthread_mutex_t *mutex); <br>　　int pthread_mutex_trylock(pthread_mutex_t *mutex); <br>　　返回值: 成功则返回0, 出错则返回错误编号. <br>　　说明: 具体说一下trylock函数, 这个函数是非阻塞调用模式, 也就是说, 如果互斥量没被锁住, trylock函数将把互斥量加锁, 并获得对共享资源的访问权限; 如果互斥量被锁住了, trylock函数将不会阻塞等待而直接返回EBUSY, 表示共享资源处于忙状态。 <br>　　再说一下解所函数: <br>　　头文件: <br>　　原型: int pthread_mutex_unlock(pthread_mutex_t *mutex); <br>　　返回值: 成功则返回0, 出错则返回错误编号. <br>　　3. 死锁: <br>　　死锁主要发生在有多个依赖锁存在时, 会在一个线程试图以与另一个线程相反顺序锁住互斥量时发生. 如何避免死锁是使用互斥量应该格外注意的东西。 <br>　　总体来讲, 有几个不成文的基本原则: <br>　　对共享资源操作前一定要获得锁。 <br>　　完成操作以后一定要释放锁。 <br>　　尽量短时间地占用锁。 <br>　　如果有多锁, 如获得顺序是ABC连环扣, 释放顺序也应该是ABC。 <br>　　线程错误返回时应该释放它所获得的锁。
<img src ="http://www.cppblog.com/tuantuan/aggbug/91964.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tuantuan/" target="_blank">谢岱唛</a> 2009-08-02 16:57 <a href="http://www.cppblog.com/tuantuan/archive/2009/08/02/91964.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++资源之不完全导引（完整版）</title><link>http://www.cppblog.com/tuantuan/archive/2008/06/23/54414.html</link><dc:creator>谢岱唛</dc:creator><author>谢岱唛</author><pubDate>Mon, 23 Jun 2008 15:16:00 GMT</pubDate><guid>http://www.cppblog.com/tuantuan/archive/2008/06/23/54414.html</guid><wfw:comment>http://www.cppblog.com/tuantuan/comments/54414.html</wfw:comment><comments>http://www.cppblog.com/tuantuan/archive/2008/06/23/54414.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tuantuan/comments/commentRss/54414.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tuantuan/services/trackbacks/54414.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 无数次听到“我要开始学习C++!”的呐喊，无数次听到“C++太复杂了，我真的<br>学不会”的无奈。Stan Lippman先生曾在《C++ Primer》一书中指出“C++是最为难<br>学的高级程序设计语言之一”，人们常将“之一”去掉以表达自己对C++的敬畏。诚<br>然，C++程序设计语言对于学习者的确有很多难以逾越的鸿沟，体系结构的庞大，应<br>接不暇并不断扩充的特性……除此之外，参考资料之多与冗杂使它的学习者望而却<br>步，欲求深入者苦不堪言。希望这一份不完全导引能够成为您C++学习之路上的引路<br>灯。<br>&nbsp;&nbsp;<a href='http://www.cppblog.com/tuantuan/archive/2008/06/23/54414.html'>阅读全文</a><img src ="http://www.cppblog.com/tuantuan/aggbug/54414.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tuantuan/" target="_blank">谢岱唛</a> 2008-06-23 23:16 <a href="http://www.cppblog.com/tuantuan/archive/2008/06/23/54414.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>