void foo() {}
thread(&foo);
结果报错 error: ‘ foo ’ declared as reference but not initialized
a = thread(&foo);
把它赋给一个 thread 类型变量 a ,就编译通过了
或者
thread a(&foo);
也能通过
为什么呢?谢谢
1 aigebiu OP 又试了下 thread(&foo).join() 也能过 好像只要把初始化好的 thread 对象用在任何一个地方 (赋值右边,调用方法) 就能过…… |
![]() | 2 bengol 2016-03-07 19:55:18 +08:00 via Android 贴编译器版本 |
4 patrickstar 2016-03-07 20:02:35 +08:00 不是直接 std::thread(foo)就可以了吗,为啥子在函数名 foo 前加个& |
5 aigebiu OP 诶我好像明白错误提示的意思了。。 编译器把它解释为 thread &foo; 了 声明一个引用但没初始化 所以报错。。 error: ‘ foo ’ declared as reference but not initialized thread &foo; error: ‘ foo ’ declared as reference but not initialized thread(&foo); 那么问题就变成了 为什么会解析成声明呢? |
6 aigebiu OP @patrickstar 嗯 是…… 但是显式地取地址也可以吧? |
7 aigebiu OP 难道类(变量) 也是一种声明变量的方式? 比如 string(a); 也能过 得到一个空字符串 a (之前没声明 a ) |
8 patrickstar 2016-03-07 20:11:46 +08:00 一般情况下函数名本身就是一个指针,再取地址就是指针的指针,不是 std::thread() 需要的参数 |
9 aigebiu OP @patrickstar 可是&后也能正常运行啊 函数指针取不取地址应该是一样的(只有在访问成员函数时好像有区别) 贴个 stackoverflow 上的答案( http://stackoverflow.com/questions/4298654/operator-optional-in-function-pointer-assignment ) Operator & is indeed optional when taking the address of a function in your context (assigning it to something). It is not compiler-specific, it follows from the formal definition of the language. Symmetrically, operator * is optional when invoking the function through a pointer. In your example, you could invoke the function as either (*logfunc)(n) or logfunc(n). You used the latter, but the former would work as well. |
![]() | 10 ffffwh 2016-03-07 20:50:06 +08:00 via Android C++语法二义性很多的。 这种情况会优先判为 declaration 而不是函数调用,好在一般都有编译器 warning |
![]() | 11 matthewgao 2016-03-07 20:51:41 +08:00 via Android 你这里还有个问题,如果你只写 thread(foo),那么你生成的这个 thread 对象就是个右值对象,没有任何产量标识它,那么它一旦 go out of scope 那么就会 segmentalfault, 但是你写 thread(foo).join 会阻塞主进程,所以线程会正常运行 |
12 aigebiu OP @matthewgao 哦 谢谢 那 c ++里有没有像 go 语言里的 go 那样的轻量级并发呢?我是一个线程不断读数据 然后动态创建若干线程处理数据 是不是只能维护一个线程池 主线程终止时统一 join 呢? @ffffwh 为什么会优先判为 declaration ?有没有这方面资料、书推荐呢?谢谢 |
13 vanxining 2016-03-07 21:05:42 +08:00 学习了。我想应该是语法优先级的问题。就像 C++11 之前的 vector<vector<int>>以及 Foo foo(int);这样的情况一样。 |
![]() | 14 bombless 2016-03-07 21:44:38 +08:00 via Android 噗 c++本身是不支持嵌套的函数定义的,所以本身函数调用和函数定义是不会在一个层次里出现的。 你这个是 gcc 扩展。 |
15 vanxining 2016-03-07 22:10:03 +08:00 |
![]() | 16 congeec 2016-03-07 22:26:33 +08:00 @patrickstar 函数名就是函数指针,加个取地址符号&还是函数指针。这是特例 |
![]() | 17 xuboying 2016-03-07 22:29:19 +08:00 via Android 插个楼,这里的函数可以用 lambda 定义么? |
![]() | 19 matthewgao 2016-03-08 00:15:28 +08:00 via Android @xuboying 当然可以啦 |
![]() | 20 matthewgao 2016-03-08 00:18:47 +08:00 via Android @aigebiu 这个看你具体需求了,不 join detach 也可以,如果你这一堆线程都只干这一个事 |