FreeBASIC マニュアルのトップに戻る

FreeBASIC ProPgEmulateTlsTp

目次→教本→プログラマーのための案内Emulate a TLS (Thread Local Storage) and a TP (Thread Pooling) feature←オリジナル・サイト

TLS(スレッドローカルストレージ)とTP(スレッドプーリング)機能をエミュレートする 左にメニュー・フレームが表示されていない場合は、ここをクリックして下さい

←リンク元に戻る プログラム開発関連に戻る

FreeBASICで、一種の TLS (Thread Local Storage) と一種の TP (Thread Pooling) feature機能をエミュレートする方法。

前文:

TLS (Thread Local Storage)
静的変数は通常、すべてのスレッド間で共有されます。静的変数を変更すると、その変数はすべてのスレッドに表示されるため、変更されたことがわかります。
TLS 静的変数を作成する場合、通常の静的変数とは異なり、すべてのスレッドは変数の独自のコピー (ただしアクセス名は同じ) を持つ必要があります。つまり、変数への変更はスレッドに対してローカルです (ローカルに保存されます)。
これにより、スレッドセーフな手続きを作成できます。なぜなら、この手続きを呼び出すたびに、宣言された同じ静的変数の独自のコピーを取得するためです。
静的変数を使用する通常の手続きでは、その変数の内容は複数のスレッドによって更新される可能性がありますが、TLS を使用すると、これらを各スレッドにとってローカルな静的データと考えることができます。

TLS データは静的データに似ていますが、唯一の違いは、TLS データが各スレッドに固有であることです。

TP (Thread Pooling)
スレッド プールは、ユーザーのニーズに基づいてタスクを実行するために使用できるスレッドの集合です。
スレッド プールには、Type 構造体を介してアクセスできます。

新しいスレッドの作成は、プロセッサ (CPU) の観点からもメモリの観点からも、リソースの点でコストのかかる行為です。
また、プログラムが多くのタスクの実行を必要とする場合、タスクごとにスレッドを作成・削除すると、アプリケーションのパフォーマンスが大幅に低下します。
したがって、タスクの実行を終了したスレッドを将来のタスクの実行に使えるように、スレッドの作成を共有できると便利です。

1. FreeBASIC で TLS(Thread Local Storage) 機能をエミュレートする方法
The principle of this TLS emulation for FreeBASIC is to use a static array for each requested TLS variable, where each thread has its own unique index (hidden) to access the array element.
This unique index relating to the thread is deduced from the thread handle value:
- With fbc version >= 1.08, the thread handle value is simply returned from the 'Threadself()' function calling (new function) from any thread.
- With fbc version < 1.08, the code is more twisted:
- The thread handle value is only accessible from the 'ThreadCreate()' return in the parent (or main) thread when creating it.
- There is no way to properly emulate the 'Threadself()' function, but only by a twisted method.
- In the example below (for fbc version < 1.08), a 'Threadself()' function (returning by reference) value is initialized before each use by the thread (with its own thread handle), and all of this (initialization + use) protected by a mutex as for its corresponding 'ThreadCreate()'.

In the below example, the TLS static variable is an integer which is used in a single and generic counting procedure ('counter()') with none passed parameter). This counting procedure is called by each thread (thus each thread counts independently of each other but by calling the same single counting procedure).
A single macro allows to define any TLS variable (except array) of any type.

2. FreeBASIC で TP(Thread Pooling)機能の一種をエミュレートする方法
The objective of thread pooling is to pool the threads in order to avoid untimely creation or deletion of threads, and thus allow their reuse.
So when a task needs to be executed, it will be more resource efficient to check if the thread pool contains an available thread.
If so, it will be used while the task is running, and then freed when the task is completed.
If there is no thread available, a new thread can be created, and at the end of the task, the thread would be in turn available in the pool of threads.

Two Type structures are first proposed below:
These two structures make it possible to use one thread per instance created, and to chain on this dedicated thread the execution of user procedures one after the other, but without the thread stopping between each:
- The 'ThreadInitThenMultiStart' structure requires a manual start after initialization (and manual wait for completion) for each user procedure to be executed in sequence in the thread.
- The 'ThreadPooling' structure allows to register a sequence of user thread procedure submissions in a queue, while at same time the user procedures start to be executed in the thread without waiting (a last registered wait command is enough to test for full sequence completion).
By creating and using several instances, these two structures make it possible to execute sequences of user procedures in several threads, therefore executed in parallel (temporally).

A last structure is finally proposed:
This last structure is an over-structure of the ThreadPooling structure, dispatching user thread procedures over a given max number of secondary threads.

These 3 different structures are then compared from the point of view:

参照
プログラマーのための案内に戻る
←リンク元に戻る プログラム開発関連に戻る
ページ歴史:2023-05-07 16:22:59
日本語翻訳:WATANABE Makoto、原文著作者:fxm

ホームページのトップに戻る

表示-非営利-継承