Using multithreading without an explicit lock

Source: Internet
Author: User
A string is defined as a sequence of call event handles (non-parallel calls) that use strings to allow code execution in a multithreaded environment without using the displayed mutex. The string can be implicit or explicit, as shown in the alternative method below: Calling Io_service::run () in only one thread means that all event handles are executed using an implicit string because io_service ensures that the handle is only called internally by Run (). When there is an asynchronous operation chain associated with only one connection (for example, half-duplex protocol HTTP), it is not possible to execute the handle concurrently, which is an implicit string. An explicit string call is an instance of Io_service::strand, and all event handle functions need to be wrapped with io_service::strand::wrap () or advertised and sent through Io_service::strand objects. In the case of an asynchronous operation, such as Async_read () or Async_read_until (), if a completion handle passes through a strand, all other intermediate handles also need to pass through the same string, which is to ensure the thread safety of the objects that are shared between the caller and the completion operation ( In the case of Async_read () a socket, the caller can close the operation by close, which is done by embedding the hook function with the intermediate object that points to the custom handle associated with the final handle. struct my_handler{void operator () () {...}};  Template<class f>void Asio_handler_invoke (f F, my_handler*) {//do-custom invocation here.  Default implementation calls F ();} Io_service::strand::wrap () Creates a new completion handle that defines the asio_handler_invoke so that the function object is executed through the Strand. The string of English is strand.

Using multithreading without an explicit lock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.