Mutex : question from a novice

Just starting out? Need help? Post your questions and find answers here.
tatanas
Enthusiast
Enthusiast
Posts: 260
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Mutex : question from a novice

Post by tatanas »

Hi,

Look at this sample :

Code: Select all

Global close = #False
Global mutex = CreateMutex()


Procedure mythread(*val)
	Repeat
		If close
			Debug "thread " + *val + " received close message"
			Break
		EndIf
		Delay(1)
	ForEver
	
	Debug "thread " + *val + " closed"
EndProcedure


If OpenWindow(0, 0, 0, 400, 200, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	
	For i = 1 To 100
		CreateThread(@mythread(), i)
	Next
	
	Repeat
		Event = WindowEvent()
		Select Event
			Case #PB_Event_CloseWindow
				close = #True
				Break
		EndSelect
		Delay(1)
	ForEver
	
	Delay(5000)
	Debug "end"
	
EndIf
Should I create a mutex to use the "close" global variable safely ?


Thanks for your time.
Windows 10 Pro x64
PureBasic 6.20 x64
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Mutex : question from a novice

Post by NicTheQuick »

No, you don't need a mutex for a single variable of a native number data type when you only setting static values, because in this case there is always only a single read or write instruction on CPU level.
You need a mutex for strings or for 64 bit variables if you compile a 32 bit application, or when you use structured variables. You also need a mutex if you do calculations with such a variable, because then multiple reads and writes can happen.

Also you have a mistake in your event loop. You are doing a `Delay(1)` after each event which is really bad because there are always a lot of events happening that should be handled as fast as possible. You should either use `WaitWindowEvent(1)` or only doing the delay when `Event = 0`.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
tatanas
Enthusiast
Enthusiast
Posts: 260
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Re: Mutex : question from a novice

Post by tatanas »

Okay so if I understand correctly in the next example I have to use a mutex :

Code: Select all

Global close = #False
Global counter = 0
Global mutex = CreateMutex()


Procedure mythread(*val)
	
	Repeat
		LockMutex(mutex)
		counter = counter + 1
		UnlockMutex(mutex)
		
		If close
			Debug "thread " + *val + " received close message"
			Break
		EndIf
		Delay(1)
	ForEver
	
	Debug "thread " + *val + " closed"
EndProcedure


If OpenWindow(0, 0, 0, 400, 200, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	
	For i = 1 To 100
		CreateThread(@mythread(), i)
	Next
	
	Repeat
		Event = WindowEvent()
		Select Event
			Case #PB_Event_CloseWindow
				close = #True
				Break
				
			Case #PB_Event_None
				Delay(1)
		EndSelect
	ForEver
	
	Delay(5000)
	Debug "counter = " + Str(counter)
	
EndIf
Thanks for the tip with delay(1) and PB_Event.
Windows 10 Pro x64
PureBasic 6.20 x64
infratec
Always Here
Always Here
Posts: 7664
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Mutex : question from a novice

Post by infratec »

You can also use:

Code: Select all

Event = WaitWindowEvent(10)
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Mutex : question from a novice

Post by NicTheQuick »

tatanas wrote: Sat Dec 23, 2023 9:03 amOkay so if I understand correctly in the next example I have to use a mutex :
Exactly.
tatanas wrote: Sat Dec 23, 2023 9:03 amThanks for the tip with delay(1) and PB_Event.
You're welcome. Also see what infratec wrote.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Mutex : question from a novice

Post by Oso »

Hello tatanas

A recent thread in November on the subject of atomicity, may be of interest to you, at the below link — which refers to several cases of variables being atomic, therefore in some instances not requiring a mutex.

https://www.purebasic.fr/english/viewtopic.php?t=82805
Post Reply