• <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 井泉 閱讀(210) 評論(0)  編輯 收藏 引用 所屬分類: 數據結構

            亚洲中文精品久久久久久不卡| 国内精品久久久久影院一蜜桃| 国产精品狼人久久久久影院| 国产一区二区精品久久岳 | 久久毛片一区二区| 久久九九久精品国产免费直播| 久久久久亚洲Av无码专| 久久99久久99小草精品免视看| 久久精品一区二区三区不卡| 久久亚洲国产成人影院网站| 久久精品国产清自在天天线| 久久综合狠狠色综合伊人| 欧美粉嫩小泬久久久久久久 | 亚洲级αV无码毛片久久精品| 久久久久久久亚洲Av无码| 久久久这里有精品中文字幕| 久久影院综合精品| 日本久久久久久久久久| 99久久成人国产精品免费| 久久久久国产视频电影| 人妻无码中文久久久久专区| 热RE99久久精品国产66热| av午夜福利一片免费看久久| 噜噜噜色噜噜噜久久| 精品国产综合区久久久久久| 99久久国产综合精品女同图片| 久久精品?ⅴ无码中文字幕| 久久国产精品一国产精品金尊| 模特私拍国产精品久久| 久久久久亚洲AV无码专区桃色| 久久丫精品国产亚洲av| 伊人久久无码中文字幕| 久久无码AV中文出轨人妻| 国产精品青草久久久久福利99| 国产精品久久网| 狠狠色婷婷综合天天久久丁香 | 亚洲成av人片不卡无码久久| 99久久精品国产一区二区三区| 久久亚洲AV成人无码国产| 亚洲AV成人无码久久精品老人 | 久久久久久夜精品精品免费啦|