师父给出了另外一道题:
给一个数,然后开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);
}
以下是运行结果,只截取了部分,因为结果过长:
