How to Manage a Critical Section of the code of a Thread in FB

Forum for discussion about the documentation project.
fxm
Posts: 9086
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

How to Manage a Critical Section of the code of a Thread in FB

Postby fxm » Dec 14, 2015 19:15

FB provides built-in support for multi-threaded programming.
A multi-threaded process contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.

What is the difference between a thread and a process?

Process:
- Intuitively, a process is nothing more than the program placed in memory or running with all the run-time environment (or all the resources) associated with it.
- In other words, when your program is located on the hard disk of your machine it is always a program or a simple application, but once executed (or placed in memory) a set of resources (amount of memory space occupied, used processor registers and its status, the owner of the process, the permitted operations, ...) is assigned to it and it simply changes its name. It becomes a process.
- So to simplify, process rhymes with program running.

Thread:
- A thread is a portion or part of the process and the smallest sequential unit of instructions processed independently by the scheduler of the operating system.
- It is simply an execution or processing unit composed of a set of instructions contained in the process. See it as the smallest task or operation within the process that is manageable by the scheduler.
- A thread shares information such as a data segment, a code segment, ..., with its peer threads spawned by the same base-thread (see below in post), while it contains its own registers, stack, ....
- Obviously, a process can be composed of one or more threads, everything will depend on the programmer. In the case of a single-threaded process we speak of single-threading, in the opposite case of multi-threading.

What kind of algorithm can be applied to handle critical sections?

A critical section is a part of a multi-threading program that may not be concurrently executed by more than one of the program threads:
- It is a piece of a program that requires mutual exclusion of access.
- Typically, the critical section accesses a shared resource, such as a data structure, a peripheral device, or a network connection, that does not allow multiple concurrent accesses.

When a program starts up, one thread already begins running immediately. This is usually called the "main" thread of the program, because it is the one that is executed when a program begins.
- It is the thread from which user may spawn other “child” threads (which in turn may spawn other "sub-child" threads).
- Often, it must be the last thread to finish execution because it performs various shutdown actions (as a "child" thread must also do so with respect to its eventual "sub-child" threads spawned).
- But other than that, it can also compete (with its own critical sections) with all other threads explicitly spawned by user.

The method to ensure exclusive use of a critical section may be designed in a algorithm either asynchronous or synchronous, which applies to the threads.


1) ASYNCHRONOUS ALGORITHM EXAMPLES

1.1) Asynchronous algorithm using one flag (a boolean variable) per thread
By putting 'true' its own flag means that the thread wants take the exclusive control to access the shared resource (when shared resource access is ended, the thread resets its own flag to 'false').

1.1.1) Asynchronous wait for expected condition, including a sleep in order to release CPU time
After putting its own flag to 'true', the thread waits that all other flags are set to 'false' before entering its critical section.
When shared resource access is ended, the thread resets its own flag to 'false'.

Algorithm:

Code: Select all

'   my_thread_flag = true
'   Do
'   |  Sleep 5
'   Loop Until number_of_thread_flag_true = 1
'   .....
'   Critical section of code
'   .....
'   my_thread_flag = false
This algorithm does not work because of the case of infinite blocking of the thread in its waiting loop (as soon as at least 2 threads wait at same time).

1.1.2) Asynchronous jump if not expected condition
After putting its own flag to 'true', the thread verifies if all other flags are set to 'false' before entering its critical section, otherwise the thread jumps its critical section and continues.
In all cases (after running its critical section or only jumping), the thread resets its own flag to 'false'.

Algorithm:

Code: Select all

'   my_thread_flag = true
'   If number_of_thread_flag_true = 1 Then
'   | .....
'   | Critical section of code
'   | .....
'   End If
'   my_thread_flag = false
There is no case of infinite blocking of the thread, but in general, some threads may be strongly advantaged or disadvantaged compared to others for running their critical sections.

1.1.3) Asynchronous repeat awake then sleep, until expected condition
After putting its own flag to 'true', the thread verifies if all other flags are set to 'false' before entering its critical section, otherwise the thread resets its own flag to 'false' before sleeping up to a new attempt.
When shared resource access is ended, the thread resets its own flag to 'false'.

Algorithm:

Code: Select all

'   Do
'   |  my_thread_flag = true
'   |  If number_of_thread_flag_true = 1 Then
'   |  |  Exit Do
'   |  End If
'   |  my_thread_flag = false
'   |  Sleep 5
'   Loop
'   .....
'   Critical section of code
'   .....
'   my_thread_flag = false
There is no case of infinite blocking of the thread, but in general, some threads may be strongly advantaged or disadvantaged compared to others for running their critical sections.

1.2) Asynchronous algorithm using one mutex for all threads
By getting the mutex locking, the thread can take the exclusive control to access the shared resource.
When shared resource access is ended, the thread unlocks the mutex.

Algorithm:

Code: Select all

'   Mutexlock
'   | .....
'   | Critical section of code
'   | .....
'   Mutexunlock
There is no any advantage or disadvantage between threads for running their critical sections.


2) SYNCHRONOUS ALGORITHM EXAMPLES

2.1) Synchronous algorithm using a priority number among the threads
The thread which has the priority runs its critical section, then passes the priority to the next thread.

2.1.1) Synchronous wait for expected condition, including a sleep in order to release CPU time
The thread waits for its turn before executing its critical section.
When shared resource access is ended, the thread passes the priority to the next thread in the thread number list.

Algorithm:

Code: Select all

'   While thread_priority_number <> my_number
'   |  Sleep 5
'   Wend
'   .....
'   Critical section of code
'   .....
'   thread_priority_number = next thread_priority_number
The critical sections of the threads are run synchronously one after the other, with a predefined order.

2.1.2) Synchronous wait for expected condition, including a condwait then a condbroadcast (and mutex) in order to release CPU time
The thread waits for its turn and also a condition signal (condwait) before executing its critical section.
When shared resource access is ended, the thread passes the priority to the next thread in the thread number list, then send a condition signal to all other threads (condbroadcast).

Algorithm:

Code: Select all

'   Mutexlock
'   |  While thread_priority_number <> my_number
'   |  |  Condwait
'   |  Wend
'   |  .....
'   |  Critical section of code
'   |  .....
'   |  thread_priority_number = next thread_priority_number
'   |  Condbroadcast
'   Mutexunlock
The critical sections of the threads are run synchronously one after the other, with a predefined order.

2.2) Synchronous algorithm using a mutex for each thread, by self lock and mutual unlock
When one thread has run its critical section, it unlocks the mutex of the next thread and attempts to re-obtain its own mutex.
At initialization all mutexes are locked, and the main thread enters directly in its critical section (code of the main thread slightly different of the other thread, with the self lock pushed at end).

