关于include之后全局变量重定义的问题

在测试shared_ptr线程安全时出了这个问题。原因在于include之后,这个全局变量编译了两次(因为被main和实现cpp两个单元引用了)。原代码如下:

shared_ptr_test.h

#include<stdio.h>
#include<iostream>
#include <string.h>
#include <memory>
#include <mutex>
#include <thread>
using namespace std;
shared_ptr<long> global_instance;
void thread_fun();
void RunTestMain();

main.cpp

#include"shared_ptr_test.h"
int main(int argc, char** argv) {
	RunTestMain();
	cout << global_instance<<":" << *global_instance << endl;
} 

shared_ptr_test.cpp

#include"shared_ptr_test.h"
void thread_fun() {
	
	shared_ptr<long> local = global_instance;
	for (int i = 0; i < 10000000; i++) {
		*local = *local + 1;
	}
}
shared_ptr<long> global_instance = make_shared<long>(10L);
void RunTestMain() {
	thread thread1(thread_fun);
	thread thread2(thread_fun);

	thread1.join();
	thread2.join();
	cout << global_instance << ":" << *global_instance << endl;
}

解决方法一:加入extern关键字
shared_ptr_test.h(注意必须在外面再给global_instance赋一次值。)

extern shared_ptr<long> global_instance;

解决方法二:加static(这会将定义的范围限制为当前对象文件,并允许多个对象文件具有其自己的变量副本。 不建议在头文件中定义静态变量,因为可能会与全局变量混淆。 倾向于将静态变量定义移动到使用它们的源文件中。)

解决方法三:声明变量selectany,这会告知链接器选取一个定义供所有外部引用使用,并放弃其余定义。 组合导入库时,此解决方案有时很有用。 否则,不建议将它用作避免链接器错误的方法。

shared_ptr_test.h

__declspec(selectany) shared_ptr<long> global_instance;
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值