1. 程式人生 > >‘sleep_for’ is not a member of ‘std::this_thread’

‘sleep_for’ is not a member of ‘std::this_thread’

在嵌入式開發中可能遇到這個問題。

解決方法:

加上_GLIBCXX_USE_NANOSLEEP巨集定義。

在qt的.pro檔案中加上這一句

DEFINES += _GLIBCXX_USE_NANOSLEEP

在linux Makefile 中則要加上

NEEDED_CXXFLAGS +=  -D_GLIBCXX_USE_NANOSLEEP

原因:

在gcc-linaro-arm-linux-gnueabihf-4.7-2013.03-20130313_linux/arm-linux-gnueabihf/include/c++/4.7.3/thread標頭檔案中,可以看到如下程式碼。

#ifdef _GLIBCXX_USE_NANOSLEEP
    /// sleep_for
    template<typename _Rep, typename _Period>
      inline void
      sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
      {
	chrono::seconds __s =
	  chrono::duration_cast<chrono::seconds>(__rtime);

	chrono::nanoseconds __ns =
	  chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);

	__gthread_time_t __ts =
	  {
	    static_cast<std::time_t>(__s.count()),
	    static_cast<long>(__ns.count())
	  };

	::nanosleep(&__ts, 0);
      }

    /// sleep_until
    template<typename _Clock, typename _Duration>
      inline void
      sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
      { sleep_for(__atime - _Clock::now()); }
#endif