这是我的发送线程,在它第一次被调用完成执行后。我无法恢复此发件人线程。 C++中有恢复线程的机制吗?
void ClientSocket::sender()
{
char buf[1024];
//readBuffer = m_ptrsendStream->Read_Adt(filePath);
//readStream();
//cout << readBuffer.str()<<endl;
cout << "write stream to send through socket\n" << endl;
cin >> buf;
if (isConnected == 0)
{
//send(clientSock, readBuffer.str().c_str(), strlen((char *)readBuffer.str().c_str()), 0);
send(clientSock, buf, strlen(buf), 0);
cout << "sending stream :\n"<<endl << buf << endl;
}
}
//this is where my thread creation happens and join() happens.
int main(int argc, char *argv[])
{
ClientSocket objSocket(argv[1]);
sender_thread = make_shared<thread>([&objSocket]() {
objSocket.sender();
});
try
{
if (sender_thread->joinable())
sender_thread->join();
}
最佳答案
不,一旦你的线程加入就完成了,你需要创建一个新线程。
如果您有这种不断创建新线程的模式,那么考虑使用线程池来避免不断产生新线程的开销可能是值得的。
此外,如果这与网络有关,最好避免使用线程,而是使用像 boost::asio 这样的异步方式。 .
关于c++ - 调用 join() 后如何恢复线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30616049/