https://blog.csdn.net/birdunderastarrysky/article/details/90573820
https://blog.csdn.net/birdunderastarrysky/article/details/90578635
注:每个函数的错误检查由于写上会导致不方便阅读,所以没加上。自己试验学习时请加上。
makefile
SRCS:=$(wildcard *.c)
TARGETS:=$(SRCS:%.c=%)
all:$(TARGETS)
%:%.c
gcc $< -g -o $@ -lpthread
clean:
rm $(TARGETS)
1线程不安全代码
#include <func.h>
static int global = 0;
void* thread_fun(void *p){
int loops = *(int*)p;
int loc, j;
for(j=0;j<loops;j++){
loc = global;
loc++;
global = loc;
}
pthread_exit(NULL);
}
int main(int argc, char* argv[])
{
ARGS_CHECK(argc, 2);//检查是否传入参数
int loops = atoi(argv[1]);//由传参决定循环次数
pthread_t pthid1, pthid2;
pthread_create(&pthid1, NULL, thread_fun, &loops);
pthread_create(&pthid2, NULL, thread_fun, &loops);
pthread_join(pthid1, NULL);
pthread_join(pthid2, NULL);
printf("global = %d\n", global);
exit(EXIT_SUCCESS);
return 0;
}
测试结果
分析:
开始时候,线程1先执行,由于数据量小,所以先是线程1执行完毕再到线程2执行,故所得结果正确。但是从倒数第二个测试开始我们可以发现两个线程发生了竞争。过程可能是这样的:
- 线程1将glob值赋给局部变量loc。假设当前glob的值为2000 。
- 线程1时间片满,而线程2开始执行。
- 线程2执行多次循环,假设当线程2时间片到期后,glob的值已增至3000。
- 线程1获得另一片时间片,并从上次停止处恢复执行。由于之前已经读取过glob的值,所以现在先递增loc值(从2000增至2001),然后将loc值赋给glob,这时候glob并不是我们想要的3001,而是2001。此前,线程2递增操作结果遭到覆盖。
将代码1中函数thread_fun()内for循环中3条语句加以替换,似乎可以解决这一问题:
glob++;//或者 ++glob;
不过,在很多硬件架构上,编译器仍会将这条语句转换成机器码,其执行步骤仍旧等同于上面的3条语句。也就是说,尽管C语言的递增符看似简单,其操作未必属于原子操作,依然可能发生上述行为。
2线程安全——互斥量
#include <func.h>
static int global = 0;
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
void* thread_fun(void *p){
int loops = *(int*)p;
int loc, j;
for(j=0;j<loops;j++){
pthread_mutex_lock(&mtx);
loc = global;
loc++;
global = loc;
pthread_mutex_unlock(&mtx);
}
pthread_exit(NULL);
}
int main(int argc, char* argv[])
{
ARGS_CHECK(argc, 2);//检查是否传入参数
int loops = atoi(argv[1]);//由传参决定循环次数
pthread_t pthid1, pthid2;
pthread_create(&pthid1, NULL, thread_fun, &loops);
pthread_create(&pthid2, NULL, thread_fun, &loops);
pthread_join(pthid1, NULL);
pthread_join(pthid2, NULL);
printf("global = %d\n", global);
exit(EXIT_SUCCESS);
return 0;
}
测试结果
3.互斥量的属性
#include <func.h>
#define N 10000000
static int global = 0;
void* thread_fun(void *p){
pthread_mutex_t *pmtx = (pthread_mutex_t*)p;
for(int j=0;j<N;j++){
pthread_mutex_lock(pmtx);
global += 1;
pthread_mutex_unlock(pmtx);
}
pthread_exit(NULL);
}
int main(int argc, char* argv[])
{
pthread_t p1, p2;
pthread_mutex_t mtx;
pthread_mutexattr_t mtxAttr;
pthread_mutexattr_init(&mtxAttr);
pthread_mutexattr_settype(&mtxAttr, PTHREAD_MUTEX_ERRORCHECK);//设置属性
pthread_mutex_init(&mtx, &mtxAttr);
pthread_create(&p1, NULL, thread_fun, &mtx);
pthread_create(&p2, NULL, thread_fun, &mtx);
pthread_join(p1, NULL);
pthread_join(p2, NULL);
printf("global = %d\n", global);
pthread_mutex_destroy(&mtx);
pthread_mutexattr_destroy(&mtxAttr);
return 0;
}
- PTHREAD_MUTEX_ERRORCHECK
- 对此类互斥量的所有操作都会执行错误检查。错误情况如下:1、线程试图对已由自己锁定的互斥量加锁;2、互斥量处于未锁定状态,或者已由其他线程锁定,对其解锁会导致不确定结果。
4条件变量的用处
#include <func.h>
static int avail = 0;
//生产者
void* thread_fun(void *p){
pthread_mutex_t* pmtx = (pthread_mutex_t*)p;
int n=3;//次数
while(n-->0){
pthread_mutex_lock(pmtx);
avail+=10;
printf("生产中ing, 新产品数为%d\n", avail);
pthread_mutex_unlock(pmtx);
usleep(400);
}
pthread_exit(NULL);
}
int main(int argc, char* argv[])
{
pthread_t produce;
pthread_mutex_t mtx;
pthread_mutex_init(&mtx, NULL);
pthread_create(&produce, NULL, thread_fun, &mtx);
//消费者
while(1){
pthread_mutex_lock(&mtx);
while(avail > 0){
avail--;
printf("销耗中,当前剩余产品:%d\n", avail);
usleep(200);
}
pthread_mutex_unlock(&mtx);
}
pthread_join(produce, NULL);
pthread_mutex_destroy(&mtx);
return 0;
}
5
6
7
8
9
10