FreeBASIC MutexLock
目次→実行時ライブラリー参考→多重スレッド化サポート→MUTEXLOCK←オリジナル・サイト
ミューテックスを取得します。
構文:
用法:
Mutexlock( id )
パラメタ:
記述:
Mutexlock は、
Mutexcreate で生成されるミューテックス「ハンドル」を使っている他の全てのスレッドを、停止させます。ハンドルが
Mutexunlock で開錠されるまでです。
このような停止したスレッドは、その実行が中断され、ミューテックスがロック解除されるまで CPU時間を消費しません。
例:
'2つのスレッド間の同期のための相互排除の例
'2つのミューテックスだけを使います(セルフロックと相互ロック解除):
'生産者一度動作し、そして、消費者が一度動作します。
'
'相互排除による同期の原則
'(initial condition: mut#A and mut#B locked)
'
' Thread#A XORs Thread#B
'Do_something#A_with_exclusion MutexLock(mut#A)
'MutexUnlock(mut#A) Do_something#B_with_exclusion
'..... MutexUnlock(mut#B)
'MutexLock(mut#B) .....
'----------------------------------------------------------------------
Dim Shared produced As Any Ptr
Dim Shared consumed As Any Ptr
Dim consumer_id As Any Ptr
Dim producer_id As Any Ptr
Sub consumer ( ByVal param As Any Ptr )
For i As Integer = 0 To 9
MutexLock produced
Print , ",消費者が取得:" ; i
MutexUnlock consumed
Sleep 5, 1
Next i
End Sub
Sub producer ( ByVal param As Any Ptr )
For i As Integer = 0 To 9
Print "2.生産者が置く:" ; i;
MutexUnlock produced
MutexLock consumed
Sleep 5, 1
Next i
End Sub
produced = MutexCreate
consumed = MutexCreate
If ( produced = 0 ) Or ( consumed = 0 ) Then
Print "Error creating mutexes! Exiting..."
Sleep
End
End If
MutexLock produced
MutexLock consumed
consumer_id = ThreadCreate ( @ consumer )
producer_id = ThreadCreate ( @ producer )
If ( producer_id = 0 ) Or ( consumer_id = 0 ) Then
Print "Error creating threads! Exiting..."
Sleep
End
End If
ThreadWait consumer_id
ThreadWait producer_id
MutexDestroy consumed
MutexDestroy produced
Sleep
' 'Threadcreate' は、別々の実行スレッドで、ユーザー定義 Sub を1回起動します
' (これはメインコードの残りの部分と同時に実行されます).
' スレッドから定期的に表示したい場合は、
' スレッドの中に、[Do...Loop]ブロックと、表示期間を調整する 'Sleep x, 1' を
入れる必要があります
' そして、ループを終了し、スレッドを終了するフラグも
'
' Warning:
' - Each thread has not its own memory of cursor position, so for that and other reasons, it is mandatory
' to apply an exclusion between displaying from the main code (main thread) and displaying from the user thread,
' by using a 'Mutex' ([Mutexlock...Mutexunlock] block).
' At beginning of each display block both into main thread and user thread,
' the initial cursor position must also be re-initialized.
' - The input keywords (like keyboard, mouse) cannot be safely run when the screen is locked,
' therefore a such keyword must be outside of any [Screenlock...Screenunlock] block (outside this block in its own thread,
' and protected of block of another thread by a 'Mutex').
'
' See below a rustic program, but showing all these constraints:
Dim Shared As Any Ptr sync
'' pointer to Mutex
Dim Shared As Byte quit =
0 '' flag to end user thread
Dim As Any Ptr handle
'' pointer to thread handle
Sub ProcedureThread
(ByVal param
As Any Ptr) '' param not used in thread body
Do
MutexLock(sync
) '' Mutex for exclusion of displaying
ScreenLock '' keyword after Mutexlock
Locate 1,
70 '' re-initialize cursor position
Print Date
Locate ,
71
Print Time;
ScreenUnlock '' keyword before Mutexunlock
MutexUnlock(sync
) '' end exclusion
Sleep 100,
1 '' ajust display period
Loop Until quit <>
0 '' test for exit thread
End Sub
Screen 12
Locate 1,
62
Print "Date:"
Locate ,
62
Print "Time:";
Locate 15,
20
Print "Mouse (position):"
Locate ,
20
Print "Mouse (buttons) :";
Locate 30,
2
Print "<any_key> or <click on window close button>: exit";
sync =
MutexCreate '' create Mutex (before Threadcreate)
handle =
ThreadCreate(@ProcedureThread,
0) '' launch thread
Dim As String s
Do
MutexLock(sync
) '' Mutex for exclusion of displaying
Dim As Long x, y, b
GetMouse x, y , , b
'' keyword outside [Screenlock...Screenunlock] and protected by Mutex
ScreenLock '' keyword after Mutexlock
Locate 15,
37 '' re-initialize cursor position
Print Using "######"; x; y
Locate ,
43
Print Using "##"; b;
ScreenUnlock '' Keyword before Mutexunlock
s =
Inkey '' keyword outside [Screenlock...Screenunlock] and protected by Mutex
MutexUnlock(sync
) '' end exclusion
Sleep 10,
1 '' ajust display period
Loop Until s <>
""
quit =
Not quit
'' order thread end
ThreadWait(handle
) '' wait for thread end
MutexDestroy(sync
) '' free Mutex
方言差:
プラットホーム差:
- FreeBASIC の DOS バージョンは、スレッドを考慮しません。OS がスレッドをサポートしないからです。
- Linux では、スレッドは、常に作成された順に、開始されます。Win32 では、これを想定できません。
これは OS の問題で、FreeBASIC の問題ではありません。
QBからの違い:
参照:
ページ歴史:2022-03-27 13:22:20
日本語翻訳:WATANABE Makoto、原文著作者:JofersToonski
