• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 74, 文章 - 0, 評(píng)論 - 26, 引用 - 0
            數(shù)據(jù)加載中……

            隊(duì)列

            隊(duì)列

            需要隊(duì)列是出于兩個(gè)原因。首先,需要隊(duì)列來保存工作作業(yè)。還需要可用于跟蹤已終止線程的數(shù)據(jù)結(jié)構(gòu)。還記得前幾篇文章(請(qǐng)參閱本文結(jié)尾處的 參考資料)中,我曾提到過需要使用帶有特定進(jìn)程標(biāo)識(shí)的 pthread_join 嗎?使用“清除隊(duì)列”(稱作 "cq")可以解決無法等待 任何已終止線程的問題(稍后將詳細(xì)討論這個(gè)問題)。以下是標(biāo)準(zhǔn)隊(duì)列代碼。將此代碼保存到文件 queue.h 和 queue.c:


            queue.h
            												
            														/* queue.h
            ** Copyright 2000 Daniel Robbins, Gentoo Technologies, Inc.
            ** Author: Daniel Robbins
            ** Date: 16 Jun 2000
            */
            
            typedef struct node {
              struct node *next;
            } node;
            
            typedef struct queue {
              node *head, *tail; 
            } queue;
            
            void queue_init(queue *myroot);
            void queue_put(queue *myroot, node *mynode);
            node *queue_get(queue *myroot);
            
            												
            										



            queue.c
            												
            														/* queue.c
            ** Copyright 2000 Daniel Robbins, Gentoo Technologies, Inc.
            ** Author: Daniel Robbins
            ** Date: 16 Jun 2000
            **
            ** This set of queue functions was originally thread-aware.  I
            ** redesigned the code to make this set of queue routines
            ** thread-ignorant (just a generic, boring yet very fast set of queue
            ** routines).  Why the change?  Because it makes more sense to have
            ** the thread support as an optional add-on.  Consider a situation
            ** where you want to add 5 nodes to the queue.  With the
            ** thread-enabled version, each call to queue_put() would
            ** automatically lock and unlock the queue mutex 5 times -- that's a
            ** lot of unnecessary overhead.  However, by moving the thread stuff
            ** out of the queue routines, the caller can lock the mutex once at
            ** the beginning, then insert 5 items, and then unlock at the end.
            ** Moving the lock/unlock code out of the queue functions allows for
            ** optimizations that aren't possible otherwise.  It also makes this
            ** code useful for non-threaded applications.
            **
            ** We can easily thread-enable this data structure by using the
            ** data_control type defined in control.c and control.h.  */
            
            #include <stdio.h>
            #include "queue.h"
            
            void queue_init(queue *myroot) {
              myroot->head=NULL;
              myroot->tail=NULL;
            }
            
            void queue_put(queue *myroot,node *mynode) {
              mynode->next=NULL;
              if (myroot->tail!=NULL)
                myroot->tail->next=mynode;
              myroot->tail=mynode;
              if (myroot->:head==NULL)
                myroot->head=mynode;
            }
            
            node *queue_get(queue *myroot) {
              //get from root
              node *mynode;
              mynode=myroot->head;
              if (myroot->head!=NULL)
                myroot->head=myroot->head->next;
              return mynode;
            }
            
            												
            										

            posted on 2006-06-26 19:25 井泉 閱讀(224) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 數(shù)據(jù)結(jié)構(gòu)

            久久久久成人精品无码中文字幕| 国产精品内射久久久久欢欢| 色综合久久天天综线观看| 无码国内精品久久人妻麻豆按摩| 伊人色综合久久天天人守人婷| 性欧美大战久久久久久久久| 国产精品99久久久久久www| 丁香色欲久久久久久综合网| 一本一道久久精品综合 | 97热久久免费频精品99| 国产精品无码久久综合网| 亚洲午夜久久久影院伊人| 久久av免费天堂小草播放| 日本欧美久久久久免费播放网| 久久WWW免费人成—看片| 97久久精品人妻人人搡人人玩| 久久精品一本到99热免费| 久久99精品久久久久久不卡 | 国产成人精品久久亚洲高清不卡| 18禁黄久久久AAA片| 国产视频久久| 午夜不卡888久久| 97久久国产亚洲精品超碰热| 亚洲国产精品18久久久久久| 香蕉久久AⅤ一区二区三区| 久久亚洲精品中文字幕三区| 91精品国产综合久久婷婷| 色妞色综合久久夜夜| 99久久夜色精品国产网站| 国产精品99久久久久久宅男小说| 久久国产视屏| 国产精品永久久久久久久久久 | 免费精品久久天干天干| 欧美精品丝袜久久久中文字幕| 99国内精品久久久久久久| Xx性欧美肥妇精品久久久久久| 99久久www免费人成精品| 国产免费久久久久久无码| 国产真实乱对白精彩久久| 亚洲欧美久久久久9999| 久久精品日日躁夜夜躁欧美|