【Linux】线程安全问题

线程安全

一、线程安全的定义
线程安全就是在多线程运行的时候,不论线程的调度顺序怎样,最终的结果都是一样的,正确的,那么就说这些线程是安全的。
二、如何保证线程是安全的
1、对线程同步,保证同一时刻只有一个线程访问临界资源
先看一段线程不安全的代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
int g = 0;
void* fun(void *arg)
{
	int i = 0;
	for(; i < 10000; ++i)
	{
		g++;
	}
}
int main()
{
	pthread_t id;
	int res = pthread_create(&id, NULL, fun, NULL);
	assert(res == 0);
	int i = 0;
	for(; i < 10000; ++i)
	{
		g++;
	}
	pthread_join(id, NULL);//保证函数线程已经结束了
	printf("g = %d\n", g);
	exit(0);
}

运行之后我们发现,每次执行的结果都不一样:
在这里插入图片描述
因为线程安全的定义来说:论线程的调度顺序怎样,最终的结果都是一样的。这说明此线程是不安全的。那为什么不安全呢?

  • 首先,g是一个全局变量,同一进程中的多线程是共享的
  • 其次就是g++不是原子操作,他会从内存中读值,然后将CPU进行+1,最后将结果写回内存。
  • 这样就会使得每次的结果不一样。
    四、避免线程不安全
    我们用互斥锁来避免线程不安全
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
int g = 0;
pthread_mutex mutex;
void* fun(void *arg)
{
	int i = 0;
	for(; i < 10000; ++i)
	{
		pthread_mutex_lock(&mutex);
		g++;
		pthread_mutex_unlock(&mutex);
	}
}
int main()
{
	pthread_mutex_init(&mutex, NULL);
	pthread_t id;
	int res = pthread_create(&id, NULL, fun, NULL);
	assert(res == 0);
	int i = 0;
	for(; i < 10000; ++i)
	{
		pthread_mutex_lock(&mutex);
		g++;
		pthread_mutex_unlock(&mutex);
	}
	pthread_join(id, NULL);//保证函数线程已经结束了
	printf("g = %d\n", g);
	exit(0);
}

执行结果:
在这里插入图片描述
这里可以看到,每次执行的结果都是一样的
2、在多线程中使用线程安全的函数(可重入函数),所谓线程安全的函数指的是:如果一个函数能被多个线程同时调用且不发生竞态条件,则我们认为它是线程安全。
先看一段不安全的代码

#include <stdio.h>
#include <stdlib.h>
#include <unsitd.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
void * fun(void *arg)
{
	char buff[] = "1 2 3 4 5 6 7 8";
	char *p = strtok(buff, " ");
	while(p)
	{
		printf("fun: %s\n", p);
		p = strtok(NULL, " ");
		sleep(1);
	}
}
int main()
{
	pthread_t id;
	int res = pthread_create(&id, NULL, fun, NULL);
	assert(res == 0);
	char str[] = "a b c d e f g h ";
	char *p = strtok(str, " ");
	while(p)
	{
		printf("main:%s\n", p);
		p = strtok(NULL, " ");
		sleep(1);
	}
	pthread_exit(0);
}

执行结果如下:
在这里插入图片描述
我们发现主线程每次打印的时候不仅有字母还有数字。这样的线程是不安全的。

#include <stdio.h>
#include <stdlib.h>
#include <unsitd.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
void * fun(void *arg)
{
	char buff[] = "1 2 3 4 5 6 7 8";
	char *q = NULL;
	char *p = strtok_r(buff, " ",&q);//并不能保证线程安全
	while(p)
	{
		printf("fun: %s\n", p);
		p = strtok_r(NULL, " ",&q);
		sleep(1);
	}
}
int main()
{
	pthread_t id;
	int res = pthread_create(&id, NULL, fun, NULL);
	assert(res == 0);
	char str[] = "a b c d e f g h ";
	char *q = NULL;
	char *p = strtok_r(str, " ",&q);
	while(p)
	{
		printf("main:%s\n", p);
		p = strtok_r(NULL, " ",&q);
		sleep(1);
	}
	pthread_exit(0);
}

执行结果如下:
在这里插入图片描述
我们发现主线程永远切割的是字符,函数线程切割的是数字,这样线程就安全了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值