跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • c++11 std::thread 和 std::bind 请教,成员函数的参数是另一个成员函数
未分類
19 5 月 2020

c++11 std::thread 和 std::bind 请教,成员函数的参数是另一个成员函数

c++11 std::thread 和 std::bind 请教,成员函数的参数是另一个成员函数

資深大佬 : auto8888 1

成员函数:
void Adas::udpBind(unsigned short LocalPort,void (Adas::*fun)(std::string))
在构造函数里:
std::bind(&Adas::udpBind,this,this->LocalPort,&Adas::readStatus) 这编译 OK 。
但是
std::thread thread1(std::bind(&Adas::udpBind,this,this->LocalPort,&Adas::readStatus));
thread1.join();
编译失败了。
std::thread 类成员函数(成员函数的参数还有成员函数,有点绕,汗)不能作为线程函数吗?
还是我写法有问题啊
大佬有話說 (10)

  • 資深大佬 : codehz

    0202 年了,为何不直接用 lambda…可读性都好很多

  • 資深大佬 : kidtest

    应该是写法有问题,可以贴一下更完整的代码,或者去网上搜一搜 “std::thread member function”,对比一下。

  • 資深大佬 : FH0

    @codehz lambda 确实方便,就是套娃 lambda 之后缩进不美观

  • 資深大佬 : codehz

    @FH0 (clang-format 的受害者+1

  • 資深大佬 : chinuno

    好久没用函数指针了。你用 std:: function<void(Adas*, std::string)>不好吗

  • 資深大佬 : newmlp

    mem_fn

  • 資深大佬 : wtof

    可能其他地方写错了?
    我简单试了试 bind 的语法应该是没什么问题的,msvc 、gcc 都可以过
    https://godbolt.org/z/Ivuc2b

  • 主 資深大佬 : auto8888

    搞定了,
    void Adas::udpBind(unsigned short LocalPort,void (Adas::*fun)(std::string))改为
    void Adas::udpBind(unsigned short LocalPort,void (Adas::*fun)(const std::string&))
    就行了,别问我 我也不知道为什么。。。。。
    还有 lambda
    std::thread thread1([=] {udpBind( mStatus_Port,&Adas::readStatus); });
    thread1.join();
    有一说一,lambda 有点帅气。。

  • 資深大佬 : wtof

    @auto8888 那可能是你 Adas::readStatus 这个函数签名的问题

  • 主 資深大佬 : auto8888

    @wtof 是的 是函数签名的问题

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具