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

FreeBASIC CondBroadcast

目次→実行時ライブラリー参考→多重スレッド化サポートCONDBROADCAST←オリジナル・サイト

CONDBROADCAST 左にメニュー・フレームが表示されていない場合は、ここをクリックして下さい

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

ハンドルのための、全てのスレッドの Condwait を再開します。

構文:
declare sub Condbroadcast ( byval handle as any ptr )

用法:
Condbroadcast ( handle )

パラメタ:
handle
条件変数のハンドル。

記述:
条件付きが Condcreate でスレッドが開始すると、そのうちの1つ以上(主プログラムを実行している暗黙の主スレッドを含む)が条件付きで Condwait に設定されます。これらは、他のスレッドが Condsignal して、待機中のスレッドが再起動できることを示すまで、停止します。
Condbroadcast を使うと、条件を待機しているすべてのスレッドを再起動できます。
プログラムの最後に、Conddestroy を使って OS のリソース・リークを回避する必要があります。

条件付きで待機しているすべてのスレッドを再起動するには、Condsignal の代わりに Condbroadcast を使う必要があります。

例:
Condcreate を参照下さい。

'' すべてのスレッドが、コードの連続セクションの実行を続行する前に、
'' どのようにすれば互いを待機できるのでしょうか?
''
'' これは、コードの同時セクションの同期を管理するメイン コードです。:
''    - Each thread signals to main code that it is waiting for synchronization.
''    - The main code waits for each of all threads, then sends the synchronization signal to all threads.
''
'' The synchronization uses the following keywords from threading:
''   - In each thread : 'Condsignal' (to send signal to main code) then 'Condwait' (to wait signal from main code).
''   - In main code : 'Condwait' (to wait signal from each thread) then 'Condbroadcast' (to send signal to all threads).
'' With the associated mutexes and conditional variables and flags.
''
'' すべてのスレッドを同期させて、対応するコードセクションを実行するときの、スレッドの構造とその動作を強調したテスト例 :

Dim threadnumber As Integer = 4

'' global data for all threads
    Dim Shared restart As Integer = 0
    Dim Shared threadcount As Integer = 0
   
    Dim Shared hmutexrestart As Any Ptr
    Dim Shared hcondrestart As Any Ptr
    Dim Shared hmutexready As Any Ptr
    Dim Shared hcondready As Any Ptr

Sub mythread(ByVal p As Any Ptr)
   
    Dim id As Integer = Cast(Integer, p)
   
    '' for visualizing thread status
        Print "   Thread #" & id & " is started..."
       
    '' instead of starting user code
        Sleep id * 40, 1
       
    '' for visualizing thread status
        Print "      Thread #" & id & " is running..."
        Sleep id * 20, 1  '' just to well visualize thread interlacing
        Print "   Thread #" & id & " is waiting for synchronization..."
       
    '' for sending signal to main code
        MutexLock hmutexready
            threadcount += 1
            CondSignal hcondready
        MutexUnlock hmutexready
       
    '' for waiting signal from main code
        MutexLock hmutexrestart
            Do While restart = 0  
                CondWait hcondrestart, hmutexrestart
            Loop
        MutexUnlock hmutexrestart
       
    '' for visualizing thread status
        Print "   Thread #" & id & " is reactivated..."
       
    '' instead of synchronized user code
        Sleep id * 40, 1
       
    '' for visualizing thread status
        Print "      Thread #" & id & " is continuing..."
        Sleep id * 20, 1  '' just to well visualize thread interlacing
        Print "   Thread #" & id & " is finishing execution..."
   
End Sub


Dim threads(1 To threadnumber) As Any Ptr

hcondrestart = CondCreate()
hmutexrestart = MutexCreate()
hcondready = CondCreate()
hmutexready = MutexCreate()

'' for visualizing main code status
    Print "Start all threads from main code :"
       
'' for starting all threads
    For i As Integer = 1 To threadnumber
        threads(i) = ThreadCreate(@mythread, Cast(Any Ptr, i))
    Next i

'' for waiting for all threads waiting for synchronisation
    MutexLock hmutexready
        Do Until threadcount = threadnumber
            CondWait(hcondready, hmutexready)
        Loop
    MutexUnlock hmutexready

'' for visualizing main code status
    Print "All thread seen waiting from main code"
           
    Print
    Print "Reactivate all threads from main code :"
       
'' for reactivating all threads
    MutexLock hmutexrestart
        restart = 1
        CondBroadcast hcondrestart
    MutexUnlock hmutexrestart

'' for waiting for all threads to be completed
    For i As Integer = 1 To threadnumber
        If threads(i) <> 0 Then
            ThreadWait threads(i)
        End If
    Next i
   
'' for visualizing main code status
    Print "All thread seen completed from main code"

MutexDestroy hmutexready
CondDestroy hcondready
MutexDestroy hmutexrestart
CondDestroy hcondrestart

Sleep

プラットホーム差:
方言差:
QBからの違い:
参照:
多重スレッド化サポート に戻る
←リンク元に戻る プログラム開発関連に戻る
ページ歴史:2024-03-17 14:02:28
日本語翻訳:WATANABE Makoto、原文著作者:AntoniGual

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

表示-非営利-継承