#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>//Linux/Unix系统中内置头文件,包含许多系统函数
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
int num = 200, count = 30;
void* sub1(void* arg)
{
for (int i = 0; i < count; i++)
{
printf("线程1 num-1前值为:%d,", num);
int tmp = num - 1;
printf("线程1 num-1后tmp值为:%d,", tmp);
usleep(13);
num = tmp;
printf("线程1 num-1后的值为:%d\n", num);
}
return ((void*)0);
}
void *sub2(void *arg)
{
int i = 0, tmp;
for (; i < count; i++)
{
printf("线程2 num-1前值为:%d,", num);
tmp = num - 1;
printf("线程2 num-1后tmp值为:%d,", tmp);
usleep(31);
num = tmp;
printf("线程2 num-1后的值为:%d\n", num);
}
return ((void*)0);
}
int main(int argc, char** argv)
{
pthread_t tid1, tid2;
int err, i = 0, tmp = 0;
void* tret;
err = pthread_create(&tid1, NULL, sub1, NULL);//创建线程
if (err != 0)
{
printf("pthread_create error:%s\n", strerror(err));
exit(-1);
}
err = pthread_create(&tid2, NULL, sub2, NULL);
if (err != 0)
{
printf("pthread_create error:%s\n", strerror(err));
exit(-1);
}
for (; i < count; i++)
{
printf("main num-1前的值为:%d,", num);
tmp = num - 1;
printf("main num-1后tmp值为:%d,", tmp);
num = tmp;
printf("main num-1后的值为:%d\n", num);
}
printf("两个线程运行结束\n");
err = pthread_join(tid1, &tret);//阻塞等待ID为tid1的线程,直至该线程退出
if (err != 0)
{
printf("cannot join with thread1:%s\n", strerror(err));
exit(-1);
}
printf("thread1 exit code %d\n", (int)tret);
err = pthread_join(tid2, &tret);//阻塞等待ID为tid1的线程,直至该线程退出
if (err != 0)
{
printf("cannot join with thread2:%s\n", strerror(err));
exit(-1);
}
printf("thread2 exit code %d\n", (int)tret);
return 0;
}
加锁后:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>//Linux/Unix系统中内置头文件,包含许多系统函数
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
int num = 200, count = 30;
pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
void* sub1(void* arg)
{
for (int i = 0; i < count; i++)
{
pthread_mutex_lock(&mylock);
printf("线程1 num-1前值为:%d,", num);
int tmp = num - 1;
printf("线程1 num-1后tmp值为:%d,", tmp);
usleep(13);
num = tmp;
printf("线程1 num-1后的值为:%d\n", num);
pthread_mutex_unlock(&mylock);
}
return ((void*)0);
}
void *sub2(void *arg)
{
int i = 0, tmp;
for (; i < count; i++)
{
pthread_mutex_lock(&mylock);
printf("线程2 num-1前值为:%d,", num);
tmp = num - 1;
printf("线程2 num-1后tmp值为:%d,", tmp);
usleep(31);
num = tmp;
printf("线程2 num-1后的值为:%d\n", num);
pthread_mutex_unlock(&mylock);
}
return ((void*)0);
}
int main(int argc, char** argv)
{
pthread_t tid1, tid2;
int err, i = 0, tmp = 0;
void* tret;
err = pthread_create(&tid1, NULL, sub1, NULL);//创建线程
if (err != 0)
{
printf("pthread_create error:%s\n", strerror(err));
exit(-1);
}
err = pthread_create(&tid2, NULL, sub2, NULL);
if (err != 0)
{
printf("pthread_create error:%s\n", strerror(err));
exit(-1);
}
for (; i < count; i++)
{
pthread_mutex_lock(&mylock);
printf("main num-1前的值为:%d,", num);
tmp = num - 1;
printf("main num-1后tmp值为:%d,", tmp);
num = tmp;
printf("main num-1后的值为:%d\n", num);
pthread_mutex_unlock(&mylock);
}
printf("两个线程运行结束\n");
err = pthread_join(tid1, &tret);//阻塞等待ID为tid1的线程,直至该线程退出
if (err != 0)
{
printf("cannot join with thread1:%s\n", strerror(err));
exit(-1);
}
printf("thread1 exit code %d\n", (int)tret);
err = pthread_join(tid2, &tret);//阻塞等待ID为tid1的线程,直至该线程退出
if (err != 0)
{
printf("cannot join with thread2:%s\n", strerror(err));
exit(-1);
}
printf("thread2 exit code %d\n", (int)tret);
return 0;
}