师父给出了另外一道题:
给一个数,然后开5个线程对它进行相减,直到这个数为0或小于0为止;
我用多线程实现如下:
// methods.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
int sum;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void * thread1(void *);
void * thread2(void *);
void * thread3(void *);
void * thread4(void *);
void * thread5(void *);
int main(int argc, char * argv[])
{
pthread_t tid1, tid2, tid3, tid4, tid5;
int rc1 = 0, rc2 = 0, rc3 = 0, rc4 = 0, rc5 = 0;
printf("enter main\n");
printf("Please input a number : \n");
scanf("%d", &sum);
while (sum >= 0)
{
rc1 = pthread_create(&tid1, NULL, thread1, NULL);
if (rc1 != 0)
printf("The thread1-create is failed!\n");
rc2 = pthread_create(&tid2, NULL, thread2, NULL);
if (rc2 != 0)
printf("The thread2-create is failed!\n");
rc3 = pthread_create(&tid3, NULL, thread3, NULL);
if (rc3 != 0)
printf("The thread3-create is failed!\n");
rc4 = pthread_create(&tid4, NULL, thread4, NULL);
if (rc4 != 0)
printf("The thread4-create is failed!\n");
rc5 = pthread_create(&tid5, NULL, thread5, NULL);
if (rc5 != 0)
printf("The thread5-create is failed!\n");
pthread_cond_wait(&cond, &mutex);
}
printf("leave main\n");
exit(0);
}
void * thread1(void * arg)
{
printf("enter thread1\n");
pthread_mutex_lock(&mutex);
if (sum <= 0)
exit(0);
else
printf("this is thread1, sum : %d, thread id is %u\n", sum,
(unsigned int)pthread_self());
pthread_cond_signal(&cond);
sum -= 1;
printf("this is thread1, sum : %d, thread id is %u\n", sum,
(unsigned int)pthread_self());
pthread_mutex_unlock(&mutex);
printf("leave thread1\n");
pthread_exit(0);
}
void * thread2(void * arg)
{
printf("enter thread2\n");
pthread_mutex_lock(&mutex);
if (sum <= 0)
exit(0);
else
printf("this is thread2, sum : %d, thread id is %u\n", sum,
(unsigned int)pthread_self());
pthread_cond_signal(&cond);
sum -= 2;
printf("this is thread2, sum : %d, thread id is %u\n", sum,
(unsigned int)pthread_self());
pthread_mutex_unlock(&mutex);
printf("leave thread2\n");
pthread_exit(0);
}
void * thread3(void * arg)
{
printf("enter thread3\n");
pthread_mutex_lock(&mutex);
if (sum <= 0)
exit(0);
else
printf("this is thread3, sum : %d, thread id is %u\n", sum,
(unsigned int)pthread_self());
pthread_cond_signal(&cond);
sum -= 3;
printf("this is thread3, sum : %d, thread id is %u\n", sum,
(unsigned int)pthread_self());
pthread_mutex_unlock(&mutex);
printf("leave thread3\n");
pthread_exit(0);
}
void * thread4(void * arg)
{
printf("enter thread4\n");
pthread_mutex_lock(&mutex);
if (sum <= 0)
exit(0);
else
printf("this is thread4, sum : %d, thread id is %u\n", sum,
(unsigned int)pthread_self());
pthread_cond_signal(&cond);
sum -= 4;
printf("this is thread4, sum : %d, thread id is %u\n", sum,
(unsigned int)pthread_self());
pthread_mutex_unlock(&mutex);
printf("leave thread4\n");
pthread_exit(0);
}
void * thread5(void * arg)
{
printf("enter thread5\n");
pthread_mutex_lock(&mutex);
if (sum <= 0)
exit(0);
else
printf("this is thread5, sum : %d, thread id is %u\n", sum,
(unsigned int)pthread_self());
pthread_cond_signal(&cond);
sum -= 5;
printf("this is thread5, sum : %d, thread id is %u\n", sum,
(unsigned int)pthread_self());
pthread_mutex_unlock(&mutex);
printf("leave thread5\n");
pthread_exit(0);
}
以下是运行结果,只截取了部分,因为结果过长:
- 大小: 402 KB
分享到:
相关推荐
鱼刺类_多线程应用 - 更新日志 5.4.3(2017-12-01) *修正 修正鱼刺类_线程池Ex/线程池Ex一处很难被触发的BUG *修改 去除所有用到取启动时间的函数/方法 (修正在系统开机超过29天的机器上可能会出现问题) *修正 类回调...
鱼刺类_多线程应用 - 更新日志 5.4.3(2017-12-01) *修正 修正鱼刺类_线程池Ex/线程池Ex一处很难被触发的BUG *修改 去除所有用到取启动时间的函数/方法 (修正在系统开机超过29天的机器上可能会出现问题) *修正 类回调...
245 12.3.3 在外部类外访问静态内部类 246 12.4 匿名内部类 247 12.4.1 创建匿名内部类 247 12.4.2 匿名内部类的初始化 249 12.5 综合练习 250 12.6 小结 250 12.7 习题 250 第13章 多线程(精彩视频:55分钟) 252 ...
实例119 在指定的目录下创建多个临时文件 158 实例120 删除指定目录下的文件 160 实例121 移动指定目录下的文件 163 实例122 文件搜索引挚 167 7.2 字节流 169 实例123 复制指定目录下的文件 170 ...
实例119 在指定的目录下创建多个临时文件 158 实例120 删除指定目录下的文件 160 实例121 移动指定目录下的文件 163 实例122 文件搜索引挚 167 7.2 字节流 169 实例123 复制指定目录下的文件 170 实例124 显示文件中...
实例119 在指定的目录下创建多个临时文件 158 实例120 删除指定目录下的文件 160 实例121 移动指定目录下的文件 163 实例122 文件搜索引挚 167 7.2 字节流 169 实例123 复制指定目录下的文件 ...
实例119 在指定的目录下创建多个临时文件 158 实例120 删除指定目录下的文件 160 实例121 移动指定目录下的文件 163 实例122 文件搜索引挚 167 7.2 字节流 169 实例123 复制指定目录下的文件 170 实例124 显示文件中...
其中一些非线程安全操作是加法和减法。 该分配是一个MPMC(多生产者多消费者)问题,我们使用以下类: 市场-同步生产者和消费者线程,注册生产者,为客户创建购物车; 产品; 生产者-发布产品并等待; 消费者-在...
实例119 在指定的目录下创建多个临时文件 158 实例120 删除指定目录下的文件 160 实例121 移动指定目录下的文件 163 实例122 文件搜索引挚 167 7.2 字节流 169 实例123 复制指定目录下的文件 170 实例124 ...
上机实践8 多线程 41 实验1 汉字打字练习 41 实验2 旋转的行星 43 实验3 双线程接力 47 上机实践9 输入输出流 50 实验1 学读汉字 50 实验2 统计英文单词字 53 实验2 读取Zip文件 56 上机实践10 Java 中的网络编程 57...