Redis的事件驅動分析

測試如何標記代碼測試

<p>fdsfsa </p>
<div class="cnblogs_code">
  <pre><span style="color: #008080;"> 1</span> #include <span style="color: #800000;">"</span><span style="color: #800000;">lock_stack.h</span><span style="color: #800000;">"</span><br /><span style="color: #008080;"> 2</span> #include &lt;pthread.h&gt;<br /><span style="color: #008080;"> 3</span> #include &lt;stdlib.h&gt;<br /><span style="color: #008080;"> 4</span> <br /><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">struct</span> stack *stack_alloc(<span style="color: #0000ff;">void</span>)<br /><span style="color: #008080;"> 6</span> {<br /><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">struct</span> stack *fp;<br /><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">if</span>((fp = (<span style="color: #0000ff;">struct</span> stack*)malloc(<span style="color: #0000ff;">sizeof</span>(<span style="color: #0000ff;">struct</span> stack))) != NULL)<br /><span style="color: #008080;"> 9</span> {<br /><span style="color: #008080;">10</span> fp-&gt;top = NULL;<br /><span style="color: #008080;">11</span> <span style="color: #0000ff;">if</span>(pthread_mutex_init(&amp;fp-&gt;<span style="color: #0000ff;">lock</span>,NULL) != <span style="color: #800080;">0</span>)<br /><span style="color: #008080;">12</span> {<br /><span style="color: #008080;">13</span> free(fp);<br /><span style="color: #008080;">14</span> <span style="color: #0000ff;">return</span> NULL;<br /><span style="color: #008080;">15</span> }<br /><span style="color: #008080;">16</span> }<br /><span style="color: #008080;">17</span> <span style="color: #0000ff;">return</span> fp;<br /><span style="color: #008080;">18</span> }<br /><span style="color: #008080;">19</span> <br /><span style="color: #008080;">20</span> <span style="color: #0000ff;">void</span> stack_push(<span style="color: #0000ff;">struct</span> stack *fp,<span style="color: #0000ff;">struct</span> cell *cl)<br /><span style="color: #008080;">21</span> {<br /><span style="color: #008080;">22</span> pthread_mutex_lock(&amp;fp-&gt;<span style="color: #0000ff;">lock</span>); <br /><span style="color: #008080;">23</span> <br /><span style="color: #008080;">24</span> cl-&gt;next = fp-&gt;top;<br /><span style="color: #008080;">25</span> fp-&gt;top = cl; <br /><span style="color: #008080;">26</span> <br /><span style="color: #008080;">27</span> pthread_mutex_unlock(&amp;fp-&gt;<span style="color: #0000ff;">lock</span>); <br /><span style="color: #008080;">28</span> }<br /><span style="color: #008080;">29</span> <br /><span style="color: #008080;">30</span> <span style="color: #0000ff;">struct</span> cell *stack_pop(<span style="color: #0000ff;">struct</span> stack *fp)<br /><span style="color: #008080;">31</span> {<br /><span style="color: #008080;">32</span> cell *head;<br /><span style="color: #008080;">33</span> pthread_mutex_lock(&amp;fp-&gt;<span style="color: #0000ff;">lock</span>); <br /><span style="color: #008080;">34</span> <br /><span style="color: #008080;">35</span> head = fp-&gt;top;<br /><span style="color: #008080;">36</span> <span style="color: #0000ff;">if</span>(head == NULL)<br /><span style="color: #008080;">37</span> {<br /><span style="color: #008080;">38</span> pthread_mutex_unlock(&amp;fp-&gt;<span style="color: #0000ff;">lock</span>); <br /><span style="color: #008080;">39</span> <span style="color: #0000ff;">return</span> head;<br /><span style="color: #008080;">40</span> }<br /><span style="color: #008080;">41</span> <br /><span style="color: #008080;">42</span> fp-&gt;top = head-&gt;next;<br /><span style="color: #008080;">43</span> pthread_mutex_unlock(&amp;fp-&gt;<span style="color: #0000ff;">lock</span>); <br /><span style="color: #008080;">44</span> }</pre>
  </div>
相關文章
相關標籤/搜索