Is StartVectorDrawing() thread safe on Mac?

Just starting out? Need help? Post your questions and find answers here.
User avatar
robertfern
User
User
Posts: 38
Joined: Fri Feb 02, 2018 10:33 pm
Location: New Jersey

Is StartVectorDrawing() thread safe on Mac?

Post by robertfern »

I have a small program that draws the Recamån series on a Canvas Gadget.
The drawing routines are in a thread. A loop occurs to draw each segment followed by a StopVectorDrawing().
On the Mac, the drawings of each segment don't appear until the entire thread is finished.
I can post the entire program if needed.

Was wondering is StartVectorDrawing() thread safe on Mac?
Mac OSX Ventura & Windows 10, PB 6.12
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Is StartVectorDrawing() thread safe on Mac?

Post by infratec »

This is in the wrong section. It is a coding question.
Or what exactly is the bug :?:
User avatar
robertfern
User
User
Posts: 38
Joined: Fri Feb 02, 2018 10:33 pm
Location: New Jersey

Re: Is StartVectorDrawing() thread safe on Mac?

Post by robertfern »

The bug is, it doesn't redraw till the thread completes. It should redraw after every StartVectorDrawing()-StopVectorDrawing() loop

Haven't tested on PC yet

** EDIT ** Tested on PC, It works great!
Mac OSX Ventura & Windows 10, PB 6.12
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Is StartVectorDrawing() thread safe on Mac?

Post by mk-soft »

Had to try something to update the NSView from Thread.
But this has to be done from the main thread. But it works with a PostEvent.

Update
- Added semaphore because crashed is the thread too fast

Code: Select all

;-TOP

#ProgramTitle = "Main Window"
#ProgramVersion = "v1.01.2"

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
  #MainMenuStart
EndEnumeration

Enumeration Gadgets
  #MainCanvas
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

; ----

Enumeration #PB_EventType_FirstCustomValue
  #MyEventType_Update
EndEnumeration

Procedure thWork(*data)
  Protected pool
  Protected semaphore, x, y
  
  Pool = CocoaMessage(0, 0, "NSAutoreleasePool new")
  
  semaphore = CreateSemaphore()
  
  For x = 10 To 500 Step 40
    For y = 10 To 300 Step 40
      If StartDrawing(CanvasOutput(#MainCanvas))
        Box(x + 2, y + 2, 36, 36, #Blue)
        Delay(50) ; Sim slow drawing
        StopDrawing()
      EndIf
      PostEvent(#PB_Event_Gadget, #Main, #MainCanvas, #MyEventType_Update, semaphore)
      WaitSemaphore(semaphore)
    Next
  Next
  
  FreeSemaphore(semaphore)
  
  If Pool
    CocoaMessage(0, Pool, "release")
  EndIf
  
EndProcedure

; ----

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainCanvas, 0, 0, dx, dy)
EndProcedure

Procedure Main()
  Protected dx, dy
  Protected ThreadID
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      MenuItem(#PB_Menu_About, "")
    CompilerElse
      MenuItem(#MainMenuAbout, "About")
    CompilerEndIf
    ; Menu File Items
    MenuItem(#MainMenuStart, "Start Drawing")
    
    CompilerIf Not #PB_Compiler_OS = #PB_OS_MacOS
      MenuBar()
      MenuItem(#MainMenuExit, "E&xit")
    CompilerEndIf
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    CanvasGadget(#MainCanvas, 0, 0, dx, dy)
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    ;DumpObjectMethods(GadgetID(#MainCanvas), 1)
    
    ; Event Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
              CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
              CompilerEndIf
              
            Case #MainMenuAbout
              MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
            Case #MainMenuExit
              PostEvent(#PB_Event_CloseWindow, #Main, #Null)
              
            Case #MainMenuStart
              If Not IsThread(ThreadID)
                ThreadID = CreateThread(@thWork(), 0)
              EndIf
              
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainCanvas
              Select EventType()
                Case #MyEventType_Update
                  CocoaMessage(0, GadgetID(#MainCanvas), "display")
                  SignalSemaphore(EventData())
              EndSelect
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply