• <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, 評論 - 26, 引用 - 0
            數(shù)據(jù)加載中……

            隊列

            隊列

            需要隊列是出于兩個原因。首先,需要隊列來保存工作作業(yè)。還需要可用于跟蹤已終止線程的數(shù)據(jù)結(jié)構(gòu)。還記得前幾篇文章(請參閱本文結(jié)尾處的 參考資料)中,我曾提到過需要使用帶有特定進(jìn)程標(biāo)識的 pthread_join 嗎?使用“清除隊列”(稱作 "cq")可以解決無法等待 任何已終止線程的問題(稍后將詳細(xì)討論這個問題)。以下是標(biāo)準(zhǔn)隊列代碼。將此代碼保存到文件 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 井泉 閱讀(218) 評論(0)  編輯 收藏 引用 所屬分類: 數(shù)據(jù)結(jié)構(gòu)

            国产精品狼人久久久久影院 | 久久久精品无码专区不卡| 久久久久人妻精品一区| 熟妇人妻久久中文字幕| 久久线看观看精品香蕉国产| 久久国产精品无码网站| 亚洲va久久久噜噜噜久久天堂| 亚洲精品美女久久久久99| 国产精品免费久久| 久久精品国产第一区二区三区| 国产激情久久久久影院老熟女免费 | 四虎久久影院| 久久国产精品99精品国产987| 国内精品久久久久久久涩爱| 亚洲中文久久精品无码ww16| 精品久久久无码中文字幕天天| 伊人久久大香线蕉综合影院首页| 99久久免费只有精品国产| 一本色道久久88精品综合| 国内精品免费久久影院| 国内精品伊人久久久久av一坑| 中文精品99久久国产 | 狠狠色丁香婷婷久久综合| 久久久久综合网久久| 一本一本久久aa综合精品| 久久久中文字幕日本| 99久久精品无码一区二区毛片| 久久国产乱子伦免费精品| 亚洲综合久久夜AV | 久久久久成人精品无码| 亚洲精品高清国产一久久| 国产亚洲色婷婷久久99精品| 色综合久久久久久久久五月| 久久精品无码一区二区WWW| 亚洲精品综合久久| 伊人久久大香线蕉综合5g| 久久国产免费直播| 思思久久99热只有频精品66| 午夜肉伦伦影院久久精品免费看国产一区二区三区| 青青青青久久精品国产 | 久久精品免费大片国产大片|