std::future.wait_for 永远阻塞
std::future.wait_for blocking forever
我正在创建 SSL 套接字,当我尝试连接到该套接字时,我的程序挂起。
我发现一些问题,服务器不发送任何数据,因此 SSL 连接正在等待任何响应。
我决定创建 future 并在超时后将其杀死。但它仍然挂着。
如果你看到代码,下面的部分没有执行:cout<<"TEST";
{
std::future<void> future = std::async(std::launch::async,
[&](){
err = SSL_connect (m_ssl);
});
std::future_status status;
status = future.wait_for(std::chrono::seconds(t_timeout));
}
cout<<"TEST";
为了模拟来自服务器的停止响应,我只是 运行:
for item in $(seq 1 1000); do sudo nc -i 100000ms -l 443 >> log.out; done;
如何终止 SSL 连接?
kill future后想继续执行代码
编辑:
我不想为此提出另一个问题。
回答:所以我现在确定这是因为未来的析构函数。它正在等待未来主体的代码完成。
问题二:
我该如何解决上述问题?我想在 future 所在的范围之后执行代码。
是否可以创建线程并等待直到超时或 ssl 某些互斥体被解锁?
您的代码的问题在于,当 future 超出范围时,其析构函数可能会阻塞,如 documentation for std::future<T>::~future
中所报告:
它明确表示:
... will not block for the shared state to become ready, except that it may block if all of the following are true: the shared state was created by a call to std::async, the shared state is not yet ready, and this was the last reference to the shared state.
你的情况:
- 共享状态尚未就绪:SSL 永远挂起
- 这是对共享状态的唯一(也是最后一个)引用。
您的问题的解决方案是您必须使变量 std::future<void> future
的范围超出您不希望被阻止的代码部分。
检查这些答案:
- main thread waits for std::async to complete
- Can I use std::async without waiting for the future limitation?
我正在创建 SSL 套接字,当我尝试连接到该套接字时,我的程序挂起。
我发现一些问题,服务器不发送任何数据,因此 SSL 连接正在等待任何响应。
我决定创建 future 并在超时后将其杀死。但它仍然挂着。
如果你看到代码,下面的部分没有执行:cout<<"TEST";
{
std::future<void> future = std::async(std::launch::async,
[&](){
err = SSL_connect (m_ssl);
});
std::future_status status;
status = future.wait_for(std::chrono::seconds(t_timeout));
}
cout<<"TEST";
为了模拟来自服务器的停止响应,我只是 运行:
for item in $(seq 1 1000); do sudo nc -i 100000ms -l 443 >> log.out; done;
如何终止 SSL 连接? kill future后想继续执行代码
编辑: 我不想为此提出另一个问题。 回答:所以我现在确定这是因为未来的析构函数。它正在等待未来主体的代码完成。
问题二: 我该如何解决上述问题?我想在 future 所在的范围之后执行代码。
是否可以创建线程并等待直到超时或 ssl 某些互斥体被解锁?
您的代码的问题在于,当 future 超出范围时,其析构函数可能会阻塞,如 documentation for std::future<T>::~future
中所报告:
它明确表示:
... will not block for the shared state to become ready, except that it may block if all of the following are true: the shared state was created by a call to std::async, the shared state is not yet ready, and this was the last reference to the shared state.
你的情况:
- 共享状态尚未就绪:SSL 永远挂起
- 这是对共享状态的唯一(也是最后一个)引用。
您的问题的解决方案是您必须使变量 std::future<void> future
的范围超出您不希望被阻止的代码部分。
检查这些答案:
- main thread waits for std::async to complete
- Can I use std::async without waiting for the future limitation?