在测试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;