| 日本語版メニュー | |
| メインページ - Lazarus Documentation日本語版 - 翻訳ノート - æ—¥æœ¬èªžéšœå®³æƒ…å ± | |
概�
��ページ�� Free Pascal � Lazarus を使��マル�スレッドアプリケーションを書��りデ�ッグ��り�る方法����述���。
ã‚‚ã�—ã€�èªè€…ã�Œãƒžãƒ«ãƒ�スレッディングã�«åˆ�ã‚�ã�¦è§¦ã‚Œã‚‹ã�®ã�ªã‚‰ã€�是é�žãƒžãƒ«ãƒ�スレッディングã�Œæœ¬å½“ã�«å¿…è¦�ã�‹ã�©ã�†ã�‹ã‚’「マルãƒ�スレッディングã�Œå¿…è¦�ã�§ã�™ã�‹ã€�節ã�§ã�Šç¢ºã�‹ã‚�ã��ã� ã�•ã�„。ã��ã�†ã�™ã‚Œã�°é ç—›ã�®ç¨®ã‚’激減ã�™ã‚‹ã�“ã�¨ã�Œã�§ã��ã‚‹ã�§ã�—ょã�†ã€‚
マルãƒ�スレッドアプリケーションã�¯ã€�2ã�¤ã�‹ã��れ以上ã�®ã‚¹ãƒ¬ãƒƒãƒ‰ã‚’作æˆ�ã€�å�Œæ™‚実行ã�—ã�¦ä½œæ¥ã‚’行ã�†ã‚‚ã�®ã�§ã�™ã€‚ã��ã�®ã�†ã�¡ä¸€ã�¤ã�¯ã€�メインスレッドã�¨å‘¼ã�°ã‚Œã‚‹ã‚‚ã�®ã�§ã�™ã€‚メインスレッドã�¯ã‚¢ãƒ—リケーションã�Œèµ·å‹•ã�™ã‚‹éš›ã€�オペレーティングシステムã�«ã‚ˆã�£ã�¦ç”Ÿæˆ�ã�•れã�¾ã�™ã€‚メインスレッドã�¯ãƒ¦ãƒ¼ã‚¶ãƒ¼ã‚¤ãƒ³ã‚¿ãƒ¼ãƒ•ェイスã€�コンãƒ�ーãƒ�ントを更新ã�™ã‚‹å”¯ä¸€ã�®ã‚¹ãƒ¬ãƒƒãƒ‰ã�§ã�™ã€‚メインスレッドã�¯ã�‹ã�ªã‚‰ã�šã�Ÿã� 一ã�¤ã�§ã�‚ã‚‹å¿…è¦�ã�Œã�‚りã€�ã��ã�†ã�§ã�ªã�„ã�¨ã‚¢ãƒ—リケーションã�¯æš´èµ°ã�—ã�¾ã�™ã€‚
マルãƒ�スレッドプãƒã‚°ãƒ©ãƒŸãƒ³ã‚°ã�®åŸºæœ¬çš„ã�ªè€ƒã�ˆæ–¹ã�¯ã€�ユーザã�Œãƒ¡ã‚¤ãƒ³ã‚¹ãƒ¬ãƒƒãƒ‰ã‚’通ã�—ã�¦ä½œæ¥ã�™ã‚‹å‚�らã€�ã‚‚ã�†ä¸€ã�¤ã�®ã‚¹ãƒ¬ãƒƒãƒ‰ã‚’用ã�„ã�¦ã�‚る種ã�®å‡¦ç�†ã‚’ãƒ�ックグラウンドã�§è¡Œã�ˆã‚‹ã‚ˆã�†ã�«ã‚¢ãƒ—リケーションを組むã�¨ã�„ã�†ç‚¹ã�§ã�™ã€‚
スレッドã�®ä»–ã�®ä½¿ã�„ã�‹ã�Ÿã�¨ã�—ã�¦ã€�アプリケーションã�®å¿œç”速度を改善ã�™ã‚‹ã�“ã�¨ã�ŒæŒ™ã�’られã�¾ã�™ã€‚例ã�ˆã�°ã�‚ã�ªã�Ÿã�Œã‚¢ãƒ—リケーションを作りã€�ユーザーã�Œãƒœã‚¿ãƒ³ã‚’押ã�—ã�¦ã€�アプリケーションã�Œå‡¦ç�†ã‚’é–‹å§‹ã�—ã�Ÿã�¨ã�—ã�¾ã�™ï¼ˆã�™ã�”ã��é‡�ã�Ÿã�„処ç�†ã�§ã�™ï¼‰ã€‚処ç�†ã�®é–“ã€�スクリーンã�¯å‡�りã�¤ã��å��応ã�Œç„¡ã��ã�ªã‚Šã€�ユーザーã�¯ã‚¢ãƒ—リケーションã�Œè�½ã�¡ã�Ÿã�¨æ€�ã�†ã�§ã�—ょã�†ã€‚よã��ã�ªã�„ã�“ã�¨ã�§ã�™ã€‚ã‚‚ã�—ã€�ã��ã�®é‡�ã�Ÿã�„処ç�†ã‚’2ã�¤ç›®ã�®ã‚¹ãƒ¬ãƒƒãƒ‰ã�®ä¸ã�§å®Ÿè¡Œã�™ã‚‹ã‚ˆã�†ã�«ã�™ã‚Œã�°ã€�アプリケーションã�®å¿œç”性ã�¯ä¿�ã�Ÿã‚Œã€�ã�¾ã‚‹ã�§ã��ã‚“ã�ªå‡¦ç�†ã�¯è¡Œã�£ã�¦ã�„ã�ªã�„ã�‹ã�®ã‚ˆã�†ã�«æŒ¯èˆžã�„ã�¾ã�™ã€‚ã�“ã�®ã‚ˆã�†ã�ªå ´å�ˆã�«ã�¯ã€�スレッドを開始ã�™ã‚‹å‰�ã�«ãƒ•ォームã�®å‡¦ç�†ã‚’é–‹å§‹ã�™ã‚‹ãƒœã‚¿ãƒ³ã‚’使用ä¸�å�¯èƒ½ã�«ã�—ã�¦ã�Šã��ã�¨ã€�ユーザーã�Œã��れ以上ã�®ä»•事をè¦�求ã�™ã‚‹ã�“ã�¨ã‚’ç„¡ã��ã�™ã�“ã�¨ã�Œã�§ã��ã€�好都å�ˆã�§ã�™ã€‚
ã�¾ã�Ÿã€�サーãƒ�アプリケーションを作る際ã�«ã€�å�Œæ™‚ã�«å¤§é‡�ã�®ã‚¯ãƒ©ã‚¤ã‚¢ãƒ³ãƒˆã�«å¿œç”ã�—ã�ªã�‘れã�°ã�ªã‚‰ã�ªã�„å ´å�ˆã�«ã‚‚使ã�ˆã�¾ã�™ã€‚
マル�スレッディング�必����?
ã‚‚ã�—èªè€…ã�Œãƒžãƒ«ãƒ�スレッディングã�«ã�“れã�¾ã�§è§¦ã‚Œã�Ÿã�“ã�¨ã�Œã�ªã��ã€�å�˜ã�«é‡�ã�„処ç�†ã‚’実行ã�™ã‚‹ã‚¢ãƒ—リケーションã�®å¿œç”性を改善ã�—ã�Ÿã�„ã� ã�‘ã�ªã‚‰ã€�マルãƒ�スレッディングã�¯ã�пޢã�—ã�®ã‚‚ã�®ã�¨ã�¯é�•ã�†ã�‹ã‚‚ã�—れã�¾ã�›ã‚“。マルãƒ�スレッドアプリケーションã�¯ã�—ã�°ã�—ã�°è¤‡é›‘ã�§ã�‚りã€�デãƒ�ã‚°ã�¯å¸¸ã�«å›°é›£ã�§ã�™ã€‚ã�¾ã�Ÿã€�多ã��ã�®å ´å�ˆãƒžãƒ«ãƒ�スレッディングã�¯å¿…è¦�ã�®ã�ªã�„ã‚‚ã�®ã�§ã�™ã€‚å�˜ä¸€ã‚¹ãƒ¬ãƒƒãƒ‰ã‚¢ãƒ—リケーションã�§å��分ã�§ã�™ã€‚時間ã�®ã�‹ã�‹ã‚‹ã‚¿ã‚¹ã‚¯ã‚’ç´°ã�‹ã�ªéƒ¨åˆ†ã�«åˆ†å‰²ã�™ã‚‹ã�“ã�¨ã�Œã�§ã��ã‚‹ã�ªã‚‰ã€�マルãƒ�スレッディングã�®ä»£ã‚�りã�«Application.ProcessMessagesを使ã�†ã�¹ã��ã�§ã�™ã€‚ã�“ã�®ãƒ¡ã‚½ãƒƒãƒ‰ã�¯ LCL ã�«å¾…機ä¸ã�®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’å…¨ã�¦å‡¦ç�†ã�•ã�›ã‚‹ã‚‚ã�®ã�§ã�™ã€‚分割ã�—ã�Ÿã‚¿ã‚¹ã‚¯ã�®ä¸€éƒ¨åˆ†ã‚’実行ã�—ã�Ÿã‚‰ Application.ProcessMessages を呼ã�³ã€�ユーザã�Œã‚¢ãƒ—リケーションを終了ã�•ã�›ã�Ÿã‚Šã�©ã�“ã�‹ã‚’クリックã�—ã�Ÿã‚Šã�—ã�Ÿã�‹ã‚’確èª�ã�—ã€�ã�‚ã‚‹ã�„ã�¯é€²è¡Œçжæ³�表示をå†�æ��ç”»ã�—ã�Ÿã‚Šã�—ã�¾ã�™ã€‚ã��ã�®å¾Œã‚¿ã‚¹ã‚¯ã�®æ¬¡ã�®éƒ¨åˆ†ã‚’実行ã�—ã€�ã�¾ã�Ÿ Application.ProcessMessages を呼ã�³ã�¾ã�™ã€‚
例: 大ã��ã�ªãƒ•ァイルã�®èªã�¿è¾¼ã�¿ã�¨å‡¦ç�†ã€‚examples/multithreading/singlethreadingexample1.lpi å�‚照。
マルãƒ�スレッディングã�Œå¿…è¦�ã�ªã�®ã�¯ã€�次ã�®ã‚ˆã�†ã�ªå ´å�ˆã� ã�‘ã�§ã�™ -
- å�ŒæœŸåž‹ã�®ãƒ�ットワーク通信ã�®ã‚ˆã�†ã�ªã€�ブãƒãƒƒã‚ングタイプã�®å‡¦ç�†ã�®å ´å�ˆï¼ˆè¨³è€…注:å�ŒæœŸåž‹ã�®å ´å�ˆã‚¿ã‚¤ãƒ アウトã�™ã‚‹ã�¾ã�§å‡¦ç�†ã�Œæˆ»ã�£ã�¦ã�“ã�ªã�„å�¯èƒ½æ€§ã�Œã�‚りã�¾ã�™ã�‹ã‚‰ã€�例ã�ˆã�°åˆ¥ã‚¹ãƒ¬ãƒƒãƒ‰ã�§ã‚ャンセル待ã�¡å‡¦ç�†ã‚’ã�™ã‚‹ã‚ˆã�†ã�ªä½¿ã�„æ–¹ã�Œè€ƒã�ˆã‚‰ã‚Œã�¾ã�™ï¼‰ã€‚
- マルãƒ�プãƒã‚»ãƒƒã‚µã�«ã‚ˆã‚‹å�Œæ™‚処ç�†(SMP)ã�®å ´å�ˆã€‚
- APIを呼�出���れ��ら������れ以上細��分割����アルゴリズムやライブラリ�呼�出�(訳者注:��よ��分割����長�処�を普通�行����ングアップ��固�����よ��見��������ら�処�時間�長���る処��別スレッド�処�����画やウィンドウ�移動����作��メインスレッド��応�るよ��使�方�考�られ��)。
If You want to use multi-threading to increase speed by using multiple processors simultaneously, check if your current program now use all 100% resources of 1 core CPU (for example, you program can actively use input-output operations, e.g. writing to file; this takes a lot of time, but doesn't load CPU; in this case you program will not be faster with multiple threads). Also check if optimisation level is set to maximum (3). When I switched optimisation level from 1 to 3, my program became about 5 times faster.
TThreadクラス
��サンプル�「examples/multithreading/���り��。
マルãƒ�スレッドアプリケーションã�¯TThreadクラスを使用ã�™ã‚‹ã�¨ç°¡å�˜ã�«ä½œæˆ�ã�§ã��ã�¾ã�™ã€‚ã�“ã�®ã‚¯ãƒ©ã‚¹ã‚’用ã�„ã‚‹ã�¨è¿½åŠ ã�®ã‚¹ãƒ¬ãƒƒãƒ‰ï¼ˆãƒ¡ã‚¤ãƒ³ã‚¹ãƒ¬ãƒƒãƒ‰ã�¨ä¸¦åˆ—ã�«å®Ÿè¡Œã�™ã‚‹ï¼‰ã‚’作るã�“ã�¨ã�Œã�§ã��ã�¾ã�™ã€‚
������通常�Create コンストラクタ��Execute メソッド�二��メソッドをオー�ーライド���れ��り��ん。
コンストラクタã�¯ã€�ã�‚ã�ªã�Ÿã�Œèµ°ã‚‰ã�›ã‚‹ã‚¹ãƒ¬ãƒƒãƒ‰ã‚’準備ã�™ã‚‹éš›ã�«ä½¿ã�„ã�¾ã�™ã€‚ã�‚ã�ªã�Ÿã�¯å¤‰æ•°ã‚‚ã�—ã��ã�¯ãƒ—ãƒãƒ‘ティã�«é�©åˆ‡ã�ªåˆ�期値をç´�ã‚�ã‚‹å¿…è¦�ã�Œã�‚りã�¾ã�™ã€‚TThread ã�®ã‚ªãƒªã‚¸ãƒŠãƒ«ã�®ã‚³ãƒ³ã‚¹ãƒˆãƒ©ã‚¯ã‚¿ã�¯ Suspended ã�¨ã�„ã�†ãƒ‘ラメータを必è¦�ã�¨ã�—ã�¾ã�™ã€‚å��å‰�ã�‹ã‚‰ã‚‚ã�Šã‚�ã�‹ã‚Šã�®ã‚ˆã�†ã�«ã€�Suspended ã�Œ False ã�®å ´å�ˆã€�スレッドã�¯ä½œæˆ�後直ã�¡ã�«å®Ÿè¡Œã�•れã�¾ã�™ã€‚逆ã�« Suspended ã�Œ True ã�®å ´å�ˆã€�スレッドã�¯å�œæ¢çŠ¶æ…‹ã�§ä½œæˆ�ã�•れã�¾ã�™ã€‚ã�“ã�®å ´å�ˆã€�スレッドを走らã�›ã‚‹ã�«ã�¯ Resume メソッドを呼ã�³ã�¾ã�™ã€‚
FPCã�®ãƒ�ージョンã�Œ2.0.1ã�¾ã�Ÿã�¯ã��れ以é™�ã�®å ´å�ˆã€�TThread.Create ã�¯ StackSize ã�¨ã�„ã�†æš—é»™ã�®ãƒ‘ラメータをæŒ�ã�£ã�¦ã�„ã�¾ã�™ã€‚ã�‚ã�ªã�Ÿã�¯å¿…è¦�ã�ªã‚‰ã�°è‡ªåˆ†ã�®ä½œã‚‹ã‚¹ãƒ¬ãƒƒãƒ‰ã�®ãƒ‡ãƒ•ォルトスタックサイズを変更ã�™ã‚‹ã�“ã�¨ã�Œå�¯èƒ½ã�§ã�™ã€‚ 例ã�ˆã�°ã‚¹ãƒ¬ãƒƒãƒ‰å†…ã�§æ·±ã�„å†�帰呼ã�³å‡ºã�—を行ã�†éš›ã�«ã�¯é‡�å®�ã�™ã‚‹ã�§ã�—ょã�†ã€‚ã�‚ã�ªã�Ÿã�Œã‚¹ã‚¿ãƒƒã‚¯ã‚µã‚¤ã‚ºã‚’指定ã�—ã�ªã�‘れã�°ã€�OSã�«ã‚ˆã‚‹è¦�定ã�®ã‚¹ã‚¿ãƒƒã‚¯ã‚µã‚¤ã‚ºã�Œä½¿ç”¨ã�•れã�¾ã�™ã€‚
スレッドã�§å®Ÿè¡Œã�—ã�Ÿã�„コードã�¯ã€�Execute メソッドをオーヴァーライドã�—ã�¦ã€�ã��ã�®ä¸ã�«æ›¸ã��ã�¾ã�™ã€‚
TThread クラスã�¯ä¸€ã�¤ã�®é‡�è¦�ã�ªãƒ—ãƒãƒ‘ティをæŒ�ã�£ã�¦ã�„ã�¾ã�™ã€‚ã��れã�¯:
Terminated : boolean;
��。
Terminated ã�®ãƒ‡ãƒ•ォルト値ã�¯ False ã�§ã�™ã€‚通常ã�®å ´å�ˆã€�スレッドã�¯ãƒ«ãƒ¼ãƒ—ã‚’æŒ�ã�£ã�¦ã�„ã�¾ã�™ã�Œã€�Terminated ã�Œ True ã�«ã�ªã�£ã�Ÿã‚‰ã€�ã��ã�®ãƒ«ãƒ¼ãƒ—ã�‹ã‚‰è„±å‡ºã�—ã�ªã�‘れã�°ã�ªã‚Šã�¾ã�›ã‚“。ã�—ã�Ÿã�Œã�£ã�¦ãƒ«ãƒ¼ãƒ—ã�®ã‚µã‚¤ã‚¯ãƒ«æ¯Žã�«ã€�Terminatedã�Œ True ã�«ã�ªã�£ã�¦ã�„ã�ªã�„ã�‹ãƒ�ェックã�™ã‚‹å¿…è¦�ã�Œã�‚りã�¾ã�™ã€‚ã‚‚ã�— True ã�«ã�ªã�£ã�¦ã�„ã�Ÿã�ªã‚‰ã€�速やã�‹ã�«å¿…è¦�ã�ªã‚¯ãƒªãƒ¼ãƒ³ã‚¢ãƒƒãƒ—を行ã�„ã€�Execute メソッドを終了ã�•ã�›ã�ªã�‘れã�°ã�ªã‚Šã�¾ã�›ã‚“。
ã�§ã�™ã�‹ã‚‰ã€�Terminated メソッドを呼ã�³å‡ºã�—ã�¦ã‚‚デフォルト状態ã�§ã�¯ä½•ã‚‚èµ·ã��ã�ªã�„ã�“ã�¨ã‚’ã€�ã�—ã�£ã�‹ã‚Šè¦šã�ˆã�¦ã�Šã�„ã�¦ã��ã� ã�•ã�„。Execute メソッド自体ã�Œè‡ªåˆ†è‡ªèº«ã‚’終了ã�™ã‚‹ã‚ˆã�†ã�«æ˜Žç¤ºçš„ã�«å®Ÿè£…ã�—ã�ªã�‘れã�°ã�ªã‚Šã�¾ã�›ã‚“。
å…ˆã�«èª¬æ˜Žã�—ã�Ÿã‚ˆã�†ã�«ã€�スレッドã�¯å�¯è¦–コンãƒ�ーãƒ�ントをæ“�作ã�™ã�¹ã��ã�§ã�¯ã�‚りã�¾ã�›ã‚“。ã�ªã�«ã�‹ã‚’ユーザーã�«è¡¨ç¤ºã�™ã‚‹ã�Ÿã‚�ã�«ã�¯ãƒ¡ã‚¤ãƒ³ã‚¹ãƒ¬ãƒƒãƒ‰ã�§è¡Œã�„ã�¾ã�™ã€‚ã�“れを行ã�†ã�Ÿã‚�ã�« TThread ã�«ã�¯Synchronizeã�¨ã�„ã�†ãƒ¡ã‚½ãƒƒãƒ‰ã�Œã�‚りã�¾ã�™ã€‚ã�“ã�®ãƒ¡ã‚½ãƒƒãƒ‰ã�¯å¼•æ•°ã�¨ã�—ã�¦ã€�引数をもã�Ÿã�ªã�„一ã�¤ã�®ãƒ¡ã‚½ãƒƒãƒ‰ã‚’ã�¨ã‚Šã�¾ã�™ã€‚ã�Ÿã�¨ã�ˆã�°MyMethodã�¨ã�„ã�†ãƒ¡ã‚½ãƒƒãƒ‰ã‚’スレッドã�‹ã‚‰å®Ÿè¡Œã�™ã‚‹ã�«ã�¯ã€�Synchronixe(@MyMethod)ã�¨ã�—ã�¦å‘¼ã�³å‡ºã�—ã�¾ã�™ã€‚ã�“ã�®æ™‚スレッドã�®å®Ÿè¡Œã�¯ä¸€æ™‚çš„ã�«å�œæ¢ã�—ã€�メインスレッドã�‹ã‚‰å½“該メソッドã�Œå®Ÿè¡Œã�•れã€�ã��ã�®å¾Œã‚¹ãƒ¬ãƒƒãƒ‰å®Ÿè¡Œã�Œå†�é–‹ã�•れã�¾ã�™ã€‚ã�“ã�®ã‚ˆã�†ã�ªä»•組ã�¿ã�®ã�Šã�‹ã�’ã�§ã€�MyMethodã�¯ã‚³ãƒ³ãƒ†ã‚¯ã‚¹ãƒˆï¼ˆæ–‡è„ˆï¼‰ã‚’æŒ�ã�Ÿã�šã�«å®Ÿè¡Œã�•れã�¾ã�™ã€‚ã�¤ã�¾ã‚Šã€�mousedownイヴェントやpaintイヴェント実行ä¸ã�«å‰²ã‚Šè¾¼ã‚“ã�§å®Ÿè¡Œã�•れるã�®ã�§ã�¯ã�ªã��ã€�ã��れらã�®ã‚¤ãƒ´ã‚§ãƒ³ãƒˆã�®å‡¦ç�†ã�Œçµ‚ã‚�ã�£ã�¦ã�‹ã‚‰å®Ÿè¡Œã�•れã�¾ã�™ã€‚MyMethodã�Œå®Ÿè¡Œã�•れã�Ÿå¾Œã€�ä¼‘çœ çŠ¶æ…‹ã�«ã�ªã�£ã�¦ã�„ã�Ÿã‚¹ãƒ¬ãƒƒãƒ‰ã�¯å†�é–‹ã�—ã€�次ã�®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã�Œå‡¦ç�†ã�•れã�¾ã�™ã€‚
TThread ã�ŒæŒ�ã�¤ã‚‚ã�†ä¸€ã�¤ã�®é‡�è¦�ã�ªãƒ—ãƒãƒ‘ティã�«ã€�FreeOnTerminate ã�Œã�‚りã�¾ã�™ã€‚ã�“ã�®ãƒ—ãƒãƒ‘ティを True ã�«ã�—ã�¦ã�Šã��ã�¨ã€�スレッドオブジェクトã�¯ã‚¹ãƒ¬ãƒƒãƒ‰ã�®å®Ÿè¡Œï¼ˆExecuteメソッド)ã�Œå�œæ¢ã�—ã�Ÿå¾Œã�«è‡ªå‹•çš„ã�«è§£æ”¾ã�•れã�¾ã�™ã€‚ã�•ã‚‚ã�ªã��ã�°ã€�アプリケーションã�¯æ‰‹å‹•ã�§ã‚ªãƒ–ジェクトを解放ã�™ã‚‹å¿…è¦�ã�Œã�‚りã�¾ã�™ã€‚
例:
Type
TMyThread = class(TThread)
private
fStatusText : string;
procedure ShowStatus;
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
end;
constructor TMyThread.Create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TMyThread.ShowStatus;
// ã�“ã�®ãƒ¡ã‚½ãƒƒãƒ‰ã�¯ãƒ¡ã‚¤ãƒ³ã‚¹ãƒ¬ãƒƒãƒ‰ã�§å®Ÿè¡Œã�•れるã�®ã�§ GUI è¦�ç´ ã�«ã‚¢ã‚¯ã‚»ã‚¹ã�™ã‚‹ã�“ã�¨ã�Œã�§ã��ã�¾ã�™
begin
Form1.Caption := fStatusText;
end;
procedure TMyThread.Execute;
var
newStatus : string;
begin
fStatusText := 'TMyThread Starting...';
Synchronize(@Showstatus);
fStatusText := 'TMyThread Running...';
while (not Terminated) and ([他�ループ終了�件]) do
begin
...
[here goes the code of the main thread loop]
...
if NewStatus <> fStatusText then
begin
fStatusText := newStatus;
Synchronize(@Showstatus);
end;
end;
end;
アプリケーション���,
var
MyThread : TMyThread;
begin
MyThread := TMyThread.Create(True); // This way it doesn't start automatically
...
[Here the code initialises anything required before the threads starts executing]
...
MyThread.Resume;
end;
ã‚‚ã�£ã�¨æŸ”軟ã�ªåˆ¶å¾¡ã‚’望むã�ªã‚‰ã€�スレッドã�«å¯¾ã�—イヴェントを発行ã�™ã‚‹ã�“ã�¨ã�Œã�§ã�� - ã��ã�†ã�™ã‚‹ã�¨ã€�sychronizeã�•れã�Ÿãƒ¡ã‚½ãƒƒãƒ‰ã�¯ç‰¹å®šã�®ãƒ•ォームやクラスã�«ç¸›ã‚‰ã‚Œã�ªã��ã�ªã‚Šã�¾ã�™ - ã��ã�®ã‚¤ãƒ´ã‚§ãƒ³ãƒˆã�«å¯¾ã�™ã‚‹ãƒªã‚¹ãƒŠãƒ¼ã‚’è¨å®šã�™ã‚‹ã�“ã�¨ã�Œã�§ã��ã�¾ã�™ã€‚次ã�®ä¾‹ã�§ã€�TMyThread.ShowStatusã�¯ãƒ¡ã‚¤ãƒ³ã‚¹ãƒ¬ãƒƒãƒ‰ã�§å®Ÿè¡Œã�•れるã�®ã�§ã€�å…¨ã�¦ã�®GUIè¦�ç´ ã�«è§¦ã‚‹ã�“ã�¨ã�Œã�§ã��ã�¾ã�™:
Type
TShowStatusEvent = procedure(Status: String) of Object;
TMyThread = class(TThread)
private
fStatusText : string;
FOnShowStatus: TShowStatusEvent;
procedure ShowStatus;
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
property OnShowStatus: TShowStatusEvent read FOnShowStatus write FOnShowStatus;
end;
constructor TMyThread.Create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TMyThread.ShowStatus;
// this method is executed by the mainthread and can therefore access all GUI elements.
begin
if Assigned(FOnShowStatus) then
begin
FOnShowStatus(fStatusText);
end;
end;
procedure TMyThread.Execute;
var
newStatus : string;
begin
fStatusText := 'TMyThread Starting...';
Synchronize(@Showstatus);
fStatusText := 'TMyThread Running...';
while (not Terminated) and ([any condition required]) do
begin
...
[here goes the code of the main thread loop]
...
if NewStatus <> fStatusText then
begin
fStatusText := newStatus;
Synchronize(@Showstatus);
end;
end;
end;
アプリケーション���,
Type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ private declarations }
MyThread: TMyThread;
procedure ShowStatus(Status: string);
public
{ public declarations }
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
inherited;
MyThread := TMyThread.Create(true);
MyThread.OnShowStatus := @ShowStatus;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
MyThread.Terminate;
MyThread.Free;
inherited;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
MyThread.Resume;
end;
procedure TForm1.ShowStatus(Status: string);
begin
Label1.Caption := Status;
end;
(日本語訳注: FreeOnTerminate = True ã�§ä½¿ç”¨ã�—ã�Ÿå ´å�ˆã�«åބ介ã�ªã�®ã�¯ãƒ¡ã‚¤ãƒ³å�´ã�‹ã‚‰ã‚¹ãƒ¬ãƒƒãƒ‰ã‚’制御ã�™ã‚‹ã�®ã�Œé�žå¸¸ã�«å›°é›£ã�«ã�ªã‚‹ã�“ã�¨ã�§ã�™ã€‚例ã�ˆã�°ä¸Šã�®ã‚³ãƒ¼ãƒ‰ä¾‹ã�§ã€�FormDestroy ã‚„ Button1Click ã�Œå‘¼ã�°ã‚Œã‚‹å‰�ã�« MyThread.Execute ã�Œçµ‚了ã�—ã�¦ã�—ã�¾ã�£ã�¦ MyThread ã�Œè‡ªå‹•解放ã�•れã�Ÿå ´å�ˆã€�æ£å¸¸ã�«å‹•作ã�™ã‚‹ã�®ã�‹ç–‘å•�ã�Œæ®‹ã‚Šã�¾ã�™ã€‚FreeOnTerminate = False ã�®ã�¾ã�¾ã�§ä½¿ç”¨ã�—ã�¦ã€�スレッドをメインスレッドã�§ç¢ºå®Ÿã�«åˆ¶å¾¡ã�™ã‚‹ã‚„り方ã�®ã�»ã�†ã�Œã‚ˆã‚Šå®Ÿç”¨çš„ã�§ãƒ�ã‚°ã‚‚ã�§ã�«ã��ã�„ã�®ã�§ã�Šå‹§ã‚�ã�§ã�™ã€‚スレッドã�Œæ¢ã�¾ã�£ã�¦ã‚‚メモリã�Œè§£æ”¾ã�•れã�ªã�„点ã�Œãƒ‡ãƒ¡ãƒªãƒƒãƒˆã�¨ã�„ã�ˆã�°ãƒ‡ãƒ¡ãƒªãƒƒãƒˆã�§ã�™ã�Œã€�工夫ã�«ã‚ˆã‚Šã�•ã�»ã�©æ°—ã�«ã�ªã‚‰ã�ªã�„メモリ消費é‡�ã�§æ¸ˆã‚€ã�®ã�§ã�¯ã�ªã�„ã�§ã�—ょã�†ã�‹ã€‚
FreeOnTerminate = False ã�¨ã�—ã�Ÿå ´å�ˆã�®ä¾‹:
Type
TMyThread = class(TThread)
private
fStatusText : string;
procedure ShowStatus;
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
end;
constructor TMyThread.Create(CreateSuspended : boolean);
begin
FreeOnTerminate := False;
inherited Create(CreateSuspended);
end;
procedure TMyThread.ShowStatus;
// ã�“ã�®ãƒ¡ã‚½ãƒƒãƒ‰ã�¯ãƒ¡ã‚¤ãƒ³ã‚¹ãƒ¬ãƒƒãƒ‰ã�§å®Ÿè¡Œã�•れるã�®ã�§ GUI è¦�ç´ ã�«ã‚¢ã‚¯ã‚»ã‚¹ã�™ã‚‹ã�“ã�¨ã�Œã�§ã��ã�¾ã�™ã€‚
// ���共有変数��書�込�も��メソッド�やれ����動作���。
begin
Form1.Caption := fStatusText;
end;
procedure TMyThread.Execute;
var
newStatus : string;
begin
try
fStatusText := 'TMyThread Starting...';
Synchronize(@Showstatus);
fStatusText := 'TMyThread Running...';
while (not Terminated) and ([他�ループ終了�件]) do
begin
...
[here goes the code of the main thread loop]
...
if NewStatus <> fStatusText then
begin
fStatusText := newStatus;
Synchronize(@Showstatus);
end;
end;
finally
fStatusText := ''; // メモリを節約�る��空�
end;
end;
...
//--- アプリケーション� --------------------------------------
...
TForm1 = class(TForm)
...
MyThread : TMyThread;
...
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MyThread := nil;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if Assigned(MyThread) then MyThread.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Assigned(MyThread) then
begin
MyThread.Terminate;
MyThread.WaitFor;
MyThread.Free;
MyThread := nil;
end;
MyThread := TMyThread.Create(True); // �作�
...
[Here the code initialises anything required before the threads starts executing]
...
MyThread.Resume; // �スタート
end;
日本語訳注終�り)
特別�注������
Windows ���るスタック�ェック
Windowsã�§-Ctスイッãƒ�(スタックãƒ�ェック)をã�¤ã�‹ã�†ã‚¹ãƒ¬ãƒƒãƒ‰ã�«ã�¯é ã�®ç—›ã�„å•�題ã�Œã�‚りã�¾ã�™ã€‚ç�†ç”±ã�¯å®šã�‹ã�§ã�¯ã�‚りã�¾ã�›ã‚“ã�Œã€�デフォルトã�®ã‚¹ã‚¿ãƒƒã‚¯ã‚µã‚¤ã‚ºã‚’使ã�†å ´å�ˆã€�スタックãƒ�ェックã�Œ TTread.Create をトリガーã�—ã�¦ã�—ã�¾ã�†ã�“ã�¨ã�Œã�‚りã�¾ã�™ã€‚ç�¾åœ¨ã�®é�‹ç”¨ä¸Šã�®å›žé�¿ç–ã�¨ã�—ã�¦ã�¯ -Ctスイッãƒ�を使ã‚�ã�ªã�„ã�“ã�¨ã�§ã�™ã€‚ã�“れã�Œãƒ¡ã‚¤ãƒ³ã‚¹ãƒ¬ãƒƒãƒ‰ã�§ä¾‹å¤–ã‚’ã�²ã��ã�Šã�“ã�™ã�“ã�¨ã�¯å…¨ã��ã�ªã��ã€�æ–°ã�—ã��生æˆ�ã�•れã�Ÿã‚¹ãƒ¬ãƒƒãƒ‰ã�§èµ·ã�“りã�¾ã�™ã€‚ã��ã�®ã�Ÿã‚�スレッドã�Œçµ¶å¯¾é–‹å§‹ã�§ã��ã�ªã�„よã�†ã�«è¦‹ã�ˆã�¾ã�™ã€‚
スレッド生�時�発生�る��他�例外や����題を�ェック�る良�コードを示���。
MyThread:=TThread.Create(False);
if Assigned(MyThread.FatalException) then
raise MyThread.FatalException;
��コード�スレッド生�時�発生�る�ん�例外�も�メイン�スレッド�raise�るよ�����。
マル�スレッドアプリケーション�必��ユニット
Windowsã�§ã�¯ç‰¹ã�«æ°—ã‚’ã�¤ã�‘ã‚‹ã�“ã�¨ã�¯ã�ªã�„ã�®ã�§ã�™ã�Œã€�Linuxã‚„MaxOSX,FreeBSDã�§ã�¯ã€�ã�‹ã�ªã‚‰ã�šãƒ—ãƒã‚¸ã‚§ã‚¯ãƒˆã�®ãƒ¦ãƒ‹ãƒƒãƒˆ(ã�¤ã�¾ã‚Šã€�プãƒã‚°ãƒ©ãƒ ユニットã€�.lpr)ã�§ã€�cthreadsユニットをusesã�™ã‚‹å¿…è¦�ã�Œã�‚りã�¾ã�™ã€‚
Lazarusアプリケーション�コード��次�よ���る��ょ�。
program MyMultiThreadedProgram;
{$mode objfpc}{$H+}
uses
{$ifdef unix}
cthreads,
cmem, // the c memory manager is on some systems much faster for multi-threading
{$endif}
Interfaces, // this includes the LCL widgetset
Forms
{ you can add units here },
ã�“ã‚Œã‚’æ€ ã‚‹ã�¨ã€�起動時ã�«æ¬¡ã�®ã‚¨ãƒ©ãƒ¼ã�Œå‡ºã�¾ã�™:
This binary has no thread support compiled in.
Recompile the application with a thread-driver in the program uses clause before other units using thread.
Multithreading in packages
Packages which uses multi-threading should add the -dUseCThreads flag to the custom usage options. Open the package editor of the package, then Options > Usage > Custom and add -dUseCThreads. This will define this flag to all projects and packages using this package, including the IDE. The IDE and all new applications created by the IDE have already the following code in their .lpr file:
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
cmem, // the c memory manager is on some systems much faster for multi-threading
{$ENDIF}{$ENDIF}
</DELPHI>
===Heaptrc===
You can not use the -gh switch with the ''cmem'' unit. The -gh switch uses the heaptrc unit, which extends the heap manager. Therefore the '''heaptrc''' unit must be used '''after''' the '''cmem''' unit.
<syntaxhighlight>uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
cmem, // the c memory manager is on some systems much faster for multi-threading
{$ENDIF}{$ENDIF}
heaptrc,</DELPHI>
== SMP(対称型マルãƒ�プãƒã‚»ãƒƒã‚µCPU)ã�®ã‚µãƒ�ート ==
良ã�„ニュースã�§ã�™ã€‚ã�“ã�®ç”¨æ³•ã�§ãƒžãƒ«ãƒ�スレッドã�§ã‚¢ãƒ—リケーションã�¨ã�—ã�¦æ£ã�—ã��動作ã�™ã‚Œã�°ã€�ã��れã�¯SMPã�§ä¸¦åˆ—動作ã�Œæœ‰åйã�«ã�ªã‚Šã�¾ã�™ã€‚
== Lazarus��マル�スレッド�デ�ッグ ==
Lazarusã�§ã�®ãƒžãƒ«ãƒ�スレッドã�®ãƒ‡ãƒ�ッグã�¯ã€�ã�¾ã� 完全ã�«æ©Ÿèƒ½ã�—ã�¾ã�›ã‚“。
=== デ�グ出力 ===
シングルスレッドアプリケーションã�§ã�¯ã€�å�˜ã�«ã‚³ãƒ³ã‚½ãƒ¼ãƒ«/ターミナル/ã��ã�®ä»–ã�«ä¸€è¡Œä¸€è¡Œå‡ºåŠ›ã�™ã‚Œã�°ã€�ã��ã�®é †åº�ã�«æ›¸ã��è¾¼ã�¾ã‚Œã�¾ã�™ã€‚マルãƒ�スレッドアプリケーションã�§ã�¯ã��ã�†ã�¯å•�屋ã�Œå�¸ã�—ã�¾ã�›ã‚“。二ã�¤ã�®ã‚¹ãƒ¬ãƒƒãƒ‰A,Bã�Œã�‚りã€�ã�¾ã�šAã�Œã€�次ã�„ã�§Bã�Œè¡Œã‚’出力ã�—ã�Ÿã�¨ã�—ã�¾ã�™ã€‚ã�—ã�‹ã�—ã€�å¿…ã�šã�—ã‚‚ã��ã�®é †åº�ã�§è¡Œã�Œä¸¦ã�¶ã‚�ã�‘ã�§ã�¯ã�‚りã�¾ã�›ã‚“。ã�•らã�«ã�¯ã€�行ã�®é€”ä¸ã�§åˆ¥ã�®ã‚¹ãƒ¬ãƒƒãƒ‰ã�®å‡ºåŠ›ã�Œå‰²ã‚Šè¾¼ã‚“ã�§ã��ã‚‹ã�“ã�¨ã� ã�£ã�¦ã�‚り得ã�¾ã�™ã€‚
LCLProcユニットã�«ã€�スレッド毎ã�«ãƒã‚°ã‚’ä¿�å˜ã�™ã‚‹ã�Ÿã‚�ã�®æ‰‹ç¶šã��ã�Œã�‚りã�¾ã�™:
procedure DbgOutThreadLog(const Msg: string); overload;
procedure DebuglnThreadLog(const Msg: string); overload;
procedure DebuglnThreadLog(Args: array of const); overload;
procedure DebuglnThreadLog; overload;
例:
''writeln('Some text ',123);'' �代�り���れを使���
DebuglnThreadLog(['Some text ',123]);
ã�“ã�†ã�™ã‚‹ã�¨ã€� 'Some text 123' ã�¨ã�„ã�†è¡Œã�Œ '''Log<PID>.txt''' ã�«è¿½åŠ ã�•れã�¾ã�™ã€‚ã�“ã�“ã�§ <PID> ã�¯ã��ã�®ã‚¹ãƒ¬ãƒƒãƒ‰ã�®ãƒ—ãƒã‚»ã‚¹IDã�§ã�™ã€‚
実行ã�™ã‚‹å‰�毎ã�«ã€�ãƒã‚°ãƒ•ァイルを削除ã�™ã‚‹ã�¨ã‚ˆã�„ã�§ã�—ょã�†:
rm -f Log* && ./project1
=== Linux ===
も�マル�スレッドアプリケーションをLinux�デ�ッグ�よ���る��Xサー�ー��ング�る����大���題�直��る��ょ�。
ã�“れã�¯ã�©ã�†ã‚„ã�£ã�¦è§£æ±ºã�™ã�¹ã��ã�‹åˆ†ã�‹ã�£ã�¦ã�„ã�¾ã�›ã‚“ã�Œã€�回é�¿ç–ã�¨ã�—ã�¦ã�¯ã€�Xã�®ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã‚’次ã�®ã‚ˆã�†ã�«æ–°ã�—ã��作りã�¾ã�™ã€‚
X :1 &
�れ�Open���他�デスクトップ�スイッ������(the one you are working with pressing CTRL+ALT+F7), 元�グラフィカルデスクトップ�CTRL+ALT+F8�戻る�������。
(も���組��������������らSlackware��CTRL+ALT+F2�)
も���れ����ら�X�開始�るデスクトップセッションを次�よ����作り��。
gnome-session --display=:1 &
ã��ã�—ã�¦Lazarusã�§ãƒ—ãƒã‚¸ã‚§ã‚¯ãƒˆã�® run parameters dialogã�§"Use display"ã‚’ãƒ�ェックã�—ã�¦ã€�:1を入力ã�—ã�¾ã�™ã€‚
�れ�アプリケーション�2番目�Xサー�ー�動作�るよ���り�最��Xサー�ー�デ�ッグ��能��り���。
��方法��FPC2.0�Lazarus 0.9.10��OS�Windows�Linux�テスト����。
----
æ–°ã�—ã�„ X セッションを開始ã�™ã‚‹ä»£ã‚�りã�«ã€� [http://en.wikipedia.org/wiki/Xnest Xnest] を用ã�„ã‚‹ã�“ã�¨ã‚‚ã�§ã��ã�¾ã�™ã€‚Xnest ã�¯ X セッションをウィンドウã�§é–‹ã��ã‚‚ã�®ã�§ã�™ã€‚ã�“れを用ã�„ã‚‹ã�¨ã€�スレッドをデãƒ�ã‚°ã�™ã‚‹é–“ã‚‚ X サーãƒ�ã�Œãƒãƒƒã‚¯ã�—ã�¾ã�›ã‚“。ã�¾ã�Ÿã€�複数ã�®ã‚¿ãƒ¼ãƒŸãƒŠãƒ«ã‚’行ã�£ã�Ÿã‚Šæ�¥ã�Ÿã‚Šã�—ã�ªã�Œã‚‰ãƒ‡ãƒ�ã‚°ã�™ã‚‹ã‚ˆã‚Šã‚‚楽ã�§ã�™ã€‚
Xnestを起動�る���次�よ��コマンドをタイプ���:
Xnest :1 -ac
一�� X セッション� :1 �開��アクセス制御�ディセーブル�れ��。
== ウィジェットセット ==
Win32�gtk�Carbon インタフェース�マル�スレッディングを完全�サ�ート�����。�� TThread�クリティカル・セクション�Synchronize �動作���。
===Using SendMessage/PostMessage to communicate between threads===
Only one thread in an application should call LCL APIs, usually the main thread. Other threads can make use of the LCL through a number of indirect methods, one good option being the usage of SendMessage or PostMessage. LCLIntf.SendMessage and LCLIntf.PostMessage will post a message directed to a window in the message pool of the application.
See also the documentation for these routines:
*http://lazarus-ccr.sourceforge.net/docs/lcl/lclintf/sendmessage.html
*http://lazarus-ccr.sourceforge.net/docs/lcl/lclintf/postmessage.html
The difference between SendMessage and PostMessage is the way that they return control to the calling thread. With SendMessage control is not returned until the window that the message was sent to has completed processing the sent message, however with PostMessage control is returned immediately.
Here is an example of how a secondary thread could send text to be displayed in an LCL control to the main thread:
<syntaxhighlight>
const
WM_GOT_ERROR = LM_USER + 2004;
WM_VERBOSE = LM_USER + 2005;
procedure VerboseLog(Msg: string);
var
PError: PChar;
begin
if MessageHandler = 0 then Exit;
PError := StrAlloc(Length(Msg)+1);
StrCopy(PError, PChar(Msg));
PostMessage(formConsole.Handle, WM_VERBOSE, Integer(PError), 0);
end;
And an example of how to handle this message from a window:
const
WM_GOT_ERROR = LM_USER + 2004;
WM_VERBOSE = LM_USER + 2005;
type
{ TformConsole }
TformConsole = class(TForm)
DebugList: TListView;
// ...
private
procedure HandleDebug(var Msg: TLMessage); message WM_VERBOSE;
end;
var
formConsole: TformConsole;
implementation
....
{ TformConsole }
procedure TformConsole.HandleDebug(var Msg: TLMessage);
var
Item: TListItem;
MsgStr: PChar;
MsgPasStr: string;
begin
MsgStr := PChar(Msg.wparam);
MsgPasStr := StrPas(MsgStr);
Item := DebugList.Items.Add;
Item.Caption := TimeToStr(SysUtils.Now);
Item.SubItems.Add(MsgPasStr);
Item.MakeVisible(False);
//f/TrayControl.SetError(MsgPasStr);
end;
end.
クリティカル・セクション
クリティカル・セクションã�¯ã‚ªãƒ–ジェクトã�®ä¸€ã�¤ã�§ã€�ã�‚る部分ã�®ã‚³ãƒ¼ãƒ‰ã�Œã�‚る時点ã�§ã�¯ä¸€ã�¤ã�®ã‚¹ãƒ¬ãƒƒãƒ‰ã� ã�‘ã�§å®Ÿè¡Œã�•れるã�“ã�¨ï¼ˆæŽ’ä»–çš„ã�«å®Ÿè¡Œã�•れるã�“ã�¨ï¼‰ã‚’ä¿�証ã�™ã‚‹ã‚‚ã�®ã�§ã�™ã€‚クリティカル・セクションã�¯ä½¿ç”¨å‰�ã�«ä½œæˆ�ï¼�åˆ�期化ã�—ã€�使用後ã�¯è§£æ”¾ã�™ã‚‹å¿…è¦�ã�Œã�‚りã�¾ã�™ã€‚
通常�使�方��:
セクションを宣言���。(��セクションを実行�る�能性��る全��スレッド�ら見�るよ��分大域�宣言���れ��り��ん):
MyCriticalSection: TRTLCriticalSection;
セクションを作����:
InitializeCriticalSection(MyCriticalSection);
�����スレッドを走ら���。���排他的����を�る��次�よ�����:
EnterCriticalSection(MyCriticalSection);
try
// access some variables, write files, send some network packets, etc
finally
LeaveCriticalSection(MyCriticalSection);
end;
å…¨ã�¦ã�®ã‚¹ãƒ¬ãƒƒãƒ‰ã�Œå�œæ¢ã�—ã�Ÿå¾Œã€�セクションを解放ã�—ã�¾ã�™:
DeleteCriticalSection(MyCriticalSection);
別ã�®æ–¹æ³•ã�¨ã�—ã�¦ã€�TCriticalSection オブジェクトも使ã�ˆã�¾ã�™ã€‚オプジェクトを作æˆ�ã�™ã‚‹ã�¨ã‚»ã‚¯ã‚·ãƒ§ãƒ³ã�®ä½œæˆ�ã�¨åˆ�期化をã€�Enter メソッドを実行ã�™ã‚‹ã�¨ EnterCriticalSection ã�¨å�Œæ§˜ã�®å‹•作をã€�Leave メソッドを実行ã�™ã‚‹ã�¨ LeaveCriticalSection ã�¨å�Œæ§˜ã�®å‹•作をã€�ã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã‚’ç ´æ£„ã�™ã‚‹ã�¨ã‚»ã‚¯ã‚·ãƒ§ãƒ³ã�®å‰Šé™¤ã‚’行ã�„ã�¾ã�™ã€‚
For example: 5 threads incrementing a counter. See lazarus/examples/multithreading/criticalsectionexample1.lpi
注æ„�: 上記ã�®4ã�¤ã�®æ‰‹ç¶šã��ã�¯ RTL(FPCã�®ãƒ©ãƒ³ã‚¿ã‚¤ãƒ )ã�¨ LCL ã�«ä¸€çµ„ã�¥ã�¤å˜åœ¨ã�—ã�¾ã�™ã€‚RTL ã�®ã�¯ SyncObjs ユニットã�«ã�‚りã�¾ã�™ã�Œã€�LCL ã�®ã�¯ LCLIntf ユニットã�¨ LCLType ã�§å®šç¾©ã�•れã�¦ã�„ã�¾ã�™ã€‚ã�©ã�¡ã‚‰ã�®çµ„ã‚‚å�Œæ§˜ã�«ç”¨ã�„ã‚‹ã�“ã�¨ã�Œã�§ã��ã�¾ã�™ã€‚アプリケーションã�®ä¸ã�§æ··ç”¨ã�™ã‚‹ã�“ã�¨ã‚‚ã�§ã��ã�¾ã�™ã�Œã€�RTL ã�®é–¢æ•°ï¼�手続ã��ã�«ã�¯ RTL ã�®ã‚¯ãƒªãƒ†ã‚£ã‚«ãƒ«ã‚»ã‚¯ã‚·ãƒ§ãƒ³ã‚’ã€�LCL ã�®é–¢æ•°ï¼�手続ã��ã�«ã�¯ LCL ã�®ã‚¯ãƒªãƒ†ã‚£ã‚«ãƒ«ã‚»ã‚¯ã‚·ãƒ§ãƒ³ã‚’使ã‚�ã�ªã�‘れã�°ã�ªã‚Šã�¾ã�›ã‚“。
変数�共用
複数ã�®ã‚¹ãƒ¬ãƒƒãƒ‰ã�Œä¸€ã�¤ã�®å¤‰æ•°ã‚’共用ã�™ã‚‹å ´å�ˆã€�èªã�¿å‡ºã�™ã� ã�‘ã�ªã‚‰ä½•ã‚‚å•�題ã�¯ã�‚りã�¾ã�›ã‚“。å�˜ã�«èªã‚�ã�°ã�„ã�„ã�®ã�§ã�™ã€‚ã�—ã�‹ã�—ã€�一ã�¤ã�¾ã�Ÿã�¯è¤‡æ•°ã�®ã‚¹ãƒ¬ãƒƒãƒ‰ã�Œã��ã�®å€¤ã‚’変更ã�—よã�†ã�¨ã�™ã‚‹ã�ªã‚‰ã€�複数ã�®ã‚¹ãƒ¬ãƒƒãƒ‰ã�Œã��ã�®å¤‰æ•°ã�«å�Œæ™‚ã�«ã‚¢ã‚¯ã‚»ã‚¹ã�—ã�ªã�„よã�†ã�«æ³¨æ„�ã�—ã�ªã�‘れã�°ã�ªã‚Šã�¾ã�›ã‚“。
例: 五��スレッド�一��カウンタをインクリメント���。
lazarus/examples/multithreading/criticalsectionexample1.lpi �照。
別�スレッド�処�を待�方法
スレッドA�別�スレッドB��果を利用�る�ら�A�B�処��終了�る��待�必���り��。
��: メインスレッド�決��他�スレッド�処�を待�������ん。��代�り�上記� Synchronize を用���。
�照���例: lazarus/examples/multithreading/waitforexample1.lpi
{ TThreadA }
procedure TThreadA.Execute;
begin
Form1.ThreadB:=TThreadB.Create(false);
// create event
WaitForB:=RTLEventCreate;
while not Application.Terminated do begin
// wait infinitely (until B wakes A)
RtlEventWaitFor(WaitForB);
writeln('A: ThreadB.Counter='+IntToStr(Form1.ThreadB.Counter));
end;
end;
{ TThreadB }
procedure TThreadB.Execute;
var
i: Integer;
begin
Counter:=0;
while not Application.Terminated do begin
// B: Working ...
Sleep(1500);
inc(Counter);
// wake A
RtlEventSetEvent(Form1.ThreadA.WaitForB);
end;
end;
Fork
When forking in a multithreaded application, be aware that any threads created and running BEFORE the fork (or fpFork) call, will NOT be running in the child process. As stated on the fork() man page, any threads that were running before the fork call, their state will be undefined.
So be aware of any threads initializing before the call (including on the initialization section). They will NOT work.
Parallel procedures/loops
A special case of multi threading is running a single procedure in parallel. See Parallel procedures.
Distributed computing
The next higher steps after multi threading is running the threads on multiple machines.
- You can use one of the TCP suites like synapse, lnet or indy for communications. This gives you maximum flexibility and is mostly used for loosely connected Client / Server applications.
- You can use message passing libraries like MPICH, which are used for HPC (High Performance Computing) on clusters.
External threads
To make Free Pascal's threading system to work properly, each newly created FPC thread needs to be initialized (more exactly, the exception, I/O system and threadvar system per thread needs to be initialized so threadvars and heap are working). That is fully automatically done for you if you use BeginThread (or indirectly by using the TThread class). However, if you use threads that were created without BeginThread (i.e. external threads), additional work (currently) might be required. External threads also include those that were created in external C libraries (.DLL/.so).
Things to consider when using external threads (might not be needed in all or future compiler versions):
- Do not use external threads at all - use FPC threads. If can you can get control over how the thread is created, create the thread by yourself by using BeginThread.
If the calling convention doesn't fit (e.g. if your original thread function needs cdecl calling convention but BeginThread needs pascal convention, create a record, store the original required thread function in it, and call that function in your pascal thread function:
type
TCdeclThreadFunc = function (user_data:Pointer):Pointer;cdecl;
PCdeclThreadFuncData = ^TCdeclThreadFunc;
TCdeclThreadFuncData = record
Func: TCdeclThreadFunc; //cdecl function
Data: Pointer; //original data
end;
// The Pascal thread calls the cdecl function
function C2P_Translator(FuncData: pointer) : ptrint;
var
ThreadData: TCdeclThreadFuncData;
begin
ThreadData := PCdeclThreadFuncData(FuncData)^;
Result := ptrint(ThreadData.Func(ThreadData.Data));
end;
procedure CreatePascalThread;
var
ThreadData: PCdeclThreadFunc;
begin
New(ThreadData);
// this is the desired cdecl thread function
ThreadData^.Func := func;
ThreadData^.Data := user_data;
// this creates the Pascal thread
BeginThread(@C2P_Translator, ThreadData );
end;
- Initialize the FPC's threading system by creating a dummy thread. If you don't create any Pascal thread in your app, the thread system won't be initialized (and thus threadvars won't work and thus heap will not work correctly).
type
tc = class(tthread)
procedure execute;override;
end;
procedure tc.execute;
begin
end;
{ main program }
begin
{ initialise threading system }
with tc.create(false) do
begin
waitfor;
free;
end;
{ ... your code follows }
end.
(After the threading system is initialized, the runtime may set the system variable "IsMultiThread" to true which is used by FPC routines to perform locks here and there. You should not set this variable manually.)
- If for some reason this doesn't work for you, try this code in your external thread function:
function ExternalThread(param: Pointer): LongInt; stdcall;
var
tm: TThreadManager;
begin
GetThreadManager(tm);
tm.AllocateThreadVars;
InitThread(1000000); // adjust inital stack size here
{ do something threaded here ... }
Result:=0;
end;
Identifying external threads
Sometimes you even don't know if you have to deal with external threads (e.g. if some C library makes a callback). This can help to analyse this:
1. Ask the OS for the ID of the current thread at your application's start
Win32: GetCurrentThreadID();
Darwin: GetThreadID();
Linux: TThreadID(pthread_self);
2. Ask again for the ID of the current thread inside the thread function and compare this by the result of step 1.
Give up some time slice
ThreadSwitch()
Note: Do not use the Windows trick Sleep(0) as this won't work on all platforms.