我昨天发布了一个问题,但是我没有一个可运行的示例来介绍其他用户。现在,我删除了以前的帖子。
我有一些代码,当我试图制造和使用该编译失败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();
但由于它是静态的,因此您必须创建该类的实例以传递给thread
s构造函数。您可以为两个调用共享一个对象,也可以为每个线程分配自己的对象。那看起来像
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] 删除。
我来说两句