Semaphore issue with setgadgetstate on imagegadget

Windows specific forum
Lucifer
User
User
Posts: 26
Joined: Wed Apr 30, 2003 6:11 pm
Location: UK

Semaphore issue with setgadgetstate on imagegadget

Post by Lucifer »

I'm trying to get my head around mutex's and semaphore's and I thought I had it until my project came to a halt when trying to to use setgadgetstate to change the image of an imagegadget.
Code just locks up but does not crash.

Every other command I've used in a semaphore so far has had no problem and all works as expected.
I've put some sample code together to demonstrate the issue.

Code: Select all

Global Window_0
Global Image_0, Button_0, Button_1, Button_2, Picture_0, index
Global filename.s = "D:\Program Files\PureBasic 5.22 x64\Examples\Sources\Data\PureBasicLogo.bmp"
Global testMutex = CreateMutex()
Global testSemaphore = CreateSemaphore()

Procedure testProblem(junk.l)
  Shared testMutex, testSemaphore, Picture_0, Image_0, filename
  LockMutex(testMutex)
  
  LoadImage(Picture_0, filename)
  SetGadgetState(Image_0, ImageID(Picture_0))
  a = 2 + 2
  
  UnlockMutex(testMutex)
  SignalSemaphore(testSemaphore)
EndProcedure

Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "Semaphore Test", #PB_Window_SystemMenu)
  Image_0 = ImageGadget(#PB_Any, 10, 10, 580, 320, 0, #PB_Image_Border)
  Button_0 = ButtonGadget(#PB_Any, 10, 340, 180, 50, "Load Image (Main Thread)", #PB_Button_MultiLine)
  Button_1 = ButtonGadget(#PB_Any, 210, 340, 180, 50, "Clear Image")
  Button_2 = ButtonGadget(#PB_Any, 410, 340, 180, 50, "Load Image (Semaphore)", #PB_Button_MultiLine)
EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
        Case Button_0
          LoadImage(Picture_0, filename)
          SetGadgetState(Image_0, ImageID(Picture_0))
        Case Button_1
          SetGadgetState(Image_0, 0)
        Case Button_2
          For index = 1 To 10
            If CreateThread(@testProblem(), 0)
              WaitSemaphore(testSemaphore)
              Debug "index = " + Str(index)
            EndIf
          Next index
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

OpenWindow_0()

While Window_0_Events(WaitWindowEvent()) : Delay(1): Wend
If you comment out the following line from the testProblem routine all works fine.

Code: Select all

SetGadgetState(Image_0, ImageID(Picture_0))
Can anyone shed some light on what I may be doing wrong?


I'm use 5.22LTS on Windows 8.1 x64 using the x86 compiler with thread safe enabled.
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Semaphore issue with setgadgetstate on imagegadget

Post by idle »

Im not on windows but from what I can see you've essentially locked up the event queue with the waitsemaphore
so it can't process the setgadgetstate.
Windows 11, Manjaro, Raspberry Pi OS
Image
Lucifer
User
User
Posts: 26
Joined: Wed Apr 30, 2003 6:11 pm
Location: UK

Re: Semaphore issue with setgadgetstate on imagegadget

Post by Lucifer »

Thanks for the reply idle.

If you could run the code on a Windows machine and you commented out the SetGadgetState command then you get all 10 debugs of index as expected. The only time the semaphore seems to lock is when the command SetGadgetState is used within a semaphore.

If when creating the semaphore I use the option to set say 2 for the initial count then it only runs twice and then locks.

In the semaphore you can put a debug directly before and another debug directly after the SetGadgetState and only the debug directly before the SetGadgetState appears in the debug window. To me this shows that it's actually the SetGadgetState that is causing the lock as nothing else in the semaphore is being processed.
Post Reply