• <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国产精品| 久久嫩草影院免费看夜色| 国产精品gz久久久| 久久综合精品国产一区二区三区 | 大蕉久久伊人中文字幕| A级毛片无码久久精品免费| 一级a性色生活片久久无| 欧洲成人午夜精品无码区久久| 国产精品久久久久国产A级| 少妇久久久久久被弄到高潮| 精品无码久久久久国产| 久久精品无码免费不卡| 久久精品国产亚洲AV麻豆网站| 亚洲性久久久影院| 亚洲午夜精品久久久久久人妖| 亚洲AV日韩AV永久无码久久 | 人妻无码精品久久亚瑟影视| AAA级久久久精品无码片| 欧美亚洲另类久久综合婷婷| 精品久久久久久亚洲精品| 久久亚洲国产成人影院| 久久av免费天堂小草播放| 99久久人妻无码精品系列蜜桃| 亚洲国产日韩欧美久久| 久久久精品人妻无码专区不卡| 99久久久精品| 久久国产精品无码一区二区三区| 亚洲精品午夜国产va久久| 精品国产综合区久久久久久| 国产精品久久久天天影视| 久久99国产精品尤物| 色狠狠久久AV五月综合| 97久久婷婷五月综合色d啪蜜芽 | 久久99国内精品自在现线| 香蕉久久av一区二区三区| 欧洲精品久久久av无码电影| 国产成人精品久久| 久久亚洲精精品中文字幕| 99久久久精品| 久久WWW免费人成—看片|