• <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
            數據加載中……

            隊列

            隊列

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

            久久久久久精品免费免费自慰| 久久久久九九精品影院| 久久香蕉国产线看观看精品yw| 蜜臀久久99精品久久久久久小说| 久久精品aⅴ无码中文字字幕重口| 国产精品无码久久久久久| 久久亚洲国产成人影院网站| 亚洲中文字幕久久精品无码喷水| 久久99国产精品一区二区| 亚洲精品tv久久久久| 久久久久久免费一区二区三区| 久久久久免费精品国产| 精品久久久无码中文字幕| 久久午夜无码鲁丝片| 国产精品久久久久免费a∨| 久久精品免费观看| 欧美牲交A欧牲交aⅴ久久| 亚洲午夜久久久| 久久免费99精品国产自在现线 | 久久96国产精品久久久| 伊人久久大香线蕉av不卡| 亚洲第一永久AV网站久久精品男人的天堂AV | 97久久国产露脸精品国产| 日韩十八禁一区二区久久| 国产精品成人精品久久久| 久久99久久99精品免视看动漫| 久久精品国产99国产精品亚洲| 久久一区二区三区免费| 久久精品无码一区二区三区免费| 国产精品久久久天天影视| 久久亚洲美女精品国产精品| 狠狠色丁香久久婷婷综合| 一本色道久久99一综合| 一本色道久久综合亚洲精品| 久久久久免费精品国产| 一本色道久久综合亚洲精品| 一本久久a久久精品vr综合| 久久夜色精品国产网站| 国产精品久久久久影院嫩草| 国产成人久久精品麻豆一区| 久久精品国产色蜜蜜麻豆|