即时做一个C++类在内部,我想创建一个线程来执行类的功能,我该怎么做?我给你看一些代码。如何启动一个运行类函数的boost线程?
成员函数我想在一个线程中运行:
void SocketServer::runServer(){
bool connected;
tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), puerto));
std::string data;
while(seguirFuncionando()){
miSocket = new tcp::socket(io_service);
std::cout << "Waiting for connection...\n";
a.accept(*miSocket);
std::cout << "Connected\n";
connected = true;
try{
while (connected){
data = readStr();
if (data.compare("")==0){
std::cout << "End of connection.\n";
}
else{
std::cout << data << "\n";
}
}
}
catch (std::exception& e){
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
}
什么我尝试做:
void SocketServer::runThreadServer(){
asio::thread t(runServer);
}
但它不编译。错误:
没有匹配函数调用'asio :: thread :: thread() 注意:候选是:asio :: thread :: thread(Function)[with Function = void(SocketServer :: *)( )]
我该怎么做?
Thanx。
你可能想尝试用[boost.bind(http://www.boost.org/doc/libs/1_48_0/libs/bind/bind.html) – 2012-02-10 17:27:43
@JoachimPileborg你能举个例子吗?即时通讯新的提振,而我有点失落:P – Alex 2012-02-10 17:29:25