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

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:

深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值