Client/Server + Mutex

Windows specific forum
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Client/Server + Mutex

Post by eriansa »

Server :

Code: Select all

Structure RWMemory
  mutex.l
  Transport.b
  SampleRate.l
  DevSamplePos.l
  PanSamplePos.l
  DevBufferPos.l
  PanBufferPos.l
  SamplesToRender.l
EndStructure

OpenConsole()     
*myRWMemory.RWMemory = AllocateMemoryGlobal("SQ4",SizeOf(RWMemory))
If *myRWMemory
  *myRWMemory\mutex = CreateMutex_(0,1,"SQ4_Rewire")
  WaitForSingleObject_(*myRWMemory\mutex,10000) ; Lock it
  CommandLine.s =  "c:\rewiresdk\sq4\client.exe"
  StartupInfo.STARTUPINFO 
  StartupInfo\cb = SizeOf(STARTUPINFO) 
  StartupInfo\dwFlags = #STARTF_USESHOWWINDOW 
  StartupInfo\wShowWindow = #SW_SHOW
  ProcessInfo.PROCESS_INFORMATION 
  CreateProcess_(0, @CommandLine, 0, 0, 0, 0, 0, 0, @StartupInfo, @ProcessInfo)
EndIf
PrintN("Press return to release mutex and exit") 
Input() 
CloseConsole() 
CloseHandle_(*myRWMemory\mutex)
FreeMemoryGlobal(*myRWMemory)

Client :

Code: Select all

Structure RWMemory
  mutex.l
  Transport.b
  SampleRate.l
  DevSamplePos.l
  PanSamplePos.l
  DevBufferPos.l
  PanBufferPos.l
  SamplesToRender.l
EndStructure

OpenConsole()
*myRWMemory.RWMemory = AllocateMemoryGlobal("SQ4",SizeOf(RWMemory))
If *myRWMemory\mutex
  PrintN("Waiting...") 
  WaitForSingleObject_(*myRWMemory\mutex,#INFINITE) ; Lock it
Else
  PrintN("No mutex to wait for...")
EndIf
PrintN("Press return to exit") 
Input() 
CloseConsole() 
Problem :
Client is NOT waiting for mutex...
Someone any idea?

Thx.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

This is what I would do. I think you need to open the mutex, having only the handle value won't work.

Code: Select all

*myRWMemory.RWMemory = AllocateMemoryGlobal("SQ4", SizeOf(RWMemory))
hMutex = OpenMutex_(#MUTEX_ALL_ACCESS, #TRUE, "SQ4_Rewire")
If hMutex=*myRWMemory\mutex
  PrintN("Waiting...")
  While WaitForSingleObject_(hMutex, #INFINITE)<>#WAIT_OBJECT_0:Delay(10):Wend
Else
  PrintN("No mutex to wait for...")
EndIf
El_Choni
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Post by eriansa »

El_Choni wrote:This is what I would do. I think you need to open the mutex, having only the handle value won't work.

Code: Select all

*myRWMemory.RWMemory = AllocateMemoryGlobal("SQ4", SizeOf(RWMemory))
hMutex = OpenMutex_(#MUTEX_ALL_ACCESS, #TRUE, "SQ4_Rewire")
If hMutex=*myRWMemory\mutex
  PrintN("Waiting...")
  While WaitForSingleObject_(hMutex, #INFINITE)<>#WAIT_OBJECT_0:Delay(10):Wend
Else
  PrintN("No mutex to wait for...")
EndIf
I understand, but what's the use of having a [While Wend] loop when WaitForSingleObject_ is waiting #INFINITE ?
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

ur right, sry,took the code from a snippet which didn't wait for infinite.
El_Choni
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

What is 'AllocateMemoryGlobal'? Besides old :-)

Also, what is the value of '#MUTEX_ALL_ACCESS'?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
JCV
Enthusiast
Enthusiast
Posts: 580
Joined: Fri Jun 30, 2006 4:30 pm
Location: Philippines

Post by JCV »

Code: Select all

Procedure AllocateMemoryGlobal(Name.s,Size.l)
  Shared HandleMap.l
  HandleMap.l = CreateFileMapping_($FFFFFFFF, 0, 402653188, 0, Size, @Name)
  If HandleMap
    ProcedureReturn MapViewOfFile_(HandleMap, 983071, 0, 0 ,0)
  EndIf
EndProcedure

Procedure FreeMemoryGlobal(MemoryAddress.l)
  Shared HandleMap.l
  UnmapViewOfFile_(MemoryAddress)
  CloseHandle_(HandleMap)
EndProcedure
#MUTEX_ALL_ACCESS = $1F0001 from http://www.purebasic.fr/english/viewtopic.php?t=27439

[Registered PB User since 2006]
[PureBasic 6.20][SpiderBasic 2.2]
[RP4 x64][Win 11 x64][Ubuntu x64]
Post Reply