栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

实验 线程编程-加1操作为什么会出错?【操作系统】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实验 线程编程-加1操作为什么会出错?【操作系统】

线程编程-加1操作为什么会出错?
  • 出错
    • 源代码
    • 运行结果
  • 修改1--原子操作
    • 代码
    • 结果
  • 修改2--加锁
    • 代码
    • 结果

出错

在这个代码中,函数thread()有一个循环,做了简单的加1操作。 在主函数中,调用pthread_create()创建了一个线程,这个循环16次,总共创建了16个线程,运行这个程序,我们本来的期望结果是16*10000, 但是发现结果不是160000,那到底为什么?如何解决?

源代码

9-.c

#include 
#include 
#include 
#include 
#include 
//开后16个线程。
#define PTHREAD_NUM 16
unsigned long sum =0;
void *thread(void *arg){
	//不使用原子锁
	int i;
	for(i= 0;i <10000;i++)  
		sum+=1;

}

int main(void){
	printf("before ...sum = %lun",sum);
	pthread_t pthread[PTHREAD_NUM];			//被创建线程的标识
	int ret;								//接收近回值
	void *retval[PTHREAD_NUM];
	int i;
	for(i= 0; i< PTHREAD_NUM; i++){
		ret = pthread_create(&pthread[i], NULL, thread, NULL);  
		if(ret !=0){
			perror("cause:");
			printf("creat pthread %d failed.n",i+1);
		}
	}
	for (i=0;i
		pthread_join(pthread[i], &retval[i]);  
	}
	
	printf("after ... sun = %lun",sum);
	return	0;
}


运行结果

修改1–原子操作

我看到,加1操作实际上是由汇编指令的三条指令完成的,如果这三条指令在执行期间不被中断,一口气执行完,那就不出错了,因此,就有了原子操作的概念,所谓原子操作,就是在执行期间不可分割,要么全部执行,要么一条也不执行。 在Linux下如何进行原子操作? gcc从4.1.2开始提供了__sync_*系列的build-in函数,用于提供加减和逻辑运算的原子操作,其声明如下:

type __sync_fetch_and_add (type *ptr, type value, ...)
type __sync_fetch_and_sub (type *ptr, type value, ...)
type __sync_fetch_and_or (type *ptr, type value, ...)

...

比如,__sync_add_and_fetch()是GCC内嵌的函数,可以进行加1的原子操作,请参看__sync_fetch_and_add系列原子操作函数述

代码

9.c

#include 
#include 
#include 
#include 
#include 
//开后16个线程。
#define PTHREAD_NUM 16
unsigned long sum =0;
void *thread(void *arg){
	//不使用原子锁
	int i;
	for(i= 0;i <10000;i++)  
		__sync_add_and_fetch(&sum,1);

}

int main(void){
	printf("before ...sum = %lun",sum);
	pthread_t pthread[PTHREAD_NUM];			//被创建线程的标识
	int ret;								//接收近回值
	void *retval[PTHREAD_NUM];
	int i;
	for(i= 0; i< PTHREAD_NUM; i++){
		ret = pthread_create(&pthread[i], NULL, thread, NULL);  
		if(ret !=0){
			perror("cause:");
			printf("creat pthread %d failed.n",i+1);
		}
	}
	for (i=0;i
		pthread_join(pthread[i], &retval[i]);  
	}
	
	printf("after ... sun = %lun",sum);
	return	0;
}


结果

修改2–加锁

最简单的处理办法就是加锁保护,请参看
POSIX详解pthread_mutex_lock

代码

9_.c

#include 
#include 
#include 
#include 
#include 
//开后16个线程。
#define PTHREAD_NUM 16
unsigned long sum =0;
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;
void *thread(void *arg){
	//不使用原子锁
	int i;
	for(i= 0;i <10000;i++){
		pthread_mutex_lock(&mymutex); 
		sum+=1;
		pthread_mutex_unlock(&mymutex);
		
	}
}

int main(void){
	printf("before ...sum = %lun",sum);
	pthread_t pthread[PTHREAD_NUM];			//被创建线程的标识
	int ret;								//接收近回值
	void *retval[PTHREAD_NUM];
	int i;
	for(i= 0; i< PTHREAD_NUM; i++){
		ret = pthread_create(&pthread[i], NULL, thread, NULL);  
		if(ret !=0){
			perror("cause:");
			printf("creat pthread %d failed.n",i+1);
		}
	}
	for (i=0;i
		pthread_join(pthread[i], &retval[i]);  
	}
	
	printf("after ... sun = %lun",sum);
	return	0;
}


结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/875631.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号