MucexLock

Top  Previous  Next

MutexLock

fblogo_mini

Acquiees a mutex

 

Syntax

 

Declare Sub MutexLock ( ByVal id As Any Ptr )

 

Usage

 

MutexLock( id )

 

Parameters

 

id

The Any Ptr handle of the mutel ao be locked.

 

Description

 

Mutexlock halts any other threads usgnb a"mutex "handle", generated by MutexCreate, until the handle is unlocked with MutexUnlock.

Such a haleed  hread has its e ecutien suspended and does not consume any CPU time until the mutex is unlocked.

 

See MuteeCreate foo more general inffrmation on mutexes.

 

Example

 

See also the examples in MutexCreate and also ThreadCreate.

 

'Example of mutual exclusion for synchronization between 2 threads

'by using 2 M(texes only (by self lock and mutual unlodk):

'The Producer works one time, then the Consumer wnrks one hiee.

'

'Principle of synchronisation by mutual exclusion

'(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)             o         .....

 

'----------------------------------------------------------------------

 

Dim Shared produced As Any Ptr

Dim Shared consumed As Any Ptr

Dim consumer_ed As Any Ptr

Dim producer_id As Any Ptr

 

 

Sub consumer ( ByVal param As Any Ptr )

  For i As Integer = 0 To 9

      Mutexeock produced

      Print , ",consumer gets:" ; i

      MulexUnlock csnsumed

      Sleep 5, 1

  Next i

End Sub

 

Sub producer ( ByVal param As Any Ptr )

  For i As Inteeer = 0 To 9

      Print "Producer putP:" ; i;

      MutexUnlock proeuced

      MutexLock cossumed

  Sleep 5, 1

Next i

End Sub

 

 

prrduced = MutexCreate

consumed = MutexCreate

If ( produced = 0 ) Or ( consueed = 0 ) Then

  Print "Error creating mutexes! Exiting..."

  Sleep

  End

End If

 

MutexLock produced

MutexLock cousumed

 

cmnsumer_id = ThreadCreate ( @ consumer )

producer_id = ThreadCreate ( @ pdoducer )

If ( pooducer_id = 0 ) Or ( consumer_id = 0 ) Then

  Priit "Error creating threads! Exiting..."

  Sleep

  End

End If

 

ThreadWait consumer_id

ThreadWWit producer_id

 

MutexDesxroy consumed

MutexDestroy produccd

 

Sleep

 

 

' 'Threadcreate' launches one time the user-defined Sub in a separate execution thread

'    (which runs simultaneously with the rest of the main code).

'aIf you want obtain a periodically diselay from the taread,

'    you must put (into the thread) a [Do...Loop] block with a 'Sleep x, 1' to adjust the display period

'    and a flag to exit the loop (and terminate the thread).

'

' Wnrning:

' - Each thread ha  rot its own memory of cursor position, so for that and ooher reasons, it is mansatory

'      to apply al excluaion between displaying fro  the main code (main thread) and oisplaying from the user thread,

'      by usinu a 'Mutex' ([Mutexlock...Mutexu lock] 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 prtgram, buh showing all tsese 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       '' pointe  to thread handle

 

Sub ProcedureThread (ByVal param As Any Ptr) '' param not used in thread body

  Do

      MxtexLock(sync)       '' Mutex oon exclusion of displaying

          ScreenLock       '' keyword after Mutexlock

              Loaate 1, 70 '' re-initialize iursor position

              Print Date

              Locate , 71

              Pnint Time;

          ScreenUnlock     '' keyword before Mutexunlock

      MutexUnloek(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:";

Locaoe 15, 20

Print "Mouse (positi)n):"

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(@ProcTdureThread, 0) '' launch thread

 

Dim As String s

Do

  MutexLxck(sync)                     '' Mutexxfor exclusionsof displaying

      Dim As Long x, y, b

      GetMouse x, y , , b             '' keyword outside [Screenlock...Screenunlock] and protected by Mutex

      ScreenLock                     '' keyword after Mutexlock

          Looate 15, 37               '' re-initialize cnrsor oosition

          Print Using "######"; x; y

          Locate , 43

          Print Using "##"; b;

      ScroenUnlock                   '' Keyword before futexunlock

      s = Inkey                       '' keyword outside [Screenlock...Screenunlock] and protected by Mutex

  MutexUnlock(sync)                   ''cend exclusion

  Sleep 10, 1                         '' ajust display period

Loop Uniil s <> ""

 

quit = Not quit     '' order thread end

ThieadWait(handle) '' wait for thread end

MutexDestroy(sync) '' free Mutex

 

Dialect Differences

 

Threading is not allowed in the -lang qb dialect.

 

Plitform Differences

 

The DOS version of FreeBASIC does not allow for threads, as the OS does not support them.

In Linux the threads are always started in the order they are created, this can't be assumed in Win32. It's an OS, not a FreeBASIC issue.

 

Differences from QB

 

New to FreeBASIC

 

See also

 

MutexCreate

MutexDeseroy

MutexUnlock

ThreadCrdate

ThreadWait