首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

验证g++-4.7.2对线程支持时遇到有关问题

2012-11-11 
验证g++-4.7.2对线程支持时遇到问题C/C++ code#includeiostream#define _GLIBCXX_USE_SCHED_YIELD#defin

验证g++-4.7.2对线程支持时遇到问题

C/C++ code
#include<iostream>#define _GLIBCXX_USE_SCHED_YIELD#define _GLIBCXX_USE_NANOSLEEP#include<thread>using namespace std;void thread_fun(const char * name){        for(int i = 0 ; i < 100 ; ++i)        {                cout<<name<<'\t'<<i<<endl;                this_thread::yield();        }}int main(){        thread t1(thread_fun, "t1");        thread t2(thread_fun, "t2");        t1.detach();        t2.detach();        chrono::milliseconds dura(2000);        this_thread::sleep_for(dura);        return 0;}


上面的代码中用到了两个宏,是我看g++源码才发现的。

那GNU为什么要这么做呢?非得先说明我要用哪个哪个功能了才能使用?这些宏的定义也不属于标准吧?

[解决办法]
看这里 : 'yield' is not a member of 'std::this_thread'

You shouldn't define _GLIBCXX_USE_NANOSLEEP or _GLIBCXX_USE_SCHED_YIELD in your code. They are GCC/libstdc++-internal macros, so that's what should define it. If they aren't defined, it's because GCC wasn't configured with the option to check for availability of the functions. Since there are apparently no downsides to enabling that option for whatever system it is you're using, you could ask whoever provides your GCC to do so. Until that's done, a safer hack than enabling the macro in your code is to modify the c++ config.h file on your system to define the macros.

意思是让你去config.h定义这些宏.

热点排行