在类内部使用std :: thread()和std :: ref()会导致生成错误

y

我昨天发布了一个问题,但是我没有一个可运行的示例来介绍其他用户。现在,我删除了以前的帖子。

我有一些代码,当我试图制造和使用该编译失败std::threads()std::ref()一类里面。我不理解所产生的错误消息,这些错误消息均以error: no matching function for call开头error: no type named ‘type’

我正在使用Clion和CMake,以防万一。我的文件结构是:

Personal
--include
----main.h
--src
----main.cpp
----CMakeLists.txt
--CMakeLists.txt

CMakeLists.txt

# CMAKE version requirement
cmake_minimum_required(VERSION 3.12)

# Project name
project(scrap CXX)

# Configure the build
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Debug)
add_compile_options(-W -Wall -ggdb)
include_directories(include)
include_directories(${CMAKE_SOURCE_DIR}/include)

# What is being built
add_executable(scrap)
add_subdirectory(src)

# Add external dependencies
find_package(Threads REQUIRED)
target_link_libraries(scrap ${CMAKE_THREAD_LIBS_INIT})

src / CMakeLists.txt

# Add targets
target_sources(scrap PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
        )

主文件

#ifndef PERSONAL_MAIN_H
#define PERSONAL_MAIN_H

#include <future>
#include <iostream>
#include <unistd.h>

class ScrapPaper
{
public:
    ScrapPaper();
    static void SpinUpThreads();
    void ThreadFunction1(std::promise<bool> &prom);
    void ThreadFunction2(std::promise<bool> &prom);

private:

};

#endif //PERSONAL_MAIN_H

main.cpp

#include "../include/main.h"

using namespace std;

void ScrapPaper::ThreadFunction1(promise<bool> &prom)
{
    cout << "Thread " << this_thread::get_id() << " working in ThreadFunction1!" << endl;
    sleep(10);

    cout << "Thread " << this_thread::get_id() << " finished sleeping in Function1" << endl;
    prom.set_value_at_thread_exit(true);
}

void ScrapPaper::ThreadFunction2(promise<bool> &prom)
{
    cout << "Thread " << this_thread::get_id() << " working in ThreadFunction2!" << endl;
    sleep(2);
    cout << "Thread " << this_thread::get_id() << " finished sleeping in Function2" << endl;
    prom.set_value_at_thread_exit(true);
}

void ScrapPaper::SpinUpThreads()
{
    promise<bool> promise1;
    future<bool> future1 = promise1.get_future();
    std::thread (&ScrapPaper::ThreadFunction1, ref(promise1)).detach();

    promise<bool> promise2;
    future<bool> future2 = promise2.get_future();
    std::thread (&ScrapPaper::ThreadFunction2, ref(promise2)).detach();

    if (future1.get() && future2.get())
    {
        cout << "Everything was a-okay" << endl;
    }
    else
    {
        cout << "Whoops, there was an error..." << endl;
    }
}

int main(int argc, char *argv[])
{
    cout << "In Main..." << endl;
    ScrapPaper::SpinUpThreads();

} // end main

我遇到的一些错误是:

error: no matching function for call to ‘std::thread::_Invoker<std::tuple<void (ScrapPaper::*)(std::promise<bool>&), std::reference_wrapper<std::promise<bool> > > >::_M_invoke(std::thread::_Invoker<std::tuple<void (ScrapPaper::*)(std::promise<bool>&), std::reference_wrapper<std::promise<bool> > > >::_Indices)’
  operator()()
  ^~~~~~~~

error: no type named ‘type’ in ‘struct std::__invoke_result<void (ScrapPaper::*)(std::promise<bool>&), std::reference_wrapper<std::promise<bool> > >’

当该类被删除时,只有一个main()ThreadFunction1(...)ThreadFunction2(...),代码将构建并运行。我有示波器问题吗?任何建议或帮助,我们将不胜感激!

内森·奥利弗

这里的问题是,由于您正在使用成员函数,因此需要一个类的实例来调用成员函数。如果SpinUpThreads不是静态的,则可以使用

std::thread (&ScrapPaper::ThreadFunction1, this, ref(promise1)).detach();
std::thread (&ScrapPaper::ThreadFunction2, this, ref(promise2)).detach();

但由于它是静态的,因此您必须创建该类的实例以传递给threads构造函数。您可以为两个调用共享一个对象,也可以为每个线程分配自己的对象。那看起来像

ScrapPaper common;
std::thread (&ScrapPaper::ThreadFunction1, common, ref(promise1)).detach();
std::thread (&ScrapPaper::ThreadFunction2, common, ref(promise2)).detach();

//or

std::thread (&ScrapPaper::ThreadFunction1, ScrapPaper{}, ref(promise1)).detach();
std::thread (&ScrapPaper::ThreadFunction2, ScrapPaper{}, ref(promise2)).detach();

您也可以使用lambda来简化调用语法。如果使用lambda,则可以以更自然的方式编写函数调用,这看起来像

ScrapPaper common;
std::thread ([&](){ common.ThreadFunction1(promise1); }).detach();
std::thread ([&](){ common.ThreadFunction2(promise2); }).detach();

//or

std::thread ([&](){ ScrapPaper{}.ThreadFunction1(promise1); }).detach();
std::thread ([&](){ ScrapPaper{}.ThreadFunction2(promise2); }).detach();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用std :: max会导致链接错误?

使用t(* this)会导致RuntimeError,而t(std :: ref(* this)不会

为什么std :: condition_variable作为类成员会导致std :: thread发生编译错误?

使用std :: shared_ptr和std :: thread的编译器错误

使用 std::sort() 对自定义类的 Vector 进行排序会导致分段错误

在共享库中使用std :: thread会导致SIGSEGV

By-ref参数:这是std :: thread和std :: bind之间的不一致吗?

C ++ std :: thread和方法类

使用“和*”移动文件会导致错误

std :: thread和rvalue参考

std :: thread的构造和执行

std :: thread和异常处理

将const类对象传递给std :: function和std :: bind导致编译错误

std :: thread导致程序中止

涉及`std :: string`的编译会导致Windows错误状态

std :: thread构造函数传递指针和ref传递之间有区别吗?

错误:使用已删除的函数'std :: thread :: thread(const std :: thread&)'

使用LongStream和jOOλ生成素数会导致StackOverflowError

升级libGdx和Gradle项目会导致生成错误

在类中使用std :: thread时出错

在ostream'<<'-运算符重载中使用std :: endl以及带有变体成员的可变模板类会导致编译器错误

std :: thread使用带有ref arg的lambda无法编译

同步push_back和std :: thread

C ++ std :: unique_ptr存储在std :: map内部,使用删除的函数会形成不良

使用 collect 和 flatten 进行断言会导致错误(groovy)

使用StreamWriter和StreamReader会导致错误“流不可读”

使用 memset 和 freeaddrinfo 会导致双重释放或损坏错误

std :: thread可能导致无法使用的堆栈跟踪

在std :: bind和std :: thread中移动语义/行为