pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

std::thread 通過引用調用復制構造函數

std::thread pass by reference calls copy constructor(std::thread 通過引用調用復制構造函數)
本文介紹了std::thread 通過引用調用復制構造函數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

好吧,我在使用 std::thread 將數據傳遞到線程時遇到了問題.我以為我了解復制構造函數等的一般語義,但似乎我不太明白這個問題.我有一個名為 Log 的簡單類,它因此隱藏了它的復制構造函數:

Well I have an issue with passing data into a thread using std::thread. I thought I understood the general semantics of copy constructors, etc. but it seems I don't quite grasp the problem. I have a simple class called Log that has hidden it's copy constructor thusly:

class Log
{
public:
    Log(const char filename[], const bool outputToConsole = false);
    virtual ~Log(void);

    //modify behavior
    void appendStream(std::ostream *);
    //commit a new message
    void commitStatus(const std::string str);

private:
    //members
    std::ofstream fileStream;
    std::list<std::ostream *> listOfStreams;

    //disable copy constructor and assignment operator
    Log(const Log &);
    Log & operator=(const Log &);
}

現在我有一個主要基于 http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp11/echo/blocking_tcp_echo_server.cpp

now I have a main based heavily on http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp11/echo/blocking_tcp_echo_server.cpp

int main()
{
    static int portNumber = 10000;

    Log logger("ServerLog.txt", true);
    logger.commitStatus("Log Test String");

    try {
        boost::asio::io_service ioService;
        server(ioService, portNumber, logger);
    }
    catch (std::exception &e)
    {
        std::cerr << "Exception " << e.what() << std::endl;
        logger.commitStatus(e.what());
    }

    return 0;
}

可以看到main調用了函數server,傳遞了IOService、portNumber和logger.記錄器是通過引用傳遞的,因此:

You can see that main calls the function server and passes the IOService, portNumber and logger. The logger is passed by reference, thusly:

using boost::asio::ip::tcp;

void server(boost::asio::io_service &ioService, unsigned int port, Log &logger)
{
    logger.commitStatus("Server Start");

    tcp::acceptor acc(ioService, tcp::endpoint(tcp::v4(), port));

    while(true)
    {
        tcp::socket sock(ioService);
        acc.accept(sock);

        std::thread newThread(session, &sock, logger);
        newThread.detach();
    }

    logger.commitStatus("Server closed");
}

當我嘗試通過引用將記錄器(或套接字)傳遞給線程時出現編譯器錯誤,但通過引用將它傳遞給 session() 時我沒有收到錯誤

I get a compiler error when I try to pass the logger (or the socket) to the thread by reference, but I do not get the error when passing it to the session() by reference

static void session(tcp::socket *sock, Log &logger)
{
    std::cout << " session () " << std::endl;
}

現在我認為我正確理解了引用與傳遞指針相同.也就是說,它不調用復制構造函數,它只是傳遞指針,它讓您在語法上將其視為不是指針.

Now I thought that I understood correctly that a reference is the same as passing a pointer. That is, it does not call the copy constructor, it simply passes the pointer, which it lets you syntactically treat like it's not a pointer.

錯誤 C2248:'Log::Log':無法訪問類 'Log' 中聲明的私有成員

error C2248: 'Log::Log' : cannot access private member declared in class 'Log'

1> log.h(55) : 見 'Log::Log' 的聲明

1> log.h(55) : see declaration of 'Log::Log'

1> log.h(28) : 見 'Log' 的聲明

1> log.h(28) : see declaration of 'Log'

...

:參見正在編譯的函數模板實例化'std::thread::thread(_Fn,_V0_t &&,_V1_t)'的參考

: see reference to function template instantiation 'std::thread::thread(_Fn,_V0_t &&,_V1_t)' being compiled

1> 與

1> [

1> Fn=void (_cdecl *)(boost::asio::ip::tcp::socket *,Log &),

1> Fn=void (_cdecl *)(boost::asio::ip::tcp::socket *,Log &),

1> _V0_t=boost::asio::ip::tcp::socket *,

1> _V0_t=boost::asio::ip::tcp::socket *,

1> _V1_t=日志 &

1> _V1_t=Log &

1>]

但是如果我修改它來傳遞一個指針,一切都會很開心

However if I modify it to pass a pointer, everything is happy

...
        std::thread newThread(session, &sock, &logger);
...

static void session(tcp::socket *sock, Log *logger)
{
    std::cout << " session () " << std::endl;
}

為什么通過引用傳遞調用我的復制構造函數.由于std::thread,這里有什么特別的事情發生嗎?我是否誤解了復制構造函數并通過引用傳遞?

Why is passing by reference calling my copy constructor. Is there something special happening here because of std::thread? Did I misunderstand the copy constructor and pass by reference?

如果我像示例中那樣嘗試使用 std::move() ,我會得到一個不同但同樣令人困惑的錯誤.我的 VS2012 是否可能沒有正確實現 C++11?

I get a different but equally baffling error if I try to use std::move() as it is done in the example. Is it possible my VS2012 is not implementing C++11 correctly?

推薦答案

std::thread 按值接受其參數.你可以通過使用 std::reference_wrapper:

