検索

このコンテンツは選択した言語では利用できません。

Chapter 7. Multithreading

download PDF

AMQ C++ supports full multithreading with C++11 and later. Limited multithreading is possible with older versions of C++. See Section 7.5, “Using older versions of C++”.

7.1. Threading model

The container object can handle multiple connections concurrently. As AMQP events occur on connections, the container calls messaging_handler callback functions. Callbacks for any one connection are serialized (not called concurrently), but callbacks for different connections can be safely executed in parallel.

You can assign a handler to a connection in container::connect() or listen_handler::on_accept() using the handler connection option. We recommend creating a separate handler for each connection. That way the handler does not need locks or other synchronization to protect it against concurrent use by library threads. If any non-library threads use the handler concurrently, then you will need synchronization.

7.2. Thread-safety rules

The connection, session, sender, receiver, tracker, and delivery objects are not thread-safe and are subject to the following rules.

  1. You must use them only from a messaging_handler callback or a work_queue function.
  2. You must not use objects belonging to one connection from a callback for another connection.
  3. You can store AMQ C++ objects in member variables for use in a later callback, provided you respect rule two.

The message object is a value type with the same threading constraints as a standard C++ built-in type. It cannot be concurrently modified.

7.3. Work queues

The work_queue interface provides a safe way to communicate between different connection handlers or between non-library threads and connection handlers.

  • Each connection has an associated work_queue.
  • The work queue is thread-safe (C++11 or greater). Any thread can add work.
  • A work item is a std::function, and bound arguments are called like an event callback.

When the library calls the work function, it will be serialized safely so that you can treat the work function like an event callback and safely access the handler and AMQ C++ objects stored on it.

7.4. The wake primitive

The connection::wake() method allows any thread to prompt activity on a connection by triggering an on_connection_wake() callback. This is the only thread-safe method on connection.

wake() is a lightweight, low-level primitive for signaling between threads.

  • It does not carry any code or data, unlike work_queue.
  • Multiple calls to wake() might be coalesced into a single on_connection_wake().
  • Calls to on_connection_wake() can occur without any application call to wake() since the library uses wake() internally.

The semantics of wake() are similar to std::condition_variable::notify_one(). There will be a wakeup, but there must be some shared application state to determine why the wakeup occurred and what, if anything, to do about it.

Work queues are easier to use in many instances, but wake() may be useful if you already have your own external thread-safe queues and need an efficient way to wake a connection to check them for data.

7.5. Using older versions of C++

Before C++11 there was no standard support for threading in C++. You can use AMQ C++ with threads but with the following limitations.

  • The container will not create threads. It will only use the single thread that calls container::run().
  • None of the AMQ C++ library classes are thread-safe, including container and work_queue. You need an external lock to use container in multiple threads. The only exception is connection::wake(). It is thread-safe even in older C++.

The container::schedule() and work_queue APIs accept C++11 lambda functions to define units of work. If you are using a version of C++ that does not support lambdas, you must use the make_work() function instead.

Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.