Algorithm for main thread (#0):

Code: Select all

'   |  .....
'   |  Critical section of code
'   |  .....
'   Mutexunlock(next thread mutex (#0+1))
'   Mutexlock(own thread mutex (#0))
Algorithm for user thread (#N):

Code: Select all

'   Mutexlock(own thread mutex (#N))
'   |  .....
'   |  Critical section of code
'   |  .....
'   Mutexunlock(next thread mutex (#N+1))
The critical sections of the threads are run synchronously one after the other, with a predefined order.


See all the examples in the following post.
Last edited by fxm on Mar 20, 2019 15:49, edited 19 times in total.
fxm
Posts: 9086
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to Manage a Critical Section of the code of a Thread in FB

Postby fxm » Dec 14, 2015 19:18

Full examples of code for the previous post (paragraph: 'What kind of algorithm can be applied to handle critical sections?')

In the following examples, the shared resource is the input/output display device:
- Print its counter for each of 6 user threads (and read the flag 'quit').
- Catching a key-press (any one) for the main thread (and if yes, set the flag 'quit' to 'true').

The outputting procedure ('Sub Counter()') has voluntarily a tempo between cursor positioning and printing, and also a repositioning of text cursor at middle of line before ending, in order to thoroughly check that there is no overlap between the critical sections executions (at opposite, one can see the result by removing some code dedicated to exclusion processing).

- Example for §1.1.1: Asynchronous wait for expected condition, including a sleep in order to release CPU time

Code: Select all

' User thread algorithm (same principle for the main thread):
'
'   Do
'   |  my_thread_flag = true
'   |  Do
'   |  |  Sleep 5
'   |  Loop Until number_of_thread_flag_true = 1
'   |  .....
'   |  Critical section of code
'   |  .....
'   |  my_thread_flag = false
'   |  Sleep my_tempo
'   Loop Until quit = true
'
' This algorithm does not work because of the case of infinite blocking of the thread in its waiting loop (as soon as at least 2 threads wait at same time).


Type UDT
  Dim As Integer number
  Dim As Integer tempo
  Dim As Any Ptr pThread
  Dim As Ulongint count
  Static As Integer threadFlagArray(Any)
  Static As Integer numberMax
  Static As Integer quit
  Declare Static Function NumberOfThreadFlag () As Integer
End Type
Dim As Integer UDT.threadFlagArray(Any)
Dim As Integer UDT.numberMax
Dim As Integer UDT.quit
Static Function UDT.NumberOfThreadFlag () As Integer
  Dim As Integer n
  For I As Integer = 0 To UDT.numberMax
    n += UDT.threadFlagArray(I)
  Next I
  Return n
End Function

Sub Counter (Byval pt As UDT Ptr)
  With *pt
    Locate .number, .number, 0
    Sleep 5
    .count += 1
    Print .count;
    Locate .number, 30+.number, 0
  End With
End Sub

Sub Thread (Byval p As Any Ptr)
  Dim As Integer quit
  Dim As UDT Ptr pUDT = p
  With *pUDT
    Do
      .threadFlagArray(.number) = 1
      Do
        Sleep 5
      Loop Until .NumberOfThreadFlag() = 1
      Counter(pUDT)
      quit = .quit
      .threadFlagArray(.number) = 0
      Sleep .tempo
    Loop Until quit = 1
  End With
End Sub


UDT.numberMax = 6
Redim UDT.threadFlagArray(0 To UDT.numberMax)
Dim As UDT u(0 To UDT.numberMax)
For I As Integer = 0 To UDT.numberMax
  u(I).number = i
  u(I).tempo = 100 + 15 * I - 95 * Sgn(I)
Next I


Dim As Single t = Timer
For I As Integer = 1 To UDT.numberMax
  u(I).pThread = Threadcreate(@Thread, @u(I))
Next I

Dim As String s
Do
  UDT.threadFlagArray(u(0).number) = 1
  Do
    Sleep 5
  Loop Until UDT.NumberOfThreadFlag() = 1
  s = Inkey
  If s <> "" Then
    UDT.quit = 1
  End If
  UDT.threadFlagArray(u(0).number) = 0
  Sleep u(0).tempo
Loop Until s <> ""

For I As Integer = 1 To UDT.numberMax
  Threadwait(u(I).pThread)
Next I
t = Timer - t

Dim As Ulongint c
For I As Integer = 1 to UDT.numberMax
  c += u(I).count
Next I
Locate UDT.numberMax+2, 1
Print Culngint(c / t) & " increments per second"

Sleep
Blocking for that process!

- Example for §1.1.2: Asynchronous jump if not expected condition

Code: Select all

' User thread algorithm (same principle for the main thread):
'
'   Do
'   |  my_thread_flag = true
'   |  If number_of_thread_flag_true = 1 Then
'   |  | .....
'   |  | Critical section of code
'   |  | .....
'   |  End If
'   |  my_thread_flag = false
'   |  Sleep my_tempo
'   Loop Until quit = true
'
' There is no case of infinite blocking of the thread, but in general, some threads are strongly advantaged or disadvantaged compared to others for running their critical sections.


Type UDT
  Dim As Integer number
  Dim As Integer tempo
  Dim As Any Ptr pThread
  Dim As Ulongint count
  Static As Integer threadFlagArray(Any)
  Static As Integer numberMax
  Static As Integer quit
  Declare Static Function NumberOfThreadFlag () As Integer
End Type
Dim As Integer UDT.threadFlagArray(Any)
Dim As Integer UDT.numberMax
Dim As Integer UDT.quit
Static Function UDT.NumberOfThreadFlag () As Integer
  Dim As Integer n
  For I As Integer = 0 To UDT.numberMax
    n += UDT.threadFlagArray(I)
  Next I
  Return n
End Function

Sub Counter (Byval pt As UDT Ptr)
  With *pt
    Locate .number, .number, 0
    Sleep 5
    .count += 1
    Print .count;
    Locate .number, 30+.number, 0
  End With
End Sub

Sub Thread (Byval p As Any Ptr)
  Dim As Integer quit
  Dim As UDT Ptr pUDT = p
  With *pUDT
    Do
      .threadFlagArray(.number) = 1
      If .NumberOfThreadFlag() = 1 Then
        Counter(pUDT)
      End If
      quit =.quit
      .threadFlagArray(.number) = 0
      Sleep .tempo
    Loop Until quit = 1
  End With
End Sub


UDT.numberMax = 6
Redim UDT.threadFlagArray(0 To UDT.numberMax)
Dim As UDT u(0 To UDT.numberMax)
For I As Integer = 0 To UDT.numberMax
  u(I).number = i
  u(I).tempo = 100 + 15 * I - 95 * Sgn(I)
Next I

Dim As Single t = Timer
For I As Integer = 1 To UDT.numberMax
  u(I).pThread = Threadcreate(@Thread, @u(I))
Next I

Dim As String s
Do
  UDT.threadFlagArray(u(0).number) = 1
  If UDT.NumberOfThreadFlag() = 1 Then
    s = Inkey
    If s <> "" Then
      UDT.quit = 1
    End If
  End If
  UDT.threadFlagArray(u(0).number) = 0
  Sleep u(0).tempo
Loop Until s <> ""

For I As Integer = 1 To UDT.numberMax
  Threadwait(u(I).pThread)
Next I
t = Timer - t

Dim As Ulongint c
For I As Integer = 1 to UDT.numberMax
  c += u(I).count
Next I
Locate UDT.numberMax+2, 1
Print Culngint(c / t) & " increments per second"

Sleep

Code: Select all

407
 380
  243
   156
    153
     105

28 increments per second

- Example for §1.1.3: Asynchronous repeat awake then sleep, until expected condition

Code: Select all

' User thread algorithm (same principle for the main thread):
'
'   Do
'   |  Do
'   |  |  my_thread_flag = true
'   |  |  If number_of_thread_flag_true = 1 Then
'   |  |  |  Exit Do
'   |  |  End If
'   |  |  my_thread_flag = false
'   |  |  Sleep 5
'   |  Loop
'   |  .....
'   |  Critical section of code
'   |  .....
'   |  my_thread_flag = false
'   |  Sleep my_tempo
'   Loop Until quit = true
'
' There is no case of infinite blocking of the thread, but in general, some threads are strongly advantaged or disadvantaged compared to others for running their critical sections.


Type UDT
  Dim As Integer number
  Dim As Integer tempo
  Dim As Any Ptr pThread
  Dim As Ulongint count
  Static As Integer threadFlagArray(Any)
  Static As Integer numberMax
  Static As Integer quit
  Declare Static Function NumberOfThreadFlag () As Integer
End Type
Dim As Integer UDT.threadFlagArray(Any)
Dim As Integer UDT.numberMax
Dim As Integer UDT.quit
Static Function UDT.NumberOfThreadFlag () As Integer
  Dim As Integer n
  For I As Integer = 0 To UDT.numberMax
    n += UDT.threadFlagArray(I)
  Next I
  Return n
End Function

Sub Counter (Byval pt As UDT Ptr)
  With *pt
    Locate .number, .number, 0
    Sleep 5
    .count += 1
    Print .count;
    Locate .number, 30+.number, 0
  End With
End Sub

Sub Thread (Byval p As Any Ptr)
  Dim As Integer quit
  Dim As UDT Ptr pUDT = p
  With *pUDT
    Do
      Do
        .threadFlagArray(.number) = 1
        If .NumberOfThreadFlag() = 1 Then
          Exit Do
        End If
        .threadFlagArray(.number) = 0
        Sleep 5
      Loop
      Counter(pUDT)
      quit = .quit
      .threadFlagArray(.number) = 0
      Sleep .tempo
    Loop Until quit = 1
  End With
End Sub


UDT.numberMax = 6
Redim UDT.threadFlagArray(0 To UDT.numberMax)
Dim As UDT u(0 To UDT.numberMax)
For I As Integer = 0 To UDT.numberMax
  u(I).number = i
  u(I).tempo = 100 + 15 * I - 95 * Sgn(I)
Next I

Dim As Single t = Timer
For I As Integer = 1 To UDT.numberMax
  u(I).pThread = Threadcreate(@Thread, @u(I))
Next I

Dim As String s
Do
  Do
    UDT.threadFlagArray(u(0).number) = 1
    If UDT.NumberOfThreadFlag() = 1 Then
      Exit Do
    End If
    UDT.threadFlagArray(u(0).number) = 0
    Sleep 5
  Loop
  s = Inkey
  If s <> "" Then
    UDT.quit = 1
  End If
  UDT.threadFlagArray(u(0).number) = 0
  Sleep u(0).tempo
Loop Until s <> ""

For I As Integer = 1 To UDT.numberMax
  Threadwait(u(I).pThread)
Next I
t = Timer - t

Dim As Ulongint c
For I As Integer = 1 to UDT.numberMax
  c += u(I).count
Next I
Locate UDT.numberMax+2, 1
Print Culngint(c / t) & " increments per second"

Sleep

Code: Select all

416
 132
  283
   120
    242
     104

32 increments per second

- Example for §1.2: Asynchronous algorithm using one mutex for all threads

Code: Select all

' User thread algorithm (same principle for the main thread):
'
'   Do
'   |  Mutexlock
'   |  | .....
'   |  | Critical section of code
'   |  | .....
'   |  Mutexunlock
'   |  Sleep my_tempo
'   Loop Until quit = true
'
' There is no any advantage or disadvantage between threads for running their critical sections.


Type UDT
  Dim As Integer number
  Dim As Integer tempo
  Dim As Any Ptr pThread
  Dim As Ulongint count
  Static As Any Ptr pMutex
  Static As Integer numberMax
  Static As Integer quit
End Type
Dim As Any Ptr UDT.pMutex
Dim As Integer UDT.numberMax
Dim As Integer UDT.quit

Sub Counter (Byval pt As UDT Ptr)
  With *pt
    Locate .number, .number, 0
    Sleep 5
    .count += 1
    Print .count;
    Locate .number, 30+.number, 0
  End With
End Sub

Sub Thread (Byval p As Any Ptr)
  Dim As Integer quit
  Dim As UDT Ptr pUDT = p
  With *pUDT
    Do
      Mutexlock(.pMutex)
        Counter(pUDT)
        quit = .quit
      Mutexunlock(.pMutex)
      Sleep .tempo
    Loop Until quit = 1
  End With
End Sub


UDT.numberMax = 6
Dim As UDT u(0 To UDT.numberMax)
For I As Integer = 0 To UDT.numberMax
  u(I).number = i
  u(I).tempo = 100 + 15 * I - 95 * Sgn(I)
Next I
UDT.pMutex = Mutexcreate

Dim As Single t = Timer
For I As Integer = 1 To UDT.numberMax
  u(I).pThread = Threadcreate(@Thread, @u(I))
Next I

Dim As String s
Do
  Mutexlock(UDT.pMutex)
    s = Inkey
    If s <> "" Then
      UDT.quit = 1
    End If
  Mutexunlock(UDT.pMutex)
  Sleep u(0).tempo
Loop Until s <> ""

For I As Integer = 1 To UDT.numberMax
  Threadwait(u(I).pThread)
Next I
t = Timer - t

Mutexdestroy(UDT.pMutex)
Dim As Ulongint c
For I As Integer = 1 to UDT.numberMax
  c += u(I).count
Next I
Locate UDT.numberMax+2, 1
Print Culngint(c / t) & " increments per second"

Sleep

Code: Select all

220
 188
  147
   130
    114
     104

62 increments per second

- Example for §2.1.1: Synchronous wait for expected condition, including a sleep in order to release CPU time

Code: Select all

' User thread algorithm (same principle for the main thread):
'
'   Do
'   |  While thread_priority_number <> my_number
'   |  |  Sleep 5
'   |  Wend
'   |  .....
'   |  Critical section of code
'   |  .....
'   |  thread_priority_number = next thread_priority_number
'   |  Sleep my_tempo
'   Loop Until quit = true
'
' The critical sections of the threads are run synchronously one after the other, with a predefined order.


Type UDT
  Dim As Integer number
  Dim As Integer tempo
  Dim As Any Ptr pThread
  Dim As Ulongint count
  Static As Integer threadPriorityNumber
  Static As Integer numberMax
  Static As Integer quit
End Type
Dim As Integer UDT.threadPriorityNumber
Dim As Integer UDT.numberMax
Dim As Integer UDT.quit

Sub Counter (Byval pt As UDT Ptr)
  With *pt
    Locate .number, .number, 0
    Sleep 5
    .count += 1
    Print .count;
    Locate .number, 30+.number, 0
  End With
End Sub

Sub Thread (Byval p As Any Ptr)
  Dim As Integer quit
  Dim As UDT Ptr pUDT = p
  With *pUDT
    Do
      While .threadPriorityNumber <> .number
        Sleep 5
      Wend
      Counter(pUDT)
      quit = .quit
      .threadPriorityNumber = (.threadPriorityNumber + 1) Mod (.numberMax+1)
      Sleep .tempo
    Loop Until quit = 1
  End With
End Sub


UDT.numberMax = 6
Dim As UDT u(0 To UDT.numberMax)
For I As Integer = 0 To UDT.numberMax
  u(I).number = i
  u(I).tempo = 100 + 15 * I - 95 * Sgn(I)
Next I

Dim As Single t = Timer
For I As Integer = 1 To UDT.numberMax
  u(I).pThread = Threadcreate(@Thread, @u(I))
Next I

Dim As String s
Do
  While UDT.threadPriorityNumber <> u(0).number
    Sleep 5
  Wend
  s = Inkey
  If s <> "" Then
    UDT.quit = 1
  End If
  UDT.threadPriorityNumber = (UDT.threadPriorityNumber + 1) Mod (UDT.numberMax+1)
  Sleep u(0).tempo
Loop Until s <> ""

For I As Integer = 1 To UDT.numberMax
  Threadwait(u(I).pThread)
Next I
t = Timer - t

Dim As Ulongint c
For I As Integer = 1 to UDT.numberMax
  c += u(I).count
Next I
Locate UDT.numberMax+2, 1
Print Culngint(c / t) & " increments per second"

Sleep

Code: Select all

106
 106
  106
   106
    106
     106

31 increments per second

- Example for §2.1.2: Synchronous wait for expected condition, including a condwait then a condbroadcast (and mutex) in order to release CPU time

Code: Select all

' User thread algorithm (same principle for the main thread):
'
'   Do
'   |  Mutexlock
'   |  |  While thread_priority_number <> my_number
'   |  |  |  Condwait
'   |  |  Wend
'   |  |  .....
'   |  |  Critical section of code
'   |  |  .....
'   |  |  thread_priority_number = next thread_priority_number
'   |  |  Condbroadcast
'   |  Mutexunlock
'   |  Sleep my_tempo
'   Loop Until quit = true
'
' The critical sections of the threads are run synchronously one after the other, with a predefined order.


Type UDT
  Dim As Integer number
  Dim As Integer tempo
  Dim As Any Ptr pThread
  Dim As Ulongint count
  Static As Integer threadPriorityNumber
  Static As Any Ptr pMutex
  Static As Any Ptr pCond
  Static As Integer numberMax
  Static As Integer quit
End Type
Dim As Integer UDT.threadPriorityNumber
Dim As Any Ptr UDT.pMutex
Dim As Any Ptr UDT.pCond
Dim As Integer UDT.numberMax
Dim As Integer UDT.quit

Sub Counter (Byval pt As UDT Ptr)
  With *pt
    Locate .number, .number, 0
    Sleep 5
    .count += 1
    Print .count;
    Locate .number, 30+.number, 0
  End With
End Sub

Sub Thread (Byval p As Any Ptr)
  Dim As Integer quit
  Dim As UDT Ptr pUDT = p
  With *pUDT
    Do
      Mutexlock(.pMutex)
        While .threadPriorityNumber <> .number  '' synchronous condwait for expected condition
          Condwait(.pCond, .pMutex)
        Wend
        Counter(pUDT)
        quit = .quit
        .threadPriorityNumber = (.threadPriorityNumber + 1) Mod (.numberMax+1)
        Condbroadcast(.pCond)
      Mutexunlock(.pMutex)
      Sleep .tempo
    Loop Until quit = 1
  End With
End Sub


UDT.numberMax = 6
Dim As UDT u(0 To UDT.numberMax)
For I As Integer = 0 To UDT.numberMax
  u(I).number = i
  u(I).tempo = 100 + 15 * I - 95 * Sgn(I)
Next I
UDT.pMutex = Mutexcreate
UDT.PCond = Condcreate

Dim As Single t = Timer
For I As Integer = 1 To UDT.numberMax
  u(I).pThread = Threadcreate(@Thread, @u(I))
Next I

Dim As String s
Do
  Mutexlock(UDT.pMutex)
    While UDT.threadPriorityNumber <> u(0).number
      Condwait(UDT.pCond, UDT.pMutex)
    Wend
    s = Inkey
    If s <> "" Then
      UDT.quit = 1
    End If
    UDT.threadPriorityNumber = (UDT.threadPriorityNumber + 1) Mod (UDT.numberMax+1)
    Condbroadcast(UDT.pCond)
  Mutexunlock(UDT.pMutex)
  Sleep u(0).tempo
Loop Until s <> ""

For I As Integer = 1 To UDT.numberMax
  Threadwait(u(I).pThread)
Next I
t = Timer - t

Mutexdestroy(UDT.pMutex)
Conddestroy(UDT.pCond)
Dim As Ulongint c
For I As Integer = 1 to UDT.numberMax
  c += u(I).count
Next I
Locate UDT.numberMax+2, 1
Print Culngint(c / t) & " increments per second"

Sleep

Code: Select all

105
 105
  105
   105
    105
     105

48 increments per second
Compared to previous example, adding 'condwait' and 'condbroadcast' (and mutex) increases the complexity but also improves the execution time.

Example for §2.2: Synchronous algorithm using a mutex for each thread, by self lock and mutual unlock

Code: Select all

' Main thread (#0) algorithm:
'
'   Do
'   |  |  .....
'   |  |  Critical section of code
'   |  |  .....
'   |  Mutexunlock(next thread mutex (#0+1))
'   |  Mutexlock(own thread mutex (#0))
'   |  Sleep my_tempo
'   Loop Until key <> ""

' User thread (#N) algorithm:
'
'   Do
'   |  Mutexlock(own thread mutex (#N))
'   |  |  .....
'   |  |  Critical section of code
'   |  |  .....
'   |  Mutexunlock(next thread mutex (#N+1))
'   |  Sleep tempo
'   Loop Until quit = 1


Type UDT
  Dim As Integer number
  Dim As Integer tempo
  Dim As Any Ptr pThread
  Dim As Ulongint count
  Static As Any Ptr pMutex(Any)
  Static As Integer numberMax
  Static As Integer quit
End Type
Dim As Any Ptr UDT.pMutex(Any)
Dim As Integer UDT.numberMax
Dim As Integer UDT.quit

Sub Counter (Byval pt As UDT Ptr)
  With *pt
    Locate .number, .number, 0
    Sleep 5
    .count += 1
    Print .count;
    Locate .number, 30+.number, 0
  End With
End Sub

Sub Thread (Byval p As Any Ptr)
  Dim As Integer quit
  Dim As UDT Ptr pUDT = p
  With *pUDT
    Do
      Mutexlock(.pMutex(.number))
      Counter(pUDT)
      quit = .quit
      Mutexunlock(.pMutex((.number +1) Mod (UDT.numberMax+1)))
      Sleep .tempo
    Loop Until quit = 1
  End With
End Sub


UDT.numberMax = 6
Redim UDT.pMutex(UDT.numberMax)
Dim As UDT u(0 To UDT.numberMax)
For I As Integer = 0 To UDT.numberMax
  u(I).number = i
  u(I).tempo = 100 + 15 * I - 95 * Sgn(I)
  UDT.pMutex(I) = Mutexcreate
  Mutexlock(UDT.pMutex(I))
Next I

Dim As Single t = Timer
For I As Integer = 1 To UDT.numberMax
  u(I).pThread = Threadcreate(@Thread, @u(I))
Next I

Dim As String s
Do
    s = Inkey
    If s <> "" Then
      UDT.quit = 1
    End If
  Mutexunlock(UDT.pMutex((u(0).number +1) Mod (UDT.numberMax+1)))
  Mutexlock(UDT.pMutex(u(0).number))
  Sleep u(0).tempo
Loop Until s <> ""

For I As Integer = 1 To UDT.numberMax
  Threadwait(u(I).pThread)
Next I
t = Timer - t

For I As Integer = 0 To UDT.numberMax
  Mutexdestroy(UDT.pMutex(I))
Next I
Dim As Ulongint c
For I As Integer = 1 to UDT.numberMax
  c += u(I).count
Next I
Locate UDT.numberMax+2, 1
Print Culngint(c / t) & " increments per second"

Sleep

Code: Select all

103
 103
  103
   103
    103
     103

28 increments per second
The worst execution time among the 3 synchronous algorithms!
Last edited by fxm on May 10, 2018 11:16, edited 7 times in total.
fxm
Posts: 9086
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to Manage a Critical Section of the code of a Thread in FB

Postby fxm » Dec 15, 2015 20:55

What happens if calling 'Condsignal()' or 'Condbroadcast()' without mutex locked?

About the above example for §2.1.2, I take this opportunity to recall that:
- The mutex must always be also locked while executing 'Condsignal()' or 'Condbroadcast()' to wake up a thread (it may be unlocked but only after 'Condsignal()' or 'Condbroadcast()').
- If the mutex is not locked (or even if the mutex is unlocked only just before executing 'Condsignal()' or 'Condbroadcast()'), the behavior may become unpredictable (it may work or not, depending on the threads configuration and execution real time).

In this above example for §2.1.2:
- If one at least 'Mutexunlock()' is moved just before its 'Condbroadcast()', the program hangs very quickly.
- Although some users certify that the mutex can always be unlocked just before 'Condsignal()' or 'Condbroadcast()', and others more cautious assert that one can do it only for a 'Condbroadcast()', experiment shows the opposite!

The general rule is that:
- The condition must not be signaled (by 'Condsignal()' or 'Condbroadcast()') between the time a thread locks the mutex and the time it waits on the condition variable ('Condwait()'), otherwise it seems that it may damage the waiting queue of threads on that condition variable.
- Thus to avoid that and follow this rule, it is necessary that the mutex remains locked when the condition is signaled.
Last edited by fxm on May 10, 2018 8:09, edited 1 time in total.
fxm
Posts: 9086
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to Manage a Critical Section of the code of a Thread in FB

Postby fxm » May 13, 2016 16:51

When is it not mandatory to protect by a Mutex one shared variable between several threads?

When accessing to shared variables between several threads, all their accesses must be generally put inside blocks [Mutexlock ..... Mutexunlock], in all threads:
- When the shared variable is only one simple predefined numeric type of size <= sizeof(integer) (only one assembler instruction for access), Mutex use may be not mandatory.
- But if this is for example one shared variable LongInt with a win32 compilation, it is advised here to use a Mutex (otherwise the reading phase by a thread may be interlaced with the writing phase of another thread).

That is because to access a variable in memory (for reading or for writing), a processor uses its internal registers.
A N-bit processor has N-bit registers but none greater:
- So one only assembler instruction allows it to access a N-bit variable in memory.
- at opposite, to access a 2N-bit variable, it must use 2 assembler instructions.
- If between these two assembler instructions (for writing), another thread accesses this same variable (for reading), the got value may be incoherent (N-bit highest and N-bit lowest incoherent together).

This behavior can be checked with a graphic program using two threads and a shared LongInt (64-bit) without Mutex:
- by compiling in 32-bit, many read values are incoherent.
- by compiling in 64-bit, no read value is incoherent.

Compile the below test program:
- in 32-bit => many erroneous points not on the circle but anywhere in the square containing the circle (if you uncomment the four lines 33/35/53/55 to activate the Mutex, then all the got points are now on the circle only),
- in 64-bit => all points are valid, on the circle only, even if the Mutex is not activated.

Code: Select all

'- The "user-defined thread" computes the points coordinates on a circle,
'  and write those in a LongInt (32-bit & 32-bit = 64-bit)
'- The "main thread" plots the points from the LongInt value.
'
'Behavior:
'- The first point must be pre-determined.
'- Nothing prevents that a same calculated point could be plotted several times
'(depends on execution times of the loops between main thread and user thread).
'- Nothing prevents that a calculated point could be not plotted
'(same remark on the loop times).
'
'Remark:
'Voluntarily, there is no Sleep in the loop of each thread (normally strongly discouraged),
'but this is just in this special case to amplify the behavior effects to observe.


Union Point2D
    Dim As Longint xy
    Type
        Dim As Long y
        Dim As Long x
    End Type
End Union

Dim As Any Ptr handle
Dim Shared As Any Ptr mutex
Dim Shared As Integer quit

Sub Thread (ByVal param As Any Ptr)
    Const pi As Single = 4 * Atn(1)
    Dim As Point2D Ptr p = param
    Do
        Dim As Point2D P2D0
        Dim As Single teta = 2 * pi * Rnd
        P2D0.x = 320 + 200 * Cos(teta)
        P2D0.y = 240 + 200 * Sin(teta)
'        Mutexlock(mutex)
        p->xy = P2D0.xy
'        Mutexunlock(mutex)
'        Sleep 5
    Loop Until quit = 1
End Sub


Screen 12

Dim As Point2D P2D
P2D.x = 520
P2D.y = 240

mutex = MutexCreate
handle = ThreadCreate(@Thread, @P2D)

Dim As Integer c

Do
    Dim As Point2D P2D0
'    Mutexlock(mutex)
    P2D0.xy = P2D.xy
'    Mutexunlock(mutex)
    PSet (P2D0.x, P2D0.y), c
    c = (c Mod 15) + 1
'    Sleep 5
Loop Until Inkey <> ""
 
quit = 1
ThreadWait(handle)
Mutexdestroy(mutex)
Last edited by fxm on May 10, 2018 8:09, edited 1 time in total.
fxm
Posts: 9086
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to Manage a Critical Section of the code of a Thread in FB

Postby fxm » May 19, 2016 16:46

Why it is mandatory to put CondWait within a While loop for checking a Boolean predicate (set by other thread before activate CondSignal or CondBroadcast)?

Code: Select all

While predicate <> true
  Condwait(handle, mutex)
Wend

In all documentations, it is highly advisable to do so, mainly justified to fight against eventual spurious wake-ups.

This is probably true, but it is also advisable to do so to avoid to loose a CondSignal (or CondBroadcast) if it is prematurely activated while the receiving thread is not yet waiting on CondWait (the signal is lost forever):
- In that case, the receiving thread has even not yet locked the mutex before that CondSignal (or CondBroadcast) is activated.
- So the predicate will already true before the receiving thread reaches the While loop, inducing that CondWait is downright skipped, so avoiding a definitive blocking phenomenon.

Let two threads (thread #0 in main program, thread #1 in a user procedure, each that prints its number in a loop), having about the same execution time, and each one synchronizing the other in order to well interlace their numbers (by using one mutex, two condition variables and CondSignal/CondWait):

- Without a While loop on predicate, the program hangs quickly (Ctrl-C to quit):

Code: Select all

'          Thread#0               XOR + <==>             Thread#1
'.....                                          .....
'MutexLock(mut)                                 MutexLock(mut)
'  Do_something_with_exclusion                    Do_something_with_exclusion
'  CondWait(cond#1, mut) <----------------------- CondSignal(cond#1)
'  Do_something_with_exclusion <---------.        Do_something_with_exclusion
'  CondSignal(cond#2) ------------------ | -----> CondWait(cond#2, mut)
'  Do_something_with_exclusion     .---- | -----> Do_something_with_exclusion
'MutexUnlock(mut) -----------------'     '----- MutexUnlock(mut)
'.....                                          .....


Dim As Any Ptr handle
Dim Shared As Any Ptr mutex
Dim Shared As Any Ptr cond1
Dim Shared As Any Ptr cond2
Dim Shared As Integer quit

Sub Thread (ByVal param As Any Ptr)
    Do
        Mutexlock(mutex)
        Print "1";
        CondSignal(cond1)
        CondWait(cond2, mutex)
        If quit = 1 Then
            Mutexunlock(mutex)
            Exit DO
        End If
        Mutexunlock(mutex)
        Sleep 1
    Loop
End Sub


mutex = MutexCreate
cond1 = CondCreate
cond2 = CondCreate
handle = ThreadCreate(@Thread)

Do
    Mutexlock(mutex)
    CondWait(cond1, mutex)
    Print "0";
    CondSignal(cond2)
    If Inkey <> "" Then
        quit = 1
        Mutexunlock(mutex)
        Exit Do
    End If
    Mutexunlock(mutex)
    Sleep 1
Loop
 
ThreadWait(handle)
Mutexdestroy(mutex)
CondDestroy(cond1)
CondDestroy(cond2)
Print

Sleep

- With a While loop on predicate around each CondWait, no blocking phenomenon:

Code: Select all

'          Thread#0               XOR + <==>             Thread#1
'.....                                          .....
'MutexLock(mut)                                 MutexLock(mut)
'  Do_something_with_exclusion                    Do_something_with_exclusion
'  While bool#1 <> true <------------------------ bool#1 = true
'    CondWait(cond#1, mut) <--------------------- CondSignal(cond#1)
'  Wend <-----------------------------------.     Do_something_with_exclusion
'  bool#1 = false               .---------- | --> While bool#2 <> true
'  Do_something_with_exclusion  |   .------ | ----> CondWait(cond#2, mut)
'  bool#2 = true ---------------'   |   .-- | --> Wend
'  CondSignal(cond#2) --------------'   |   |     bool#2 = false
'  Do_something_with_exclusion          |   |     Do_something_with_exclusion
'MutexUnlock(mut) ----------------------'   '-- MutexUnlock(mut)
'.....                                          .....


Dim As Any Ptr handle
Dim Shared As Any Ptr mutex
Dim Shared As Any Ptr cond1
Dim Shared As Any Ptr cond2
Dim Shared As Integer new1
Dim Shared As Integer new2
Dim Shared As Integer quit

Sub Thread (ByVal param As Any Ptr)
    Do
        Mutexlock(mutex)
        Print "1";
        new1 = 1
        CondSignal(cond1)
        While new2 <> 1
            CondWait(cond2, mutex)
        Wend
        new2 = 0
        If quit = 1 Then
            Mutexunlock(mutex)
            Exit DO
        End If
        Mutexunlock(mutex)
        Sleep 1
    Loop
End Sub


mutex = MutexCreate
cond1 = CondCreate
cond2 = CondCreate
handle = ThreadCreate(@Thread)

Do
    Mutexlock(mutex)
    While new1 <> 1
        CondWait(cond1, mutex)
    Wend
    new1 = 0
    Print "0";
    new2 = 1
    CondSignal(cond2)
    If Inkey <> "" Then
        quit = 1
        Mutexunlock(mutex)
        Exit Do
    End If
    Mutexunlock(mutex)
    Sleep 1
Loop
 
ThreadWait(handle)
Mutexdestroy(mutex)
CondDestroy(cond1)
CondDestroy(cond2)
Print

Sleep
Last edited by fxm on May 10, 2018 8:09, edited 1 time in total.
fxm
Posts: 9086
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to Manage a Critical Section of the code of a Thread in FB

Postby fxm » May 20, 2016 20:39

What is the chronology of code execution of 2 critical sections (with a mutex locking and a conditional variable signaling) that compete between 2 threads?

Chronology for one thread signaling which occurs:
1) while another thread is waiting (within a While loop on predicate),
2) before another thread is waiting (within a While loop on predicate).

Code: Select all

#define while_loop_on_predicate

Dim As Any Ptr handle
Dim Shared As Any Ptr mutex
Dim Shared As Any Ptr cond
Dim As Integer sleep0
Dim As Integer sleep1
#ifdef while_loop_on_predicate
Dim Shared As Integer ready
#endif


Sub Thread1 (ByVal param As Any Ptr)
  Sleep *Cast(Integer Ptr, param)
  Mutexlock(mutex)
  Color 11 : Print "    Thread#1 locks the mutex"
  Color 11 : Print "    Thread#1 executes code with exclusion"
  #ifdef while_loop_on_predicate
  ready = 1
  #endif
  Color 11 : Print "    Thread#1 is signaling"
  CondSignal(cond)
  Color 11 : Print "    Thread#1 executes post-code with exclusion"
  Color 11 : Print "    Thread#1 unlocks the mutex"
  Mutexunlock(mutex)
End Sub

Sub Thread0 (ByVal param As Any Ptr)
  Sleep *Cast(Integer Ptr, param)
  Mutexlock(mutex)
  Color 10 : Print "  Thread#0 locks the mutex"
  Color 10 : Print "  Thread#0 executes pre-code with exclusion"
  #ifdef while_loop_on_predicate
  While ready <> 1
  #endif
    Color 10 : Print "  Thread#0 is waiting"
    CondWait(cond, mutex)
    Color 10 : Print "  Thread#0 is waked"
  #ifdef while_loop_on_predicate
  Wend
  #endif
  Color 10 : Print "  Thread#0 executes code with exclusion"
  #ifdef while_loop_on_predicate
  ready = 0
  #endif
  Color 10 : Print "  Thread#0 unlocks the mutex"
  Mutexunlock(mutex)
End Sub


mutex = MutexCreate
cond = CondCreate

sleep0 = 0
sleep1 = 1000
Color 7 : Print "Chronology for Thread#1 signaling while Thread#0 is waiting:"
handle = ThreadCreate(@Thread1, @sleep1)
Thread0(@sleep0)
ThreadWait(handle)
Color 7 : Print "Thread#1 finished": Print
Sleep 1000

sleep0 = 1000
sleep1 = 0
Color 7 : Print "Chronology for Thread#1 signaling before Thread#0 is waiting:"
handle = ThreadCreate(@Thread1, @sleep1)
Thread0(@sleep0)
ThreadWait(handle)
Color 7 : Print "Thread#1 finished": Print


Mutexdestroy(mutex)
CondDestroy(cond)
Sleep
Output part 1 - Chronology for Thread#1 signaling while Thread#0 is waiting:

Code: Select all

Chronology for Thread#1 signaling while Thread#0 is waiting:
  Thread#0 locks the mutex
  Thread#0 executes pre-code with exclusion
  Thread#0 is waiting
    Thread#1 locks the mutex
    Thread#1 executes code with exclusion
    Thread#1 is signaling
    Thread#1 executes post-code with exclusion
    Thread#1 unlocks the mutex
  Thread#0 is waked
  Thread#0 executes code with exclusion
  Thread#0 unlocks the mutex
Thread#1 finished
Output part 2 - Chronology for Thread#1 signaling before Thread#0 is waiting:

Code: Select all

Chronology for Thread#1 signaling before Thread#0 is waiting:
    Thread#1 locks the mutex
    Thread#1 executes code with exclusion
    Thread#1 is signaling
    Thread#1 executes post-code with exclusion
    Thread#1 unlocks the mutex
  Thread#0 locks the mutex
  Thread#0 executes pre-code with exclusion
  Thread#0 executes code with exclusion
  Thread#0 unlocks the mutex
Thread#1 finished

If CondWait is not within a While loop on predicate (by putting in comment the first line of above program), one can check in the second case (thread#1 signaling before thread#0 waiting), that thread#0 remains blocked in its waiting phase (Ctrl-C to quit).
Last edited by fxm on May 10, 2018 8:10, edited 1 time in total.
fxm
Posts: 9086
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to Manage a Critical Section of the code of a Thread in FB

Postby fxm » Oct 16, 2017 12:33

User input-line function, but fully thread-safe!

The "Input" keyword may be not thread-safe, when another thread must also access to input/output resource:
- When executing the "Input" statement, the other running threads must not change the position of the text cursor, which prohibits instructions such as "Locate", "Print", ... Only graphic instructions using the position of the graphic cursor, including "Draw String", are compatible during this time.
- Moreover, we cannot enclosed the "Input" keyword inside a mutex locking (as we can do it for the "Inkey" keyword), because while the inputting line would be not completed and validated, the other threads that want to also access to input/output would be fully blocked (waiting for mutex unlocking).

Thread-safe input-line function (versus input/output resource)
Input position, prompt message, sleeping time, line-blanking command, mutex pointer can be passed to the following "threadInput()" function that simulates a simplified input function, but thread-safe, by using a looping around the "Inkey" keyword (all input/output keywords must be enclosed inside a mutex locking block, and the cursor position must be restored at each mutex locking block ending):

Code: Select all

Function threadInput (Byval row As Integer, Byval column As Integer, Byref prompt As String = "", Byval sleeptime As Integer = 15, Byval blank As Integer = 0, Byval mutex As Any Ptr = 0) As String
  Dim As Integer r
  Dim As Integer c
  Dim As String inputchr
  Dim As String inputline
 
  Mutexlock(mutex)
    r = Csrlin()
    c = Pos()
    Locate row, column
    Print prompt & "? _";
    Locate r, c
  Mutexunlock(mutex)
 
  Do
    Mutexlock(mutex)
      r = Csrlin()
      c = Pos()
      Locate row, column + Len(inputline) + Len(prompt) + 2
      inputchr = Inkey
      If Len(inputchr) = 1 Then
        If Asc(inputchr) >= 32 Then
          Print inputchr & Chr(95);
          Locate , pos - 1
          inputline &= inputchr
        Elseif Asc(inputchr) = 08 and Len(inputline) > 0 Then
          Locate , pos - 1
          Print chr(95) & " ";
          Locate , Pos() - 2
          inputline = Left(inputline, Len(inputline) - 1)
        End If
      End If
      Locate r, c
    Mutexunlock(mutex)
    Sleep sleeptime
  Loop Until inputchr = Chr(13)
 
  If blank <> 0 Then
    Mutexlock(mutex)
      r = Csrlin()
      c = Pos()
      Locate row, column + Len(prompt) + 2
      Print Space(Len(inputline) + 1);
      Locate r, c
    Mutexunlock(mutex)
  End If
 
  Return inputline
End Function

From the previous code "Example for §1.2: Asynchronous algorithm using one mutex for all threads", now the running multi-threading code is waiting for the "quit" command in order to exit the program:

Code: Select all

' User thread algorithm:
'
'   Do
'   |  Mutexlock
'   |  | .....
'   |  | Critical section of code
'   |  | .....
'   |  Mutexunlock
'   |  Sleep my_tempo
'   Loop Until quit = true
'
' There is no any advantage or disadvantage between threads for running their critical sections.


Function threadInput (Byval row As Integer, Byval column As Integer, Byref prompt As String = "", Byval sleeptime As Integer = 15, Byval blank As Integer = 0, Byval mutex As Any Ptr = 0) As String
  Dim As Integer r
  Dim As Integer c
  Dim As String inputchr
  Dim As String inputline
 
  Mutexlock(mutex)
    r = Csrlin()
    c = Pos()
    Locate row, column
    Print prompt & "? _";
    Locate r, c
  Mutexunlock(mutex)
 
  Do
    Mutexlock(mutex)
      r = Csrlin()
      c = Pos()
      Locate row, column + Len(inputline) + Len(prompt) + 2
      inputchr = Inkey
      If Len(inputchr) = 1 Then
        If Asc(inputchr) >= 32 Then
          Print inputchr & Chr(95);
          Locate , pos - 1
          inputline &= inputchr
        Elseif Asc(inputchr) = 08 and Len(inputline) > 0 Then
          Locate , pos - 1
          Print chr(95) & " ";
          Locate , Pos() - 2
          inputline = Left(inputline, Len(inputline) - 1)
        End If
      End If
      Locate r, c
    Mutexunlock(mutex)
    Sleep sleeptime
  Loop Until inputchr = Chr(13)
 
  If blank <> 0 Then
    Mutexlock(mutex)
      r = Csrlin()
      c = Pos()
      Locate row, column + Len(prompt) + 2
      Print Space(Len(inputline) + 1);
      Locate r, c
    Mutexunlock(mutex)
  End If
 
  Return inputline
End Function


Type UDT
  Dim As Integer number
  Dim As Integer tempo
  Dim As Any Ptr pThread
  Dim As Ulongint count
  Static As Any Ptr pMutex
  Static As Integer numberMax
  Static As Integer quit
End Type
Dim As Any Ptr UDT.pMutex
Dim As Integer UDT.numberMax
Dim As Integer UDT.quit

Sub Counter (Byval pt As UDT Ptr)
  With *pt
    Locate .number, .number, 0
    Sleep 5
    .count += 1
    Print .count;
  End With
End Sub

Sub Thread (Byval p As Any Ptr)
  Dim As Integer quit
  Dim As UDT Ptr pUDT = p
  With *pUDT
    Do
      Mutexlock(.pMutex)
        Counter(pUDT)
        quit = .quit
      Mutexunlock(.pMutex)
      Sleep .tempo
    Loop Until quit = 1
  End With
End Sub


Screen 12
UDT.numberMax = 6

Dim As UDT u(0 To UDT.numberMax)
For I As Integer = 0 To UDT.numberMax
  u(I).number = i
  u(I).tempo = 100 + 15 * I - 95 * Sgn(I)
Next I
UDT.pMutex = Mutexcreate

Dim As Single t = Timer
For I As Integer = 1 To UDT.numberMax
  u(I).pThread = Threadcreate(@Thread, @u(I))
Next I

Do
Loop Until Lcase(threadInput(8, 1, """quit"" for exit", 10, 1, UDT.pMutex)) = "quit"

UDT.quit = 1

For I As Integer = 1 To UDT.numberMax
  Threadwait(u(I).pThread)
Next I
t = Timer - t

Mutexdestroy(UDT.pMutex)
Dim As Ulongint c
For I As Integer = 1 to UDT.numberMax
  c += u(I).count
Next I
Locate UDT.numberMax + 4, 1
Print Culngint(c / t) & " increments per second"

Sleep
fxm
Posts: 9086
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to Manage a Critical Section of the code of a Thread in FB

Postby fxm » Mar 21, 2019 20:15

Beware when using SCREENLOCK with multi-threading!

- [ScreenLock...ScrennUnlock] blocks are not compatible with multi-threading (otherwise, the program hangs). This is why a mutex block must be used around each such block to ensure the exclusion.
- The input keywords (like for keyboard, mouse) cannot be safely run when the screen is locked, therefore a such keyword must be outside of any [Screenlock...Screenunlock] block, so outside any [Screenlock...Screenunlock] block in its own thread, and protected of all [Screenlock...Screenunlock] blocks of other threads by a mutex block. Therefore, Getkey and Input, the statements that wait for keypress or line input are unusable, but Inkey that does not wait can work.

By applying some rules scrupulously, one can use ScreenLock/Unlock inside the threads.
Principle of coding for all threads including the main code (main thread):

Code: Select all

Do
  ' instructions without display (printing/drawing, ...) neither input (input/inkey/mouse getting, ...)
  MutexLock(m)
    Screenlock
      ' instructions with only display (printing/drawing, ...)
    Screenunlock
    ' instructions with only input without waiting (inkey/mouse getting, ...)
  MutexUnlock(m)
  Sleep tempo
Loop Until condition

For example, it is mandatory to use one [Mutexlock...Mutexunlock] block around each [Screenlock...Screenunlock] block, and one other around the "Inkey" instruction which itself must always be outside of any [Screenlock...Screenunlock] bloc:

Code: Select all

Type ThreadUDT
  Dim handle As Any Ptr
  Static sync As Any Ptr
  Static quit As Byte
End Type
Dim ThreadUDT.sync As Any Ptr
Dim ThreadUDT.quit As Byte

Function ClockTime () As String
  Return Time
End Function

Function Counter () As Integer
  Static C As Integer
  C = (C + 1) MOD 1000000
  Return C
End Function

Sub ProcedureThread (Byval param As Any Ptr)
  With *Cast(ThreadUDT Ptr, param)
    Do
      MutexLock(.sync)
        Screenlock
          Line (544, 0)-(639, 49), 0, BF  'clear the print area
          Sleep 100
          Locate 2, 71
          Print ClockTime();
        Screenunlock
      MutexUnlock(.sync)
      Sleep 100
    Loop Until .quit = 1
  End With
End Sub

Screen 12
Locate 30, 2
Print "<q/Q> : quit";

Dim TTptr As ThreadUDT Ptr = New ThreadUDT
ThreadUDT.sync = MutexCreate
TTptr->handle = ThreadCreate(@ProcedureThread, TTptr)

Dim As String s
Do
  MutexLock(ThreadUDT.sync)
    Screenlock
      Line (296, 208)-(376, 256), 0, BF  'clear the print area
      Sleep 100
      Locate 15,40
      Print Using "######"; Counter();
    Screenunlock
    s = Inkey
  MutexUnlock(ThreadUDT.sync)
  Sleep 100
Loop Until Lcase(s) = "q"
 
ThreadUDT.quit = 1
ThreadWait(TTptr->handle)
MutexDestroy(ThreadUDT.sync)
Delete TTptr
    Note: The Sleep keyword just after the 'clear the print area' lines is only here to highlight the flickering if no screen locking is used.
fxm
Posts: 9086
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to Manage a Critical Section of the code of a Thread in FB

Postby fxm » Mar 21, 2019 21:26

Beware when using "video paging (double buffering or page flipping)" with multi-threading!

Instead of "screen locking" (see the above post), "video paging (double buffering or page flipping)" can more simply be used with multi-threading, but be careful that many states in the gfxlib2 are thread-dependent like SCREENSET (and also VIEW settings, graphic cursor position, graphic colors, ...).
Therefore, the setting for the working page and the visible page must always be controlled in each thread code which want to work with a multi-video page configuration.

- Example for a double buffering method (at each step, each thread needs to update the working page and copy it to the visible page, from within an exclusion mutex code block):

Code: Select all

Type ThreadUDT
  Dim handle As Any Ptr
  Static sync As Any Ptr
  Static quit As Byte
End Type
Dim ThreadUDT.sync As Any Ptr
Dim ThreadUDT.quit As Byte

Function ClockTime () As String
  Return Time
End Function

Function Counter () As Integer
  Static C As Integer
  C = (C + 1) MOD 1000000
  Return C
End Function

Sub ProcedureThread (Byval param As Any Ptr)
  Screenset 1, 0  '' setting to define in each thread
  With *Cast(ThreadUDT Ptr, param)
    Do
      MutexLock(.sync)
        Line (544, 0)-(639, 49), 0, BF  '' clear the print area
        Sleep 100
        Locate 2, 71
        Print ClockTime();
        Screencopy
      MutexUnlock(.sync)
      Sleep 100
    Loop Until .quit = 1
  End With
End Sub

Screen 12, , 2
Screenset 1, 0  '' setting to define in each thread
Locate 30, 2
Print "<q/Q> : quit";
Screencopy

Dim TTptr As ThreadUDT Ptr = New ThreadUDT
ThreadUDT.sync = MutexCreate
TTptr->handle = ThreadCreate(@ProcedureThread, TTptr)

Dim s As String
Do
  MutexLock(ThreadUDT.sync)
    Line (296, 208)-(376, 256), 0, BF  '' clear the print area
    Sleep 100
    Locate 15,40
    Print Using "######"; Counter();
    Screencopy
    s = Inkey
  MutexUnlock(ThreadUDT.sync)
  Sleep 100
Loop Until Lcase(s) = "q"
 
ThreadUDT.quit = 1
ThreadWait(TTptr->handle)
MutexDestroy(ThreadUDT.sync)
Delete TTptr
    Note: The Sleep keyword just after the 'clear the print area' lines is only here to highlight the flickering if no double buffering is used.
- Example for a two page flipping method (at each step, each thread needs to update and flip, from within the same exclusion mutex code block, the two screen pages):

Code: Select all

Type ThreadUDT
  Dim handle As Any Ptr
  Static sync As Any Ptr
  Static quit As Byte
End Type
Dim ThreadUDT.sync As Any Ptr
Dim ThreadUDT.quit As Byte

Function ClockTime () As String
  Return Time
End Function

Function Counter () As Integer
  Static C As Integer
  C = (C + 1) MOD 1000000
  Return C
End Function

Sub ProcedureThread (Byval param As Any Ptr)
  Dim p0 As Integer = 0
  Dim p1 As Integer = 1
  Screenset 1, 0  '' setting to define in each thread
  With *Cast(ThreadUDT Ptr, param)
    Do
      MutexLock(.sync)
        Dim s As String = ClockTime()
        For I As Integer = 1 To 2  '' updating the two screen pages
          Line (544, 0)-(639, 49), 0, BF  '' clear the print area
          Sleep 100
          Locate 2, 71
          Print s;
          Screenset p0, p1
          Swap p0, p1
        Next I
      MutexUnlock(.sync)
      Sleep 100
    Loop Until .quit = 1
  End With
End Sub

Screen 12, , 2
Dim p0 As Integer = 0
Dim p1 As Integer = 1
Screenset 1, 0  '' setting to define in each thread
For I As Integer = 1 To 2  '' updating the two screen pages
  Locate 30, 2
  Print "<q/Q> : quit";
  Screenset p0, p1
  Swap p0, p1
Next I

Dim TTptr As ThreadUDT Ptr = New ThreadUDT
ThreadUDT.sync = MutexCreate
TTptr->handle = ThreadCreate(@ProcedureThread, TTptr)

Dim s As String
Do
  MutexLock(ThreadUDT.sync)
    Dim C As Integer = Counter()
    For I As Integer = 1 To 2  '' updating the two screen pages
      Line (296, 208)-(376, 256), 0, BF  '' clear the print area
      Sleep 100
      Locate 15,40
      Print Using "######"; c;
      Screenset p0, p1
      Swap p0, p1
    Next I
    s = Inkey
  MutexUnlock(ThreadUDT.sync)
  Sleep 100
Loop Until Lcase(s) = "q"
 
ThreadUDT.quit = 1
ThreadWait(TTptr->handle)
MutexDestroy(ThreadUDT.sync)
Delete TTptr
    Note: The Sleep keyword just after the 'clear the print area' lines is only here to highlight the flickering if no two page flipping is used.
Note: In these two examples, an exclusion mutex code block is mandatory in the two threads, not only because of using console statements + Inkey, but around also the graphics statements + Screencopy only because of using double buffering method (without anti-flickering process, the graphics statements could be outside the exclusion mutex code block).
fxm
Posts: 9086
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to Manage a Critical Section of the code of a Thread in FB

Postby fxm » Mar 23, 2019 20:31

When using the FB runtime library for multi-threaded applications, gfxlib2 is thread-safe :-)

The source code of gfxlib2 uses TLS (Thread Local Storage) to store many states, so many things are thread-specific.
Since gfxlib2 is thread-safe, mutex exclusion between threads is not necessary for the graphics statements themselves (including Draw String).
In contrast, console statements such as Locate, Print, ... are not thread-safe as previously mentioned (for example, text cursor position is common to all threads).

- Simple example showing that graphic states (such as graphic cursor position, graphic colors) are thread-dependent:

Code: Select all

Screen 12

Sub thread(Byval p As Any Ptr)
  Color 10
  Pset(150, 10)
  For I As Integer = 1 To 40
    Line -Step(10, 10)
    Sleep 150
  Next I
  Draw String Step (-40, 10), "user thread"
End Sub

Dim As Any Ptr p = Threadcreate(@thread)

Color 14
Pset(10, 100)
For I As Integer = 1 To 24
  Line -Step(10, 10)
  Sleep 250
Next I
Draw String Step (-40, 10), "main thread"

Threadwait(p)

Color 15
Locate 4, 2
Print "Any key for exit"

Sleep

- Example showing that graphics statements (such as Line and Draw String and Screencopy) in a thread can compete with console statements (such as Inkey) in another thread, without using any exclusion (by mutex):

Code: Select all

#include "vbcompat.bi"

Screen 12, , 2
Screenset 1, 0   
Color 0, 7
Cls

Dim Shared terminate As Integer = 0

Sub thread (byval param As Any Ptr)   
  Screenset 1, 0
  Do
    Line (16, 432)-Step(96, 32), 11, BF  'clear print area
    Sleep 100
    Draw String (24, 432), Format(Now,"dd/mm/yyyy"), 0
    Draw String (32, 448), Format(Now,"hh:mm:ss"), 0
    Screencopy
    Sleep 100
  Loop Until terminate = 1
End Sub

Dim As String reply
Locate 2, 2
Print "Enter ""q"" to quit"
Screencopy

Dim p As Any Ptr = ThreadCreate(@thread)

Do
  reply = Inkey
  Sleep 100
Loop Until Lcase(reply) = "q"

Print " Stop the thread"
Screencopy
terminate=1
Threadwait (p)
Print " Thread terminated"
Screencopy

Sleep
    Note: The Sleep keyword just after the 'clear the print area' line is only here to highlight the flickering if no double buffering is used.
- From the above example, if the date displaying and the time displaying are now two separate threads, an exclusion mutex code block between these two threads is mandatory, not due to the graphics statements themselves competing, but only due to the double buffering method used (against flickering) that puts competing these two threads:

Code: Select all

#include "vbcompat.bi"

Screen 12, , 2
Screenset 1, 0   
Color 0, 7
Cls

Dim Shared terminate As Integer = 0
Dim Shared mutex As Any Ptr

Sub thread1 (byval param As Any Ptr)   
  Screenset 1, 0
  Do
    Mutexlock(mutex)
      Line (16, 432)-Step(96, 16), 11, BF  'clear the print area
      Sleep 200
      Draw String (24, 432), Format(Now,"dd/mm/yyyy"), 0
      Screencopy
    Mutexunlock(mutex)
    Sleep 100
  Loop Until terminate = 1
End Sub

Sub thread2 (byval param As Any Ptr)   
  Screenset 1, 0
  Do
    Mutexlock(mutex)
      Line (16, 448)-Step(96, 16), 11, BF  'clear the print area
      Sleep 100
      Draw String (32, 448), Format(Now,"hh:mm:ss"), 0
      Screencopy
    Mutexunlock(mutex)
    Sleep 100
  Loop Until terminate = 1
End Sub

Dim As String reply
Locate 2, 2
Print "Enter ""q"" to quit"
Screencopy

mutex = Mutexcreate
Dim p1 As Any Ptr = ThreadCreate(@thread1)
Dim p2 As Any Ptr = ThreadCreate(@thread2)

Do
  reply = Inkey
  Sleep 100
Loop Until Lcase(reply) = "q"

Print " Stop the threads"
Screencopy
terminate=1
Threadwait (p1)
Threadwait (p2)
Mutexdestroy(mutex)
Print " Threads terminated"
Screencopy

Sleep
    Note: The Sleep keyword just after the 'clear the print area' lines is only here to highlight the flickering if no double buffering is used, or if no mutex is used.
fxm
Posts: 9086
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to Manage a Critical Section of the code of a Thread in FB

Postby fxm » Mar 26, 2019 16:49

How to use console statements and keyboard inputs with multi-threading?

  • Console statements (such as Locate, Print, Color, ...), as well as Locate and Print on Graphics window (but not Color on Graphics Window), and keyboard inputs (such as Inkey, Getkey, Input, ...) are not thread-safe:
    • Thus when they are used in competing sections of different threads, mutual exclusion is mandatory by means of mutex locking blocks in which in addition code can restore states (such as text cursor position, console color, ...) at end of the block (after its own usage), as they were before (at begin of the block).
    • But the GetKey or Input keyword cannot be enclosed inside a mutex locking block (as it can be do with the Inkey keyword), because as long as the keyboard input is not completed, the other threads in compete would be fully blocked (waiting for the mutex unlocking).
    - Example showing that the keywords Locate and Print are not thread-safe both when applied on a console window or when applied on a graphics window (the text cursor states being not thread dependent in the two cases):

    Code: Select all

    Sub Thread (Byval p As Any Ptr)
      Locate Cast(Integer, p), Cast(Integer, p)
      For I As Integer = 1 To 50 - 2 * Cast(Integer, p)
        Sleep 20 * Cast(Integer, p)
        Print Str(Cast(Integer, p));
      Next I
    End Sub

    Sub test ()
      Dim As Any Ptr p(1 To 9)
      For I As Integer = 1 To 9
        p(I) = Threadcreate(@Thread, Cast(Any Ptr, I))
        Sleep 25
      Next I
      For I As Integer = 1 To 9
        Threadwait(p(I))
      Next I
    End Sub

    Screen 0
    test()
    Locate 15, 1
    Print "Any key to continue"
    Sleep

    Screen 12
    test()
    Locate 15, 1
    Print "Any key to quit"
    Sleep
      Note: One can see that each thread does not write on its own line corresponding to its thread number (id between 1 and 9), on the console window and on the graphics window.
    - From the above example, the thread code has been completed in its competing sections by mutex locking blocks and by saving/restoring cursor states before/after its own cursor moving:

    Code: Select all

    Dim Shared As Any Ptr mutex

    Sub Thread (Byval p As Any Ptr)
      Mutexlock(mutex)
        Dim As Long l0 = Locate()
        Locate Cast(Integer, p), Cast(Integer, p)
        Dim As Long l = Locate()
        Locate Hibyte(Loword(l0)), Lobyte(Loword(l0)), Hiword(l0)
      Mutexunlock(mutex)
      For I As Integer = 1 To 50 - 2 * Cast(Integer, p)
        Sleep 20 * Cast(Integer, p)
        Mutexlock(mutex)
          l0 = Locate()
          Locate Hibyte(Loword(l)), Lobyte(Loword(l)), Hiword(l)
          Print Str(Cast(Integer, p));
          l = Locate()
          Locate Hibyte(Loword(l0)), Lobyte(Loword(l0)), Hiword(l0)
        Mutexunlock(mutex)
      Next I
    End Sub

    Sub test ()
      Dim As Any Ptr p(1 To 9)
      For I As Integer = 1 To 9
        p(I) = Threadcreate(@Thread, Cast(Any Ptr, I))
        Sleep 25
      Next I
      For I As Integer = 1 To 9
        Threadwait(p(I))
      Next I
    End Sub

    mutex = Mutexcreate

    Screen 0
    test()
    Locate 15, 1
    Print "Any key to continue"
    Sleep

    Screen 12
    test()
    Locate 15, 1
    Print "Any key to quit"
    Sleep

    Mutexdestroy(mutex)
      Note: One can see that each thread writes now on its own line corresponding to its thread number (id between 1 and 9), on the console window and on the graphics window.
    - Example showing that the Color keyword is not thread-safe when applied on a console window, but is thread-safe when applied on a graphics window (the color states being thread dependent in that case):

    Code: Select all

    Sub Thread (Byval p As Any Ptr)
      Color Cast(Integer, p) + 8, Cast(Integer, p)
      For I As Integer = 1 To 50 - 2 * Cast(Integer, p)
        Print " " & Cast(Integer, p) & " ";
        Sleep 20 * Cast(Integer, p)
      Next I
    End Sub

    Sub test ()
      Dim As Any Ptr p(1 To 9)
      Locate 1, 1
      For I As Integer = 1 To 9
        p(I) = Threadcreate(@Thread, Cast(Any Ptr, I))
        Sleep 25
      Next I
      For I As Integer = 1 To 9
        Threadwait(p(I))
      Next I
      Locate 16, 1
    End Sub

    Screen 0
    test()
    Print "Any key to continue"
    Sleep

    Screen 12
    test()
    Print "Any key to quit"
    Sleep
      Note: One can see that the foreground/background colors are not specific to the thread number (id between 1 and 9) on the console window, but this works great on the graphics window.
    - From the above example, the thread code has been completed in its competing sections by mutex locking blocks and by saving/restoring color states before/after its own color values usage:

    Code: Select all

    Dim Shared As Any Ptr mutex

    Sub Thread (Byval p As Any Ptr)
      Mutexlock(mutex)
        Dim As Ulong c0 = Color(Cast(Integer, p) + 8, Cast(Integer, p))
        Dim As Ulong c = Color()
        Color(Loword(c0), Hiword(c0))
      Mutexunlock(mutex)
      For I As Integer = 1 To 50 - 2 * Cast(Integer, p)
        Mutexlock(mutex)
          c0 = Color(Loword(c), Hiword(c))
          Print " " & Cast(Integer, p) & " ";
          Color(Loword(c0), Hiword(c0))
        Mutexunlock(mutex)
        Sleep 20 * Cast(Integer, p)
      Next I
    End Sub

    Sub test ()
      Dim As Any Ptr p(1 To 9)
      Locate 1, 1
      For I As Integer = 1 To 9
        p(I) = Threadcreate(@Thread, Cast(Any Ptr, I))
        Sleep 25
      Next I
      For I As Integer = 1 To 9
        Threadwait(p(I))
      Next I
      Locate 16, 1
    End Sub

    mutex = Mutexcreate

    Screen 0
    test()
    Print "Any key to continue"
    Sleep

    Screen 12
    test()
    Print "Any key to quit"
    Sleep

    Mutexdestroy(mutex)
      Note: One can see that the foreground/background colors are now specific to the thread number (id between 1 and 9) on the console window (obviously this always works on the graphics window).
  • Therefore, for using Getkey or Input in competing sections of threads:
    • Only a single thread (for example, the main thread) can uses Getkey or Input in addition to console statements (such as Locate, Print, Color, ...) and also Inkey, in its competing sections.
    • The other threads must not to use in their competing sections any console statement neither any keyboard input keyword, but can use by cons graphics statements (such as Pset, Line, Circle, Draw String, graphic Color, ...) which are themselves thread-safe (they can interlace graphically with the main thread without any problem).
    • Input and Getkey also exclude the screen locking usage in competing sections of threads (double buffering is recommended as anti-flickering method).
    - Example showing that graphics statements (such as Line and Draw String and Screencopy) in a thread (user thread here) can compete with console statements (such as Locate and Print and Input) in another thread (main thread here), without using any exclusion (by mutex):

    Code: Select all

    #include "vbcompat.bi"

    Screen 12, , 2
    Screenset 1, 0   
    Color 0, 7
    Cls

    Dim Shared terminate As Integer = 0

    Sub thread (byval param As Any Ptr)   
      Screenset 1, 0
      Do
        Line (16, 432)-Step(96, 32), 11, BF  'clear the print area
        Sleep 100
        Draw String (24, 432), Format(Now,"dd/mm/yyyy"), 0
        Draw String (32, 448), Format(Now,"hh:mm:ss"), 0
        Screencopy
        Sleep 100
      Loop Until terminate = 1
    End Sub

    Dim As String reply
    Locate 2, 2
    Print "Enter ""quit"" to quit"
    Screencopy

    Dim p As Any Ptr = ThreadCreate(@thread)

    Do
      Locate 3, 2
      Print Space(Len(reply) + 2);
      Locate 3, 2
      Input reply
    Loop Until Lcase(reply) = "quit"

    Print " Stop the thread"
    Screencopy
    terminate=1
    Threadwait (p)
    Print " Thread terminated"
    Screencopy

    Sleep
      Note: The Sleep keyword just after the 'clear the print area' line is only here to highlight the flickering if no double buffering is used (screen locking being forbidden by Input usage).
    - From the above example, if the date displaying and the time displaying are now two separate user threads, an exclusion mutex code block between these two threads only is mandatory, not due to the graphics statements themselves competing, but only due to the double buffering method used (against flickering) that puts competing these two user threads only:

    Code: Select all

    #include "vbcompat.bi"

    Screen 12, , 2
    Screenset 1, 0   
    Color 0, 7
    Cls

    Dim Shared terminate As Integer = 0
    Dim Shared mutex As Any Ptr

    Sub thread1 (byval param As Any Ptr)   
      Screenset 1, 0
      Do
        Mutexlock(mutex)
          Line (16, 432)-Step(96, 16), 11, BF  'clear the print area
          Sleep 200
          Draw String (24, 432), Format(Now,"dd/mm/yyyy"), 0
          Screencopy
        Mutexunlock(mutex)
        Sleep 100
      Loop Until terminate = 1
    End Sub

    Sub thread2 (byval param As Any Ptr)   
      Screenset 1, 0
      Do
        Mutexlock(mutex)
          Line (16, 448)-Step(96, 16), 11, BF  'clear the print area
          Sleep 100
          Draw String (32, 448), Format(Now,"hh:mm:ss"), 0
          Screencopy
        Mutexunlock(mutex)
        Sleep 100
      Loop Until terminate = 1
    End Sub

    Dim As String reply
    Locate 2, 2
    Print "Enter ""quit"" to quit"
    Screencopy

    mutex = Mutexcreate
    Dim p1 As Any Ptr = ThreadCreate(@thread1)
    Dim p2 As Any Ptr = ThreadCreate(@thread2)

    Do
      Locate 3, 2
      Print Space(Len(reply) + 2);
      Locate 3, 2
      Input reply
    Loop Until Lcase(reply) = "quit"

    Print " Stop the threads"
    Screencopy
    terminate=1
    Threadwait (p1)
    Threadwait (p2)
    Mutexdestroy(mutex)
    Print " Threads terminated"
    Screencopy

    Sleep
      Note: The Sleep keyword just after the 'clear the print area' lines is only here to highlight the flickering if no double buffering is used (screen locking being forbidden by Input usage).

Return to “Documentation”

Who is online

Users browsing this forum: Google [Bot] and 14 guests