c语言多线程实现三人过河,C语言实现简单线程池(转-Newerth)

本文介绍了一个简单的线程池实现方法,包括线程池的创建、任务分配、任务执行及销毁过程。通过示例代码详细展示了如何利用线程池提高并发任务处理效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1: #include

2: #include

3: #include

4: #include

5: #include

6:

7: #include "tpool.h"

8:

9: static tpool_t *tpool = NULL;

10:

11: /* 工作者线程函数, 从任务链表中取出任务并执行 */

12: static void*

13: thread_routine(void *arg)

14: {

15: tpool_work_t *work;

16:

17: while(1) {

18: /* 如果线程池没有被销毁且没有任务要执行,则等待 */

19: pthread_mutex_lock(&tpool->queue_lock);

20: while(!tpool->queue_head && !tpool->shutdown) {

21: pthread_cond_wait(&tpool->queue_ready, &tpool->queue_lock);

22: }

23: if (tpool->shutdown) {

24: pthread_mutex_unlock(&tpool->queue_lock);

25: pthread_exit(NULL);

26: }

27: work = tpool->queue_head;

28: tpool->queue_head = tpool->queue_head->next;

29: pthread_mutex_unlock(&tpool->queue_lock);

30:

31: work->routine(work->arg);

32: free(work);

33: }

34:

35: return NULL;

36: }

37:

38: /*

39: * 创建线程池

40: */

41: int

42: tpool_create(int max_thr_num)

43: {

44: int i;

45:

46: tpool = calloc(1, sizeof(tpool_t));

47: if (!tpool) {

48: printf("%s: calloc failed\n", __FUNCTION__);

49: exit(1);

50: }

51:

52: /* 初始化 */

53: tpool->max_thr_num = max_thr_num;

54: tpool->shutdown = 0;

55: tpool->queue_head = NULL;

56: if (pthread_mutex_init(&tpool->queue_lock, NULL) !=0) {

57: printf("%s: pthread_mutex_init failed, errno:%d, error:%s\n",

58: __FUNCTION__, errno, strerror(errno));

59: exit(1);

60: }

61: if (pthread_cond_init(&tpool->queue_ready, NULL) !=0 ) {

62: printf("%s: pthread_cond_init failed, errno:%d, error:%s\n",

63: __FUNCTION__, errno, strerror(errno));

64: exit(1);

65: }

66:

67: /* 创建工作者线程 */

68: tpool->thr_id = calloc(max_thr_num, sizeof(pthread_t));

69: if (!tpool->thr_id) {

70: printf("%s: calloc failed\n", __FUNCTION__);

71: exit(1);

72: }

73: for (i = 0; i < max_thr_num; ++i) {

74: if (pthread_create(&tpool->thr_id[i], NULL, thread_routine, NULL) != 0){

75: printf("%s:pthread_create failed, errno:%d, error:%s\n", __FUNCTION__,

76: errno, strerror(errno));

77: exit(1);

78: }

79:

80: }

81:

82: return 0;

83: }

84:

85: /* 销毁线程池 */

86: void

87: tpool_destroy()

88: {

89: int i;

90: tpool_work_t *member;

91:

92: if (tpool->shutdown) {

93: return;

94: }

95: tpool->shutdown = 1;

96:

97: /* 通知所有正在等待的线程 */

98: pthread_mutex_lock(&tpool->queue_lock);

99: pthread_cond_broadcast(&tpool->queue_ready);

100: pthread_mutex_unlock(&tpool->queue_lock);

101: for (i = 0; i < tpool->max_thr_num; ++i) {

102: pthread_join(tpool->thr_id[i], NULL);

103: }

104: free(tpool->thr_id);

105:

106: while(tpool->queue_head) {

107: member = tpool->queue_head;

108: tpool->queue_head = tpool->queue_head->next;

109: free(member);

110: }

111:

112: pthread_mutex_destroy(&tpool->queue_lock);

113: pthread_cond_destroy(&tpool->queue_ready);

114:

115: free(tpool);

116: }

117:

118: /* 向线程池添加任务 */

119: int

120: tpool_add_work(void*(*routine)(void*), void *arg)

121: {

122: tpool_work_t *work, *member;

123:

124: if (!routine){

125: printf("%s:Invalid argument\n", __FUNCTION__);

126: return -1;

127: }

128:

129: work = malloc(sizeof(tpool_work_t));

130: if (!work) {

131: printf("%s:malloc failed\n", __FUNCTION__);

132: return -1;

133: }

134: work->routine = routine;

135: work->arg = arg;

136: work->next = NULL;

137:

138: pthread_mutex_lock(&tpool->queue_lock);

139: member = tpool->queue_head;

140: if (!member) {

141: tpool->queue_head = work;

142: } else {

143: while(member->next) {

144: member = member->next;

145: }

146: member->next = work;

147: }

148: /* 通知工作者线程,有新任务添加 */

149: pthread_cond_signal(&tpool->queue_ready);

150: pthread_mutex_unlock(&tpool->queue_lock);

151:

152: return 0;

153: }

154:

155:

内容概要:本文档主要介绍了Intel Edge Peak (EP) 解决方案,涵盖从零到边缘高峰的软件配置和服务管理。EP解决方案旨在简化客户的入门门槛,提供一系列工具和服务,包括Edge Software Provisioner (ESP),用于构建和缓存操作系统镜像和软件栈;Device Management System (DMS),用于远程集群或本地集群管理;以及Autonomous Clustering for the Edge (ACE),用于自动化边缘集群的创建和管理。文档详细描述了从软件发布、设备制造、运输、安装到最终设备激活的全过程,并强调了在不同应用场景(如公共设施、工业厂房、海上油井和移动医院)下的具体部署步骤和技术细节。此外,文档还探讨了安全设备注册(FDO)、集群管理、密钥轮换和备份等关键操作。 适合群:具备一定IT基础设施和边缘计算基础知识的技术员,特别是负责边缘设备部署和管理的系统集成商和运维员。 使用场景及目标:①帮助系统集成商和客户简化边缘设备的初始配置和后续管理;②确保设备在不同网络环境下的安全启动和注册;③支持大规模边缘设备的自动化集群管理和应用程序编排;④提供详细的密钥管理和集群维护指南,确保系统的长期稳定运行。 其他说明:本文档是详细描述了Edge Peak技术及其应用案例。文档不仅提供了技术实现的指导,还涵盖了策略配置、安全性和扩展性的考虑,帮助用户全面理解和实施Intel的边缘计算解决方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值