Loading…
|
Translation changed |
|
|
String added in the repository |
|
Loading…
|
Translation changed |
|
|
String added in the repository |
|
I/O objects such as sockets and streams [*are not thread-safe]. Although it is possible to have more than one operation outstanding (for example, a simultaneous asynchronous read and asynchronous write) the stream object itself may only be accessed from one thread at a time. This means that member functions such as move constructors, destructors, or initiating functions must not be called concurrently. Usually this is accomplished with synchronization primitives such as a [@https://en.cppreference.com/w/cpp/thread/mutex `mutex`], but concurrent network programs need a better way to access shared resources, since acquiring ownership of a mutex could block threads from performing uncontended work. For efficiency, networking adopts a model of using threads without explicit locking by requiring all access to I/O objects to be performed within a套接字和流等 I/O 对象 [*不是线程安全的]。尽管可以同时有多个操作未完成(例如,同时进行异步读和异步写),但流对象本身在同一时刻只能从一个线程进行访问。这意味着移动构造函数、析构函数或发起函数等成员函数不能并发调用。通常,这可以通过互斥锁等同步原语来实现,但并发网络程序需要更好的方式来访问共享资源,因为获取互斥锁的所有权可能会阻塞线程,使其无法执行无竞争的工作。为了提高效率,网络库采用了一种无需显式加锁的线程使用模型,要求所有对 I/O 对象的访问都必须在一个 [@boost:/doc/html/boost_asio/overview/core/strands.html ['strand]].串行器]] 内执行。