【已解决】[cocos2d-x-v3.8.1][无法创建C++11 线程锁]

我想写一个线程安全的单例模式,需要用到互斥锁,C11 新标准中提供了新的互斥锁对象mutex。代码如下:

#include

std::mutex mtx;

//单例模式
getInstance()
{
mtx.lock();
mtx.unlock();
}

加锁之后会报错:

2>HelloWorldScene.obj : error LNK2005: “class std::mutex mtx” (?mtx@@3Vmutex@std@@A) 已经在 AppDelegate.obj 中定义
2>ImageManager.obj : error LNK2005: “class std::mutex mtx” (?mtx@@3Vmutex@std@@A) 已经在 AppDelegate.obj 中定义

重复包含:#include 了?

hi:
大神,我把代码贴出来你看一下:
#ifndef HELLOWORLD_SCENE_H
#define HELLOWORLD_SCENE_H

#include “cocos2d.h”

#include

std::mutex mtx;

class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();

virtual bool init();

// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);

// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);

};

#endif // HELLOWORLD_SCENE_H

头文件中不可以直接定义变量和函数,但是可以加上static就没问题了。