Contents Up Previous Next 原文へのリンク

マルチスレッドの概要 Multithreading overview

関連クラス:Classes: wxThread, wxMutex, wxCriticalSection, wxCondition

wxWidgetsはマルチスレッド(MT)プログラムに必用なカプセル化オブジェクトの完全なセット (threadクラスと,同期のためのmutexesconditionscritical sections) を提供します. wxWidgetsのスレッドAPIは,POSIX1.cスレッドAPI(pthreadsという名でも知られている)と似ています. ただし,幾つかの関数の名前が違い,Win32のスレッドAPIの(pthreadsより良い)機能をできるだけ実装しています.
wxWidgets provides a complete set of classes encapsulating objects necessary in multithreaded (MT) programs: the thread class itself and different synchronization objects: mutexes and critical sections with conditions. The thread API in wxWidgets resembles to POSIX1.c threads API (a.k.a. pthreads), although several functions have different names and some features inspired by Win32 thread API are there as well.

これらのクラスは,マルチスレッドプログラムを簡単に書けるように,そして幾つかの(Win32やPosixに比べて)追加 されたエラーチェックを提供するように作られています.
however it is ... projects.の部分の訳不能
マルチスレッドアプリケーションを作り始める(もしくは既存のアプリケーションにマルチスレッド機能を追加する)前に, その機能をスレッドを使うより簡単で安全な方法が無いか考えるべきです. もちろん幾つかの状況ではスレッドは本当に理に適っています(古典的な例では,サーバアプリケーションは クライアントの数だけのスレッドを用いる)が,それ以外ではとても愚かな選択です.(例:長い計算の途中で, プログレスダイアログの表示の為にスレッドを用いる) スレッド以外の実装手段も有効です.プログレスダイアログの例では,計算をidle handler の中で行うか,表示を更新するためにwxYield()を定期的に呼び出すといった方法が, スレッドを使うよりも遥かに良い手段です.
These classes will hopefully make writing MT programs easier and they also provide some extra error checking (compared to the native (be it Win32 or Posix) thread API), however it is still an non-trivial undertaking especially for large projects. Before starting an MT application (or starting to add MT features to an existing one) it is worth asking oneself if there is no easier and safer way to implement the same functionality. Of course, in some situations threads really make sense (classical example is a server application which launches a new thread for each new client), but in others it might be a very poor choice (example: launching a separate thread when doing a long computation to show a progress dialog). Other implementation choices are available: for the progress dialog example it is far better to do the calculations in the idle handler or call wxYield() periodically to update the screen.

もしアプリケーションの中でスレッドを用いることを決定したなら,複数のスレッドがGUI関数を呼び出さないように することが強く推奨されます. スレッドのサンプルでは沢山のスレッドがGUI関数を一度に呼び出すことが可能であることが示されていますが(サンプルで 作られた全てのスレッドがGUIにアクセスする),サンプルとして以外の目的ではとても愚かな設計方法です. 一つのGUIスレッドがあり複数のworkerスレッドがイベントを用いてメインスレッドとコミュニケートする設計の方が, ずっと健全で,数え切れない問題(例:Win32では,ペン,ブラシといったGDIオブジェクトは,生成したスレッド からしか用いることが出来ない)から開発者を守ります.
&cって何? by 思兼
If you do decide to use threads in your application, it is strongly recommended that no more than one thread calls GUI functions. The thread sample shows that it is possible for many different threads to call GUI functions at once (all the threads created in the sample access GUI), but it is a very poor design choice for anything except an example. The design which uses one GUI thread and several worker threads which communicate with the main one using events is much more robust and will undoubtedly save you countless problems (example: under Win32 a thread can only access GDI objects such as pens, brushes, &c created by itself and not by the other threads).

スレッド間通信の為に,wxEvtHandler::AddPendingEventか, そのshort versionのwxPostEventを用いて下さい. これらの関数はスレッドセーフな実装がされていて,あるスレッドから別のスレッドにイベントを送るのに使えます.
For communication between threads, use wxEvtHandler::AddPendingEvent or its short version wxPostEvent. These functions have thread safe implementation so that they can be used as they are for sending event from one thread to another.