易截截图软件、单文件、免安装、纯绿色、仅160KB

Linux C 实现线程池

最近做的一些工作需要用到线程池技术,因此参考了一些资料和书籍,如《0bug c/c++商用工程之道》。
为此在linux平台上用纯c写了一个线程池的实现。
在此列出了原代码。
主要用到的数据结构有
1.struct  thread_pool_t    // thread pool 的实现代码
2.struct thread_pool_token_t    //在thread pool 中用到的结构,一个该类型变量代表一条线程
3.struct thread_pool_job_t     //当线前程处理该任务将回调结构
设计思路:
主线程创建线程池的控制线程,
管理线程将进入循环来做以下事情:
1.如果当前空闲进程不足并且总线程数没有达到上限,将创建一条空闲线程,至道空闲线程达到3条(见THREAD_POOL_MIN_IDLE_SIZE)
2.不断检查工作任务队列,有则安排一条空闲线程对该任务进行处理
普通线程被管理线程创建后也将进入循环来做以下事情:
1.检查当前assign的thread_pool_token_t结构的状态,如果是TREAD_POOL_STATE_BUSY则调用thread_pool_token_t所注册的回调函数来处理任务。
2.完成该任务后检测当前空闲进程够不够,如果太多则退出本线程,否则会改写运行状态为TREAD_POOL_STATE_IDLE后继续循环
注册任务:
1.当没有空闲线程可用时将把该任务加入工作任务队列(需定义_THREAD_POOL_JOB_LIST_),以备将来有空闲线程是能够进行处理。
主要文件 :
1.thread_pool.h
 
#ifndef _LIB_DOL_THREAD_POOL_H_
#define _LIB_DOL_THREAD_POOL_H_
#include "thread_pool.def"
#include <pthread.h>
#define THREAD_POOL_MIN_SIZE      5     /* at least 5 threads, 4 job + 1 manage thread */
#define THREAD_POOL_MIN_IDLE_SIZE 3     /* at least 3 threads idle to improve the efficiency */
#define THREAD_POOL_DEFAULT_SIZE  10    /* default threads in thread pool */
#define THREAD_POOL_MAX_SIZE      100   /* max threads in thread pool */
#define TREAD_POOL_STATE_NOTRUN   0
#define TREAD_POOL_STATE_IDLE     1
#define TREAD_POOL_STATE_BUSY     2
#define TREAD_POOL_REG_OK         0
#define


相关文档:

关于LINUX中的FORK函数

一直想写点LINUX中的FORK函数,但是吧,我实在是太懒了,再加上文采不怎么好,所以就从网上找了篇写的不错的文章,看完之后应该对FORK函数有一定的了解~~
 
给出如下C程序,在linux下使用gcc编译:
1 #include "stdio.h"
2 #include "sys/types.h"
3 #include "unistd.h"

5  int  main()
6 ......

Linux操作系统内核启动参数详细解析

Linux内核在启动的时候,能接收某些命令行选项或启动时参数。当内核不能识别某些硬件进而不能设置硬件参数或者为了避免内核更改某些参数的值,可以通过这种方式手动将这些参数传递给内核。
如果不使用启动管理器,比如直接从BIOS或者把内核文件用“cp zImage /dev/fd0”等方法直接从设备启动,就不能给内核传递 ......

Linux Shell 笔记二(循环结构)

程序12:类似java 里面的switch case
[root@localhost scripts]# cat sh12.sh
read -p "input comand:" command
case $command in
"fix")
echo "fix system"
;;
"fuck")
echo "fuck you"
;;
*)
echo "what a stupid man!ex>$0 some word"
......

linux mmap驱动实现

在实现驱动程序的mmap函数时,要注意映射地址的转换问题,见代码。
定义一个设备结构体:
struct leedriver
{
struct cdev cdev;
unsigned char mem[MEMSIZE];
};
这里面这个MEMSIZE,最小都要是4096,因为内存映射是以页为单位的。
在实现simple_remap_mmap函数时,代码如下
static int simple_remap_mmap(stru ......

Can C beat RTL?


http://www.edn.com/article/457428-Can_C_beat_RTL_.php 
With the appearance of higher speeds and more DSP macrocells in low-cost FPGAs, more and more design teams are seeing the configurable chips not as glue but as a way to accelerate the inner loops of numerical algorithms, either in conjun ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号