热爱学习的呆萌的文青 2016-01-08 04:57 采纳率: 54.8%
浏览 1892

C++ 线程和仿函数的问题??

 #include<iostream>
#include<thread>
using namespace std;
void function_1()
{
    std::cout << "Hello,world!" << std::endl;
}
class Fctor {                                      //仿函数
public:
    void operator()() {                    //对()进行重载
        for (int i = 0; i > -100; i--) {
            cout << "from t1 " << i << endl;
        }
    }
};
int main() {
    //Fctor fct;
    std::thread t1((Fctor()));         //t1  starts  running. 实例化一个线程对象t1
    try {
        for (int i = 0; i < 100; i++)
            cout << "from main: " << i << endl;
    }
    catch (...) {
        t1.join();
        throw;
    }
    t1.join();
    //t1.detach();        
    return 0;
}

其中这句 std::thread t1((Fctor())); 为什么要再“Fctor()”两边还要加括号?否则“ t1.join();”这句会提示错误??
还有能解释下执行结果吗?
图片说明

  • 写回答

3条回答 默认 最新

  • oyljerry 2016-01-08 05:10
    关注

    thread要求一个线程函数,所以你不能用类名,而需要用它的仿函数。

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)