【发布时间】:2017-04-10 14:01:07
【问题描述】:
我第一次尝试在我的类的方法上使用线程。
这是我的代码的简化版本,会导致相同的错误。
#include <thread>
#include <iostream>
#include <memory>
#include <vector>
class Foo{
public:
std::vector<int> DoThing() {
return {1};
}
std::vector<int> DoThingMultiThread(unsigned int threads) {
std::vector<int> values;
std::vector<std::thread> threadVec(threads);
for (unsigned int i = 0; i < threads; ++i)
{
threadVec.at(i) = std::thread(&Foo::DoPartialThing, this, i, i, std::ref(values));
}
return values;
}
private:
void DoPartialThing(unsigned int value, unsigned int position, std::vector<unsigned int> &container) {
container.at(position) = value;
}
};
在实际代码中,我希望每个线程填充更大的向量块。
我在带有 CMakeLists.txt 文件的 Clion 项目中有此代码:
cmake_minimum_required(VERSION 3.6)
project(ThreadTest)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS -pthread)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
set(SOURCE_FILES main.cpp foo.hpp foo.cpp)
add_executable(ThreadTest ${SOURCE_FILES})
我是否包含标志 -pthread 与编译无关,它总是给出:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mjgalindo/ClionProjects/ThreadTest/cmake-build-debug
[ 33%] Building CXX object CMakeFiles/ThreadTest.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/ThreadTest.dir/foo.cpp.o
In file included from /usr/include/c++/6/thread:39:0,
from /home/mjgalindo/ClionProjects/ThreadTest/foo.cpp:3:
/usr/include/c++/6/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (Foo::*)(unsigned int, unsigned int, std::vector<unsigned int>&)>(Foo*, unsigned int, unsigned int, std::reference_wrapper<std::vector<int> >)>’:
/usr/include/c++/6/thread:137:26: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Foo::*)(unsigned int, unsigned int, std::vector<unsigned int>&); _Args = {Foo*, unsigned int&, unsigned int&, std::reference_wrapper<std::vector<int, std::allocator<int> > >}]’
/home/mjgalindo/ClionProjects/ThreadTest/foo.cpp:15:89: required from here
/usr/include/c++/6/functional:1374:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (Foo::*)(unsigned int, unsigned int, std::vector<unsigned int>&)>(Foo*, unsigned int, unsigned int, std::reference_wrapper<std::vector<int> >)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^~~~~~~~~~~
/usr/include/c++/6/functional:1395:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (Foo::*)(unsigned int, unsigned int, std::vector<unsigned int>&)>(Foo*, unsigned int, unsigned int, std::reference_wrapper<std::vector<int> >)>’
_M_invoke(_Index_tuple<_Indices...>)
^~~~~~~~~
CMakeFiles/ThreadTest.dir/build.make:86: recipe for target 'CMakeFiles/ThreadTest.dir/foo.cpp.o' failed
make[3]: *** [CMakeFiles/ThreadTest.dir/foo.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/ThreadTest.dir/all' failed
make[2]: *** [CMakeFiles/ThreadTest.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/ThreadTest.dir/rule' failed
make[1]: *** [CMakeFiles/ThreadTest.dir/rule] Error 2
Makefile:118: recipe for target 'ThreadTest' failed
make: *** [ThreadTest] Error 2
我见过很多类似这个的问题,但是他们都没有在线程构造函数中使用 this 参数或者没有使用 ref。我的猜测是我链接 pthread 错误,但这只是推测。
【问题讨论】:
-
腰带和吊带编程:代码创建一个大小为
threads的向量,运行一个从0开始的for循环,当索引为threads时退出,以防万一,使用at()检查索引是否在边界内。 -
@PeteBecker 你的意思是说 at() 在这种情况下是多余的?我一直使用 [] 直到我发现 at() 更安全。
-
是的,
at()是多余的。如果它添加了一些原本不存在的重要内容,它只会“更安全”。您的循环正确地写入向量中,并且检查索引是否有效不会向已经存在的内容添加任何内容。如果您正确编写循环,则很少需要额外检查。
标签: c++ multithreading methods pthreads