插入“这个”从构造函数转换为 STL 映射
版本 1
class Doh { private: static std::map<const std::string, const Doh*> someMap; std::string stringValue_; public: Doh(std::string str) : stringValue_(str) { Doh::someMap.insert( std::make_pair<const std::string,const Doh*> (this->stringValue_,this) ); } }
上面的内容在 MSVC 2010 中是可以的,但在 MSVC 2008 中却失败了——我猜这是因为对象插入到映射中时尚未构造(我遇到了内存访问冲突)。
因此,我尝试了延迟插入,它起作用了:
版本 2
Doh(std::string str) : stringValue_(str) { boost::thread(&Doh::insertIntoTheStaticMap,this); } void insertIntoTheStaticMap() { boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); Doh::someMap.insert( std::make_pair<const std::string,const Doh*> (this->stringValue_,this) ); }But as you might be able to guess, my intention is to have the static Doh::someMap as a common lookup dictionary.
版本 1 不需要任何线程安全,因为我会在同一个线程中创建所有 Doh 实例 - 在初始化块中 - 这将被称为在进入 main() 之前通过动态初始化程序。
但对于 VERSION 2,简单的 sleep() 既不优雅也不可靠(更不用说,我可能需要在插入之前锁定映射)。
什么是好的 KISS 方法?
VERSION 1
class Doh { private: static std::map<const std::string, const Doh*> someMap; std::string stringValue_; public: Doh(std::string str) : stringValue_(str) { Doh::someMap.insert( std::make_pair<const std::string,const Doh*> (this->stringValue_,this) ); } }
The above was ok with MSVC 2010 but with MSVC 2008 it fails – and I guess it is because the object is not constructed yet when it is inserted in the map (I got a memory access violation).
So, I tried a delayed insertion, which worked:
VERSION 2
Doh(std::string str) : stringValue_(str) { boost::thread(&Doh::insertIntoTheStaticMap,this); } void insertIntoTheStaticMap() { boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); Doh::someMap.insert( std::make_pair<const std::string,const Doh*> (this->stringValue_,this) ); }
But as you might be able to guess, my intention is to have the static Doh::someMap as a common lookup dictionary.
VERSION 1 didn’t need any thread-safety because I would create all Doh instances in the same thread – in initialization blocks - which would be called by dynamic initializers before I enter main().
But with VERSION 2, the naïve sleep() is neither graceful nor reliable (not to mention, I might need to lock the map before insertion).
What would be a nice KISS approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到的唯一潜在问题是静态成员的初始化(如果有多个源文件)。尝试用函数来保护它。
Only potential issue I see is the initialization of the
static
member, if there are multiple source files. Try guarding it with a function.在这两个版本中都没有任何 stringvalue_ init 的迹象 - 当您在代码版本 1 中点击映射插入时,调试器会向您显示有关此键的什么信息?这个字段是如何设置的,它的类型是什么?
我想,在 VS2008 的调试器中运行它应该可以让您将故障点缩小到
源中。
In neither version is there any sign of init for stringvalue_ - what does the debugger show you about this key when you hit the map insert in version 1 of the code? How is this field set up, and what is its type?
Running this in the debugger for VS2008 should allow you to narrow down the point of failure into the
<map>
source, I would have thought.