一个多线程实例
2011-04-24 17:22
288 查看
queue.h
queue.c
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; } |
/* 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); control.h
|
相关文章推荐
- 工作中为什么要用多线程,它的好处,以及一个简单多线程实例。
- JAVA多线程的一个实例
- 一个多线程web服务器实例(C,Linux,详细的web服务器原理)
- 一个java多线程实例
- 用Python写的一个多线程TCP通信实例
- GDB 多线程调试基本命令和一个实例问题的解决(转贴)
- 一个多线程web服务器实例(C,Linux,详细的web服务器原理) 转
- 多线程程序设计------一个实例(SDES破解)
- 一个基于MFC多线程和Opencv库的打开两个摄像头实例
- 用一个实例介绍Java多线程
- JAVA多线程的一个实例
- C#一个简单多线程的实例
- 一个关于多线程的简单实例(抄书)
- c++多线程,一个很清晰的包含类定义的多线程实例
- mfc一个多线程实例
- java多线程很好的一个实例
- 一个多线程web服务器实例(C,Linux,详细的web服务器原理)
- Win32 API 多线程编程——一个简单实例(含消息参数传递)
- WCF 第五章 一个单一实例中的多线程
- 一个经典的linux多线程实例