Good evening fellow stackers, I have a mutex and threaded related question about
// ¦ = Run one or the other per call to routine
|GROUP A| GROUP B |
| A & B GROUP MUTEX |
|=======|============|
|A1 & A2¦ B1 & |B2|B3|
|MUTEX|
|==|==|
|B2¦B3|
Situation
Is this situation safe or viable?
I have 5 sub routines in VB.NET and I can run them in 2 groups comprising of seperate subsets in an asynchronous threaded manner.
However, as the horribly rough text-o-graph above shows, the mutex's work on the subset B as well.
Logic
Group A and B have to wait for the eachother if the jobs have started on either - A & B GROUP MUTEX
helps keep this in check.
Inside Group A both can routines begin concurrently (B would have to wait for both A1 and A2 to be complete).
Group B, with B1, B2 and B3 can run B1 at the same time as either B2 and B3 but B2 and B3 cannot run concurrently, hence the subset MUTEX
check.
Update:
@pstrjds made a good point with using Sync-locking instead: The reason I am using Mutex's and not Sync Lock is due to the fact that I need to make this process multi user safe as within the sub routines are a lot of SQL and data based operations which required the program to be locked whilst multiple SQL-Transactions are carried out on different databases across 3 different servers to update them concurrently and safely - transaction handling in SQL is done and working well hence the broken down psuedo-code.
@pstrjds is correct in saying that this is using threads spawned off the one class, however within the subroutines it calls to a web service (part of Group B's work in B2 and B3) which updates a separate 3rd party server.
</Wall-o-Text>
Psuedo-code
Unfortunately the exact code is rather long but I am using this structure:
Sub AGroup_Method()
Dim bln_FirstInstance As Boolean
Using objABMutex As New Mutex(True, "Global\AB_MutexLock", blnFirstInstance)
If bln_FirstInstance Then
//Start Threads for subroutine A1 And then A2
StartThread_A1()
StartThread_A2()
Else
//Post that Group A subroutine needs to wait for Group B
End If
End Using
End Sub
Sub BGroup_Method(Byval p_blnRunBTwo as Boolean)
Dim bln_FirstInstance As Boolean
Dim blnBGroup_FirstInstance As Boolean
Using objABMutex As New Mutex(True, "Global\AB_MutexLock", bln_FirstInstance)
If bln_FirstInstance Then
//Do subroutine group B
//Start B1
StartThread_B1()
Using objBGroupMutex As New Mutex(True, "Global\BGroup_MutexLock", blnBGroup_FirstInstance)
If p_blnRunBTwo
If blnBGroup_FirstInstance Then
//Wait for mutex from B3 and then run B2
StartThread_B2
End If
Else
If blnBGroup_FirstInstance Then
StartThread_B3
End If
End If
End Using
Else
//Post that Group B subroutine needs to wait for Group A
End If
End Using
End Sub
Help!
What I would like to ask is if the mutex nesting is a possible problem, if it is bad practice whatnots and is there a better way of implementing this using a threaded system.