C笔记 用C语言实现一个线程池


为什么使用线程池

        多线程通信优点之一便是cpu利用率高,响应速度快。对于单个服务器处理多个客户端任务时难免会对线程进行频繁的创建和销毁,这时引入池化技术便可以很好的解决这个问题,可以节省线程创建销毁时间开销的同时提高了线程复用性,减少线程间来回切换,从而减少cpu资源浪费。

线程池结构

         以cs架构模型为例,用任务队列存储客户端链接任务,线程池从任务队列中拿取任务。

任务队列

struct job                            //创建任务类型  
{
	void * (*func)(void *arg);        //函数指针,指向要执行的函数
	void *arg;				          //函数的参数
	struct job *next;				  //指向下一个任务的指针
};

线程池

struct threadpool              //创建线程池
{   
	int thread_num;            //线程数量
	pthread_t *pthread_ids;    //线程id

	struct job *head;          //任务队列头
	struct job *tail;          //任务队列尾
	int queue_max_num;         //任务 队列最大容量                                           
	int queue_cur_num;         //队列已有任务数量

	pthread_mutex_t mutex;          //线程池访问队列资源锁
	pthread_cond_t queue_full;      //队列满
	pthread_cond_t queue_not_empty; //队列不为空
	pthread_cond_t queue_empty;     //队列空

};

        线程池中除了保存线程信息和指向任务队列头尾的指针之外还加入了互斥锁和若干条件变量。

        mutex是一个互斥锁,可以保护任务队列资源,防止线程池中的多个线程同时访问任务队列导致的内容错乱,在一个线程访问时加锁不让其他线程访问。

        条件变量queue_empty的作用是在队列中没有任务时阻塞线线程。

        条件变量queue_not_empty在任务队列中被添加了任务时唤醒线程处理任务。

  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,作为AI语言模型,我无法为您提供完整的C语言编程代码。但我可以给您一些编程思路和实现步骤: 1. 定义结构体 ``` struct note { char title[50]; char content[500]; char date[20]; }; ``` 2. 编写函数 - 添加笔记 ``` void add_note(struct note *notes, int *count) { printf("请输入笔记标题:"); scanf("%s", notes[*count].title); printf("请输入笔记内容:"); scanf("%s", notes[*count].content); printf("请输入笔记日期:"); scanf("%s", notes[*count].date); (*count)++; } ``` - 查看笔记 ``` void view_notes(struct note *notes, int count) { for (int i = 0; i < count; i++) { printf("标题:%s\n", notes[i].title); printf("内容:%s\n", notes[i].content); printf("日期:%s\n", notes[i].date); } } ``` - 编辑笔记 ``` void edit_note(struct note *notes, int count) { char title[50]; printf("请输入要编辑的笔记标题:"); scanf("%s", title); for (int i = 0; i < count; i++) { if (strcmp(notes[i].title, title) == 0) { printf("请输入笔记内容:"); scanf("%s", notes[i].content); printf("请输入笔记日期:"); scanf("%s", notes[i].date); printf("笔记编辑成功!\n"); return; } } printf("未找到该笔记!\n"); } ``` - 删除笔记 ``` void delete_note(struct note *notes, int *count) { char title[50]; printf("请输入要删除的笔记标题:"); scanf("%s", title); for (int i = 0; i < *count; i++) { if (strcmp(notes[i].title, title) == 0) { for (int j = i; j < *count - 1; j++) { notes[j] = notes[j+1]; } (*count)--; printf("笔记删除成功!\n"); return; } } printf("未找到该笔记!\n"); } ``` 3. 主函数 ``` int main() { struct note notes[100]; int count = 0; int choice; while (1) { printf("请选择操作:\n"); printf("1. 添加笔记\n"); printf("2. 查看笔记\n"); printf("3. 编辑笔记\n"); printf("4. 删除笔记\n"); printf("5. 退出\n"); scanf("%d", &choice); switch (choice) { case 1: add_note(notes, &count); break; case 2: view_notes(notes, count); break; case 3: edit_note(notes, count); break; case 4: delete_note(notes, &count); break; case 5: exit(0); default: printf("输入有误,请重新输入!\n"); } } return 0; } ``` 以上是一个简单的笔记管理系统的实现,您可以根据需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值