我理解为了避免输出混合,必须同步多个线程对cout和cerr的访问.在同时使用cout和cerr的程序中,单独锁定它们是否足够?或者同时写cout和cerr仍然不安全?
编辑说明:我知道cout和cerr在C++ 11中是"线程安全的".我的问题是,对cout的写入和对不同线程的cerr的写入是否会以两次写入cout的方式相互干扰(导致交错输入等).
当与libstdc ++和libc ++链接时,以下(玩具)程序返回不同的东西.这是libc ++中的错误还是我不明白istream eof()是如何工作的?我尝试在linux和mac os x上使用g ++并在mac os x上运行它,使用和不使用-std = c ++ 0x.我的印象是,在尝试读取(通过get()或其他东西)实际上失败之前,eof()不会返回true.这就是libstdc ++的行为方式,而不是libc ++的行为方式.
#include <iostream>
#include <sstream>
int main() {
std::stringstream s;
s << "a";
std::cout << "EOF? " << (s.eof() ? "T" : "F") << std::endl;
std::cout << "get: " << s.get() << std::endl;
std::cout << "EOF? " << (s.eof() ? "T" : "F") << std::endl;
return 0;
}
Thor:~$ g++ test.cpp
Thor:~$ ./a.out
EOF? F
get: 97
EOF? F
Thor:~$ clang++ -std=c++0x -stdlib=libstdc++ test.cpp
Thor:~$ …Run Code Online (Sandbox Code Playgroud) 我知道异步 SwiftTask不应该阻塞(异步工作线程必须始终向前推进)。如果我有一个 100% 异步 Swift 应用程序,但需要引入一些阻塞任务,那么正确的方法是什么,不会阻塞任何 swift 异步线程池工作线程?
我假设需要异步线程池之外的新专用线程,如果该假设正确,异步函数等待该线程完成的线程安全方法是什么?我可以使用 的主体withCheckedContinuation来启动线程,将continuation句柄复制到该线程中并continuation.resume在线程完成时从该线程调用吗?
我正在调试一些使用websocketpp的外部代码。尽管一切正常,但我一直在获得相当详细的控制台输出。我可以禁用输出还是至少指向特定的东西?BR,丹尼尔
[2014-07-08 14:51:27] [fatal] error in handle_read_handshake: End of File
[2014-07-08 14:51:27] [fatal] error in handle_read_frame: End of File (websocketpp.transport:7)
[2014-07-08 14:51:27] [info] asio async_shutdown error: system:10054 (An existing connection was forcibly closed by the remote host)
[2014-07-08 14:51:27] [error] Underlying Transport Error
[2014-07-08 14:51:27] [fatal] error in handle_read_frame: End of File (websocketpp.transport:7)
[2014-07-08 14:51:27] [info] asio async_write error: system:10053 (An established connection was aborted by the software in your host machine)
[2014-07-08 14:51:27] [fatal] error in handle_write_frame: Underlying TransportError …Run Code Online (Sandbox Code Playgroud) 我使用优秀的websocketpp库在C++应用程序中提供Websockets(和HTTP)服务器.我还需要在同一个应用程序中的HTTP客户端连接到REST API.我也一直在websocketpp尝试这个,但到目前为止我没有成功.以下初步尝试为我提供了这个日志输出:
[2015-03-06 18:01:18] [connect] Successful connection
[2015-03-06 18:01:18] [error] Server handshake response error: websocketpp.processor:20 (Invalid HTTP status.)
[2015-03-06 18:01:18] [disconnect] Failed: Invalid HTTP status.
Run Code Online (Sandbox Code Playgroud)
这表明我的http_处理程序方法可能需要更多.任何意见,将不胜感激.websocketpp文档和示例似乎不包含简单的HTTP客户端.
#define _WEBSOCKETPP_CPP11_STL_
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>
namespace {
using namespace websocketpp;
typedef client<websocketpp::config::asio_client> client;
class Client {
public:
Client(void){
client_.init_asio();
client_.set_http_handler(bind(&Client::http_,this,_1));
}
std::string get(const std::string& url) {
websocketpp::lib::error_code error;
client::connection_ptr con = client_.get_connection(url,error);
if(error) std::runtime_error("Unable to connnect.\n url: "+url+"\n message: "+error.message());
client_.connect(con);
websocketpp::lib::thread asio_thread(&client::run, &client_);
asio_thread.join();
return data_; …Run Code Online (Sandbox Code Playgroud) 我无法通过名为websocketpp的 WebSockets C++库从MtGox API获取信息:
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
typedef websocketpp::client<websocketpp::config::asio_client> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
void on_open(websocketpp::connection_hdl hdl)
{
std::cout << "on_open \n";
}
void on_close(websocketpp::connection_hdl hdl)
{
std::cout << "on_close \n";
}
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg)
{
std::cout << msg->get_payload() << '\n';
}
int main()
{
client c;
try
{
c.init_asio();
c.set_open_handler(on_open);
c.set_close_handler(on_close);
c.set_message_handler(bind(&on_message, &c, ::_1, ::_2));
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection("ws://websocket.mtgox.com:80/mtgox?Currency=EUR", ec);
c.connect(con);
c.run();
}
catch …Run Code Online (Sandbox Code Playgroud) c++ ×5
websocket ×3
websocket++ ×3
async-await ×1
cout ×1
istream ×1
libc++ ×1
libstdc++ ×1
swift ×1