FreeBASIC CondBroadcast
目次→実行時ライブラリー参考→多重スレッド化サポート→CONDBROADCAST←オリジナル・サイト
ハンドルのための、全てのスレッドの Condwait を再開します。
構文:
用法:
Condbroadcast ( handle )
パラメタ:
記述:
条件付きで待機しているすべてのスレッドを再起動するには、
Condsignal の代わりに
Condbroadcast を使う必要があります。
例:
'' すべてのスレッドが、コードの連続セクションの実行を続行する前に、
'' どのようにすれば互いを待機できるのでしょうか?
''
'' これは、コードの同時セクションの同期を管理するメイン コードです。:
'' - 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
プラットホーム差:
- Condbroadcast は、FreeBASIC の DOS バージョンと、DOS 目標では、利用できません。多重スレッド化は、DOS カーネルによっても、拡張を使っても、サポートされないからです。
- Linux では、スレッドは、常に作成された順に、開始されます。Win32 では、これを想定できません。
これは OS の問題で、FreeBASIC の問題ではありません。
方言差:
QBからの違い:
参照:
ページ歴史:2024-03-17 14:02:28
日本語翻訳:WATANABE Makoto、原文著作者:AntoniGual
