我不明白何时应该使用std::move,何时应该让编译器优化...例如:
using SerialBuffer = vector< unsigned char >;
// let compiler optimize it
SerialBuffer read( size_t size ) const
{
SerialBuffer buffer( size );
read( begin( buffer ), end( buffer ) );
// Return Value Optimization
return buffer;
}
// explicit move
SerialBuffer read( size_t size ) const
{
SerialBuffer buffer( size );
read( begin( buffer ), end( buffer ) );
return move( buffer );
}
Run Code Online (Sandbox Code Playgroud)
我应该使用哪个?
可能重复:
使用成员函数启动线程
我有一个小班:
class Test
{
public:
void runMultiThread();
private:
int calculate(int from, int to);
}
Run Code Online (Sandbox Code Playgroud)
如何在方法的两个线程中calculate使用两个不同的参数集(例如calculate(0,10),calculate(11,20))运行方法runMultiThread()?
PS谢谢我忘记了我需要传递this,作为参数.
我的程序如下所示
#include <iostream>
#include <thread>
class A {
public:
void foo(int n ) { std::cout << n << std::endl; }
};
int main()
{
A a;
std::thread t1(&A::foo, std::ref(a), 100);
t1.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我使用以下命令编译它时,我得到错误
g++ -o main main.cc -lpthread -std=c++11
Run Code Online (Sandbox Code Playgroud)
错误:
In file included from /usr/local/include/c++/4.8.2/thread:39:0,
from check.cc:2:
/usr/local/include/c++/4.8.2/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (A::*)(int)>(std::reference_wrapper<A>, int)>’:
/usr/local/include/c++/4.8.2/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (A::*)(int); _Args = {std::reference_wrapper<A>, int}]’
check.cc:13:42: required from here
/usr/local/include/c++/4.8.2/functional:1697:61: error?no type named …Run Code Online (Sandbox Code Playgroud) 可能重复:
使用成员函数启动线程
我最近一直在使用c ++ 11中的新std :: thread库,我遇到了一个问题.当我尝试将一个类函数传递给一个新线程时,它给了我一个错误(我现在没有确切的错误文本,因为我远离家乡)我有一个像这样的类
class A
{
void FunctA();
void FunctB();
void run()
{
std::thread t(FunctA);
std::thread r(FunctB);
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我刚刚开始使用C++ 11线程,我一直在努力(可能是愚蠢的)错误.这是我的示例程序:
#include <iostream>
#include <thread>
#include <future>
using namespace std;
class A {
public:
A() {
cout << "A constructor\n";
}
void foo() {
cout << "I'm foo() and I greet you.\n";
}
static void foo2() {
cout << "I'm foo2() and I am static!\n";
}
void operator()() {
cout << "I'm the operator(). Hi there!\n";
}
};
void hello1() {
cout << "Hello from outside class A\n";
}
int main() {
A obj;
thread t1(hello1); // it works …Run Code Online (Sandbox Code Playgroud) 对于需要使用线程跨越的函数,是否可能存在重载?
我有一个名为Complex的简单类.
class Complex
{
public:
Complex():realPart_(0), imagPart_(0){}
Complex(double rp, double ip) : realPart_(rp), imagPart_(ip) {}
double & real() { return realPart_;}
double & imag() { return imagPart_;}
const double & real() const { return realPart_;}
const double & imag() const { return imagPart_;}
double square() const {return realPart_*realPart_ - imagPart_*imagPart_;}
void display() const
{
std::cout << "Square of the Complex number (" << realPart_ << ") + i (" << imagPart_ << " ) is " << square() << …Run Code Online (Sandbox Code Playgroud) 我有一个类播放器,一些子类Player1,Player2,Player3使用C++扩展播放器.
Class Player有一个方法"run",所有Player1,2,3都会覆盖"run"来做不同的事情.
class Player {
public:
virtual void run();
}
class Player1: public Player {
public:
void run();
}
Run Code Online (Sandbox Code Playgroud)
在"main"函数中,我将创建一些Player1,2,3的实例
和一些C++ 11线程调用方法"run"这些实例.
int main() {
Player1 player1;
Player2 player2;
Player3 player3;
Thread thread1(player1.run, this);
Thread thread2(player2.run, this);
Thread thread3(player3.run, this);
thread1.join();
thread2.join();
thread3.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试过,我知道它不起作用,
所以我尝试使用另一个函数来调用实例方法.
function doRun1(Player1 player){
player.run();
}
int main() {
Player1 player1;
Player2 player2;
Player3 player3;
Thread thread1(doRun1, player1);
Thread thread2(doRun2, player2);
Thread thread3(doRun3, player3);
thread1.join();
thread2.join();
thread3.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这种方式似乎解决了问题,但我必须创建doRun1,doRun2,doRun3 ....很多函数, …
可能重复:
使用成员函数启动线程
我对C++很陌生.我的经验主要是使用javascript和java.
我在Lion上使用Xcode.下面的代码给了我一个编译错误"必须调用对非静态成员函数的引用;你的意思是没有参数调用它吗?"
class MyClass {
private:
void handler() {
}
public:
void handleThings() {
std::thread myThread(handler);
}
};
Run Code Online (Sandbox Code Playgroud)
我也试过this->handler,&handler和其他变化,但没有一个工作.此代码编译并完成我想要的内容:
class MyClass {
private:
void handler() {
}
public:
void handleThings() {
std::thread myThread([this]() {
handler();
});
}
};
Run Code Online (Sandbox Code Playgroud)
为什么我不能传递对成员函数的引用?我的解决方案是最好的解决方案吗?
我正在尝试编写一个命令行服务器,它将从串行端口接收信息,解析它,并将其记录在内部对象中.
然后,根据客户端的请求,服务器将返回所请求的信息.
我想要做的是将接收器和解析器部件放在一个单独的线程中,以使服务器一起运行,而不是干扰数据收集.
#include <iostream>
#include <thread>
class exampleClass{
std::thread *processThread;
public void completeProcess(){
while(1){
processStep1();
if (verification()){processStep2()}
}
};
void processStep1(){...};
void processStep2(){...};
bool verification(){...};
void runThreaded();
} // End example class definition
// The idea being that this thread runs independently
// until I call the object's destructor
exampleClass::runThreaded(){
std::thread processThread(&exampleClass::completeProcess, this);
} // Unfortunately The program ends up crashing here with CIGARET
Run Code Online (Sandbox Code Playgroud) 我遇到了问题,std::thread因为它不接受采用自动指定参数的函数.以下是一些示例代码:
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
void seev(const auto &v) // works fine with const vector<int> &v
{
for (auto x : v)
cout << x << ' ';
cout << "\n\n";
}
int main()
{
vector<int> v1 { 1, 2, 3, 4, 5 };
thread t(seev, v1);
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是编译器说:
[Error] no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, std::vector<int>&)'
Run Code Online (Sandbox Code Playgroud)
为什么会这样?这是语言或GCC(4.9.2)的问题吗?