std::thread takes its arguments by value. You can get reference semantics back by using std::reference_wrapper:

std::thread newThread(session, &sock, std::ref(logger));

顯然,您必須確保 logger 比線程壽命更長.

Obviously you must make sure that logger outlives the thread.

這篇關于std::thread 通過引用調用復制構造函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: 透平油真空滤油机-变压器油板框滤油机-滤油车-华之源过滤设备 | 济南玻璃安装_济南玻璃门_济南感应门_济南玻璃隔断_济南玻璃门维修_济南镜片安装_济南肯德基门_济南高隔间-济南凯轩鹏宇玻璃有限公司 | 芜湖厨房设备_芜湖商用厨具_芜湖厨具设备-芜湖鑫环厨具有限公司 控显科技 - 工控一体机、工业显示器、工业平板电脑源头厂家 | 隔离变压器-伺服变压器--输入输出电抗器-深圳市德而沃电气有限公司 | 湖南成人高考报名-湖南成考网| 睿婕轻钢别墅_钢结构别墅_厂家设计施工报价 | 珠海白蚁防治_珠海灭鼠_珠海杀虫灭鼠_珠海灭蟑螂_珠海酒店消杀_珠海工厂杀虫灭鼠_立净虫控防治服务有限公司 | 大连海岛旅游网>>大连旅游,大连海岛游,旅游景点攻略,海岛旅游官网 | 岸电电源-60HZ变频电源-大功率变频电源-济南诚雅电子科技有限公司 | LNG鹤管_内浮盘价格,上装鹤管,装车撬厂家-连云港赛威特机械 | 手持式浮游菌采样器-全排二级生物安全柜-浙江孚夏医疗科技有限公司 | 外观设计_设备外观设计_外观设计公司_产品外观设计_机械设备外观设计_东莞工业设计公司-意品深蓝 | 鄂泉泵业官网|(杭州、上海、全国畅销)大流量防汛排涝泵-LW立式排污泵 | 蓄电池回收,ups电池后备电源回收,铅酸蓄电池回收,机房电源回收-广州益夫铅酸电池回收公司 | 冲锋衣滑雪服厂家-冲锋衣定制工厂-滑雪服加工厂-广东睿牛户外(S-GERT) | 拉力测试机|材料拉伸试验机|电子拉力机价格|万能试验机厂家|苏州皖仪实验仪器有限公司 | 比士亚-专业恒温恒湿酒窖,酒柜,雪茄柜的设计定制 | 浇钢砖,流钢砖_厂家价低-淄博恒森耐火材料有限公司 | 挤出熔体泵_高温熔体泵_熔体出料泵_郑州海科熔体泵有限公司 | 远程会诊系统-手术示教系统【林之硕】医院远程医疗平台 | 吊篮式|移动式冷热冲击试验箱-二槽冷热冲击试验箱-广东科宝 | 东莞螺丝|东莞螺丝厂|东莞不锈钢螺丝|东莞组合螺丝|东莞精密螺丝厂家-东莞利浩五金专业紧固件厂家 | 专业生物有机肥造粒机,粉状有机肥生产线,槽式翻堆机厂家-郑州华之强重工科技有限公司 | 订做不锈钢_不锈钢定做加工厂_不锈钢非标定制-重庆侨峰金属加工厂 | 欧盟ce检测认证_reach检测报告_第三方检测中心-深圳市威腾检验技术有限公司 | 东莞市踏板石餐饮管理有限公司_正宗桂林米粉_正宗桂林米粉加盟_桂林米粉加盟费-东莞市棒子桂林米粉 | 杭州网络公司_百度SEO优化-外贸网络推广_抖音小程序开发-杭州乐软科技有限公司 | 高速混合机_锂电混合机_VC高效混合机-无锡鑫海干燥粉体设备有限公司 | pbt头梳丝_牙刷丝_尼龙毛刷丝_PP塑料纤维合成毛丝定制厂_广州明旺 | 电池挤压试验机-自行车喷淋-车辆碾压试验装置-深圳德迈盛测控设备有限公司 | 楼梯定制_楼梯设计施工厂家_楼梯扶手安装制作-北京凌步楼梯 | 餐饮加盟网_特色餐饮连锁加盟店-餐饮加盟官网| 喷砂机厂家_自动除锈抛丸机价格-成都泰盛吉自动化喷砂设备 | 南京蜂窝纸箱_南京木托盘_南京纸托盘-南京博恒包装有限公司 | 数字展示在线_数字展示行业门户网站 | 天津次氯酸钠酸钙溶液-天津氢氧化钠厂家-天津市辅仁化工有限公司 | 蔡司三坐标-影像测量机-3D扫描仪-蔡司显微镜-扫描电镜-工业CT-ZEISS授权代理商三本工业测量 | 济南画室培训-美术高考培训-山东艺霖艺术培训画室 | 长沙中央空调维修,中央空调清洗维保,空气能热水工程,价格,公司就找维小保-湖南维小保环保科技有限公司 | AR开发公司_AR增强现实_AR工业_AR巡检|上海集英科技 | 14米地磅厂家价价格,150吨地磅厂家价格-百科|