.. Multithreading ============== マルチスレッド ============== .. When multithreading is enabled (it can be switched on and off in the preferences dialog or the Edit menu), you can create diagrams that model arbitarily many sequences running in parallel, not just a single one. The sequences can (at your option) be distinguished by the colours of their corresponding lifelines. There is still no real parallelism, as the messages must be specified in some order. This order can be interpreted as one of many possible interleavings of the threads. A single-processor system would execute them in a similar way. マルチスレッドが有効になっていると、任意の数のシーケンスが並列で実行されるようなモデルを表現した図を作成することができるようになります。この機能は、設定ダイアログや、編集メニューの中でON/OFFを切り替えることができます。オプションによっては、それぞれのスレッドが関連するライフラインはそれぞれ違う色で描かれ、識別しやすいようになっています。本当の意味での並列化というものは存在していなく、メッセージは特定の順序で実行されます。この順序は、多くのスレッドのスイッチで、とりうる方法のひとつであると解釈できます。シングルプロセッサのシステムでは、似たような方法でこれらを実行します。 .. Specifying diagrams with multiple threads マルチスレッドを使った図の仕様作成 ================================== .. Specifying a multithreaded diagram is only slightly harder than specifying a diagram with a single thread. マルチスレッドの図を作成するのは、シングルスレッドの図を作成するのにくらべて、多少難しい程度です。 .. Spawning threads スレッドの生成 ---------------- .. A newly spawned thread can be distinguished from older threads by the colour of its corresponding lifelines. If you spawn too many threads, colours may be repeated. Threads have successive numbers, starting with 0. 新しくスレッドを生成すると、古いスレッドとは、関連するライフラインの色が異なるため、区別することができます。数多くのスレッドを生成したい場合には、色が途中からリピートされます。スレッドは0から始まる、連続する数値を属性にもっています。 .. Statically spawned threads 静的なスレッドの生成 ~~~~~~~~~~~~~~~~~~~~ .. Objects that have been declared with the "t" flag set, for example ``"t"``\ フラグセットをが宣言時にしていされたオブジェクトはスレッドになります。例えば:: o:Object[t] .. have their own (statically spawned) thread. The first thing that happens on this thread must be a message sent by the corresponding object. このスクリプトは、静的に生成されたスレッドを持つようになります。このスレッドに関して、最初に行われるのは、関連するオブジェクトからのメッセージを受信になるでしょう。 .. If there is no object with a "t" flag, there is a single thread nonetheless. It starts executing when the first message is sent by an object. ``"t"``\ フラグセットがついたオブジェクトが一切なかった場合にも、シングルスレッドがひとつだけ準備されます。このオブジェクトは、他のオブジェクトから最初のメッセージを受け取ると、起動開始になります。 .. Dynamically spawned threads 動的なスレッドの生成 ~~~~~~~~~~~~~~~~~~~~ .. As a rule, a message sent by an actor to an object (dynamically) spawns a new thread. ルール上は、アクターからオブジェクトに対して送られるメッセージに対しては、新しいスレッドが生成されることになっています。 .. So do messages sent to "active objects". An object can be declared to be active by setting the v flag. Example そのため、メッセージは"アクティブオブジェクト"に対して送信されます。オブジェクトをアクティブであると宣言するには、以下のように\ ``"v"``\ フラグを使用することができます:: worker:BackgroundWorker[v] .. Messages sent and received by ordinary objects may also spawn new threads. In order to specify that such a message is spawning, use a colon followed by a ">" to separate the caller from the message. Example メッセージを送信し、通常のオブジェクトが受信したときに新しいスレッドを生成する、ということもあるでしょう。このようなスレッドを生成するメッセージを指定するには、呼び出し元とメッセージの間の区切り文字のコロン(:)の前に\ ``">"``\ を追加します:: object:>bar.foo() .. This means that object spawns a new thread and the first thing this new thread does is executing the foo method of the bar object. このスクリプトの意味は、objectオブジェクトが新しいスレッドを作成することをあらわします。そして、そのスレッドが生成されて最初に、barオブジェクトのfooメソッドの実行するという意味になります。 .. Note that you cannot specify answers to messages that spawn threads. If an object running on a new thread wants to communicate with the spawning object, it must explicitly send a message. .. note:: このようなスレッド生成メッセージを使うと、返り値を定義することができません。もしも新しいスレッド上で動作しているオブジェクトと、生成を行ったオブジェクトの間でコミュニケーションさせたい場合には、明示的なメッセージ送信を使用してください。 .. Broadcast messages メッセージのブロードキャスト ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. A message can have more than one callee, specified in set notation. In this case, it is a broadcast message. Example セット表記を利用すると、ひとつ以上の呼び出し先オブジェクトにメッセージを送ることができます。この場合、これをブロードキャストメッセージと呼びます:: caller:{callee1,callee2,callee3}.broadcast .. A callee may not occur more than once in the callee set, the caller may not occur at all in there. The effect of a broadcast message is that a new thread is spawned for each callee. 呼び出し先は、このセットの中に重複して登場してはいけません。また、呼び出し元もセットの中に入れてはいけません。このブロードキャストメッセージを利用すると、それぞれの呼び出し先ごとにスレッドが生成されます。 .. It is currently not possible to define mnemonics for objects that are activated by a broadcast message. 現在のバージョンでは、ブロードキャストメッセージによってアクティブ化されるオブジェクトに対して、ニーモニックを定義することはできません。 .. Specifying the thread where an activity occurs 活動開始時のスレッドの指定 ---------------------------------------------- .. When more than one thread is used, we need to be able to tell on which of the threads a message occurs. So the level of a caller (specified in square brackets) may be followed by the number of the thread, where level and thread are separated by a comma. Example: ひとつ以上のスレッドを使用している場合に、どのスレッド上でメッセージを送っているのか、というのが指定できなければなりません。そのため、角括弧で指定する、呼び出し元オブジェクトのレベル指定の後ろに、スレッドの番号を設定することができます。レベルと、スレッドの番号は、以下のサンプルのようにカンマ(,)で区切ります:: object[0,3]:bar.foo() .. This means that object is sending a foo() message to bar on the thread number 3. このスクリプトの意味は、この\ ``object``\ オブジェクトが\ ``foo()``\ メッセージを三番目のスレッドを使用して\ ``bar``\ オブジェクトに送っている、となります。 .. If an object is used by a single thread only, the number of the thread can be omitted. If there is more than one thread using an object and the thread number is omitted, the editor assumes that the message is to be sent on the same thread as the most recent message. If you spawn a new thread, the next message will, if not otherwise specified, be sent on that new thread. It is also possible to omit the level and only specify the thread, so ``object[,3]:bar.foo()`` is equivalent to the example above, as 0 is the default level. もしもオブジェクトがシングルスレッドからしか使用されない場合には、スレッドの番号は省略することができます。もしもオブジェクトを操作するスレッドがひとつ以上あり、スレッド番号が省略されている場合には、エディタは、メッセージの送信に使われるスレッドは、一番最後にメッセージを送ったのと同じスレッドであるとみなします。新しいスレッドが生成された場合に、他のスレッドが指定されなければ、新しいスレッドを通じて送信されたものとみなします。上記のようなスレッドの番号を特定してメッセージを送りたい場合には、レベルを省略して\ ``object[,3]:bar.foo()``\ と書くこともできます。この場合、ゼロというのがデフォルトのレベルを示すので、この書き方と上記の書き方は同じ意味を表します。 .. You can also use a mnemonic between square brackets. When a mnemonic is defined for an object, the mnemonic determines both the level and the thread number. 角括弧の中には数値以外にも、ニーモニックを私用することができます。オブジェクトに対してニーモニックを定義すると、レベル、スレッド番号の両方の決定に使用します。 .. Thread numbers can be made visible at your option (see the "Threads" menu): They are shown at the top of the active lifelines. オプションの"Threads"の項目にある"Show thread numbers"にチェックを入れると、スレッド番号が見えるようになります。アクティブなライフラインの最上段に表示されるようになります。 .. Instant return その場でのリターン ------------------ .. Normally, the focus of a thread's activity stays on the object that has received the most recent message, unless the thread is stopped or another message is sent. Sometimes, however, it may seem inappropriate to have a thread's focus resting on an object for so long. In that case a message should be ended by an "&". This means that the answer to the message will be sent immediately. Note that a message sent later cannot have the callee of the instantly returning message as a caller of course. Example: 通常、スレッドの実行ポイントは、スレッドが停止されたり、別のメッセージが送信されない限りは、最新のメッセージを受信したオブジェクト上にいます。しかし、スレッドの実行ポイントが長期間とあるオブジェクトの上に滞留し続けるのが適当ではない場合もあるでしょう。その場合には\ ``"&"``\ を付加すると、即座にメッセージに対する返答が返ってくるということを表現することができます。ただし、その場でのリターンのメッセージを受け取ったオブジェクトは、後で送信するメッセージで送信者として指定することはできません:: foo:bar.notify()& .. If a spawning message is suffixed with an "&", the spawned thread does nothing and dies instantly. Such messages might be interpreted as asynchronous signals. もしも、スレッド生成メッセージの後ろに\ ``"&"``\ が付くと、生成されたスレッドはなにもしないで、その場で破棄されます。このようなメッセージは非同期のシグナルとして使用されます。 .. Stopping threads スレッドの停止 ---------------- .. Normally, a thread runs until the end of the diagram. But maybe you would like to model that it dies at some point. This is done by inserting a pseudo message of the form: 通常のケースでは、スレッドは図の最後まで生き残りますが、場合によっては、それ以外の場所でスレッドが停止されるようなモデルを作成する必要があることもあるでしょう。この場合には、以下の形式の擬似メッセージをスクリプトの中に挿入します:: object:stop .. (where of course also a thread number may be specified). Stopping a thread may be necessary before an object can be destroyed, if the thread's activity does not return from the object otherwise. The text of the pseudo message (i. e. "stop") will not appear on the diagram. The visible effect rather is that the corresponding lifeline ends at that point. スレッドの番号を指定することもできます。もし、スレッドの活動が他のオブジェクトから戻ってくる前には、オブジェクトが破棄できる前に、スレッドを停止する必要があるかもしれません。擬似メッセージのテキスト(``"stop"``)は図の中には表示されません。目に見える効果としては、スレッドが終了するタイミングでライフラインが終了するというものがあります。 .. Returning control flow 制御フローを返す ------------------------ .. Before an object sends a message, a thread's control flow has to return to it, which implies that some answers pending on the thread might have to be sent. Sometimes one wants to manipulate the control flow like this without the need to send a new message, e. g. for representing that a certain set of actions is finished now or for simply doing some "clean-up". This can be done by inserting a pseudo message consisting of just an underscore: オブジェクトがメッセージを送る前にスレッドの制御フローを返すということは、スレッドが返すべき返事が保留されているということを意味します。場合によっては、新しいメッセージを送らないで、とある種類の実行が終了したということや、なんらかの"クリーンアップ"を実行した、ということを表現したいというケースがあるでしょう。この場合には、1文字のアンダースコア(_)で構成される擬似メッセージを使用します:: object:_ .. The effect is that enough pending answers will be sent such that "object" could send a message now on its current thread (again, a special thread number may be specified). The pseudo message does not appear on the diagram and there will be no extra space consumed (apart from the space needed for the answers). これを指定すると、"オブジェクト"がメッセージを送信できるようになるまで、返事を待つことができるようになります。これについても、スレッド番号を指定することができます。擬似メッセージは図の中には表示されません。答えを返すのに必要なスペースを除いて、余計なスペースが含まれなくなります。