Boost Thread Specific指针get()方法所花费的大概时间

时间:2013-11-01 11:18:08

标签: c++ windows multithreading performance thread-local-storage

我们正在使用boost thread特定指针来存储该特定线程的一些全局数据。下面是当有人调用GetInstance()时返回的单例。

我的问题是,与普通指针访问相比,获取线程特定指针(m_tspConnectionManager.get();)大约需要多长时间?

我使用了下面的代码(我在两次调用.get()方法)并且花了大约3秒来完成整个函数。

  typedef boost::thread_specific_ptr<ConnectionManager> ConnMgrPtr;
    static ConnMgrPtr m_tspConnectionManager;
    static ConnectionManager* GetInstance()
            {
                if(!m_tspConnectionManager.get())
                {
                    //first time called by this thread
                    //ConnectionManager* to be used in all subsequent calls from this thread
                    m_tspConnectionManager.reset(new ConnectionManager());
                }
                return m_tspConnectionManager.get();
            }

现在,我更改了上面的代码只调用一次.get()方法,大约需要1.9秒。

    static ConnectionManager* GetInstance()
            {
                ConnectionManager* pConnMgr = m_tspConnectionManager.get();
                if(pConnMgr == NULL)
                {
                    //first time called by this thread
                    //ConnectionManager* to be used in all subsequent calls from this thread
                    m_tspConnectionManager.reset(new ConnectionManager());
                }
                return pConnMgr != NULL ? pConnMgr : m_tspConnectionManager.get();
            }

所以,只是不调用.get()方法我可以看到1.1秒的性能提升。我一直试图了解我们如何获得收益? 注意:此增益可能是多次调用GetInstance()函数的共同收益。只是想在这里理解单个呼叫增益。

1 个答案:

答案 0 :(得分:0)

get()不太可能花费很多时间(当然,它比不调用get()慢。)使用TLS的“正常”实现,你的对象最多只有三个指针间接而去。 (线程块寄存器 - &gt; TLS块 - &gt;你的指针。)但如果你正在调用GetInstance(),那么任何减速都会变成一个大的减速。

我会看看GetInstance()被调用的频率。尝试通过缓存返回的指针来减少该数量。当然,第二种实现比第一种更好。