It is currently Sat Aug 15, 2020 12:30 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Trouble with threads
PostPosted: Sat Mar 14, 2020 1:24 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu Dec 29, 2011 5:03 pm
Posts: 587
Hi,

I'm having a lot of trouble with threads on Linux. I have created a shared library that when the main program tells it to, launches an external program and runs a thread to monitor when the program has stopped. When the program does stop, however, the main program freezes and becomes unresponsive.

Both the main program and shared library have "Create threadsafe executable" enabled.

Am I doing something wrong? It all works fine on Windows and macOS.

Code:
ProcedureDLL StartExternalProgram(path$, output$)
  myProgram = RunProgram(path$, output$, "", #PB_Program_Open)
  If IsProgram(myProgram)
    myProgramThread = CreateThread(@ThreadProcedure(), 0)
  EndIf
EndProcedure

This is ThreadProcedure:
Code:
Procedure ThreadProcedure(*Value)
  If IsProgram(myProgram)
    Repeat
      Delay(20)
    Until ProgramRunning(myProgram) = 0
  EndIf
  Stop()
EndProcedure

And this is the Stop procedure:
Code:
ProcedureDLL Stop()
  SendMainProgramSignal(1) ; Tell main program the program has exited
  If IsProgram(myProgram) And ProgramRunning(myProgram)
    KillProgram(myProgram)
    CloseProgram(myProgram)
  EndIf
EndProcedure


Top
 Profile  
Reply with quote  
 Post subject: Re: Trouble with threads
PostPosted: Sat Mar 14, 2020 1:53 pm 
Offline
Always Here
Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 6256
Location: Wales, UK
...could be the issue is using Kill Program before Close Program?

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
Reply with quote  
 Post subject: Re: Trouble with threads
PostPosted: Sat Mar 14, 2020 2:15 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu Dec 29, 2011 5:03 pm
Posts: 587
It still happens if I switch them around. If I take out KillProgram, I get an IMA. With only KillProgram, it doesn't give me an IMA, but does freeze.

It doesn't freeze or give me an IMA if I comment out the call to Stop() in ThreadProcedure...but without that, how do I tell the main program that the external program has closed? My shared library doesn't open any windows or have its own event loop, so can I use PostEvent? Or is there another way?


Top
 Profile  
Reply with quote  
 Post subject: Re: Trouble with threads
PostPosted: Sat Mar 14, 2020 3:20 pm 
Offline
Always Here
Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 6256
Location: Wales, UK
Just off the top of my head

The thread procedure is the one that detects the external program has finished. So let's try CloseProgram() from there. The Thread can update a Global Var igExternalClosed = #True, which any other part of the main program can check periodically.

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
Reply with quote  
 Post subject: Re: Trouble with threads
PostPosted: Sat Mar 14, 2020 3:56 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3309
Location: Boston, MA
I have to control a few external programs and the most stable and quickest coding method was a file based handshake. TCP/UDP was an option but more complex to implement between developers. It depends on the speed required for your implementation.

_________________
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum


Top
 Profile  
Reply with quote  
 Post subject: Re: Trouble with threads
PostPosted: Sat Mar 14, 2020 5:29 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu Dec 29, 2011 5:03 pm
Posts: 587
Regarding calling KillProgram before CloseProgram, I checked the manual and it says that's what you're supposed to do.

IdeasVacuum wrote:
Just off the top of my head

The thread procedure is the one that detects the external program has finished. So let's try CloseProgram() from there. The Thread can update a Global Var igExternalClosed = #True, which any other part of the main program can check periodically.
I just tried this and it seems to work great. Thanks for the idea!

skywalk wrote:
I have to control a few external programs and the most stable and quickest coding method was a file based handshake. TCP/UDP was an option but more complex to implement between developers. It depends on the speed required for your implementation.
Would a file based handshake be each program updating a file based on whether it's running or not? I wouldn't know where to start with TCP/UDP between two apps. I might need to look into that in the future. Do you know any good examples for that?


Top
 Profile  
Reply with quote  
 Post subject: Re: Trouble with threads
PostPosted: Sat Mar 14, 2020 7:04 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3309
Location: Boston, MA
If you are the author of the external program, then it is simple.
Write a "MyApp.run" file to some non-privileged folder(no Admin required).
MyApp.exe looks for the presence of "MyApp.run". If found, then it does something.
After doing something, MyApp.exe writes "MyApp.ran".
There are many variations, and you can add data to the contents of the files if you need.
Avoid using the Clipboard for this action, since you cannot guarantee the user, or another app will alter the clipboard.
TCP/UDP examples were done by mk-soft and maybe infratec?

_________________
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum


Top
 Profile  
Reply with quote  
 Post subject: Re: Trouble with threads
PostPosted: Sat Mar 14, 2020 7:11 pm 
Offline
Always Here
Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 6256
Location: Wales, UK
I see that about CloseProgram() - that function is effectively misnamed since it isn't closing the program, it's closing the connection. :shock:

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
Reply with quote  
 Post subject: Re: Trouble with threads
PostPosted: Sat Mar 14, 2020 7:35 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu Dec 29, 2011 5:03 pm
Posts: 587
skywalk wrote:
If you are the author of the external program, then it is simple.
Write a "MyApp.run" file to some non-privileged folder(no Admin required).
MyApp.exe looks for the presence of "MyApp.run". If found, then it does something.
After doing something, MyApp.exe writes "MyApp.ran".
There are many variations, and you can add data to the contents of the files if you need.
Avoid using the Clipboard for this action, since you cannot guarantee the user, or another app will alter the clipboard.
TCP/UDP examples were done by mk-soft and maybe infratec?
Ah, thanks. That's an interesting way of doing it. I would never use the clipboard since that is for the user's use. I would never want to risk overwriting something important.


Top
 Profile  
Reply with quote  
 Post subject: Re: Trouble with threads
PostPosted: Sat Mar 14, 2020 8:15 pm 
Offline
Always Here
Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 6256
Location: Wales, UK
.... you could also use shared memory.

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
Reply with quote  
 Post subject: Re: Trouble with threads
PostPosted: Mon Mar 16, 2020 2:06 pm 
Offline
Always Here
Always Here

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7446
Location: Norway
Your ThreadProcedure() checks myProgram, which is a variable local to StartExternalProgram().

Most likely your problem is in the SendMainProgramSignal() procedure.

Also, why not use #PB_Program_Wait?

Code:
Structure Rundata
  Path.s
  Output.s
EndStructure

Procedure RunProgramNotifyOnEnd(*Rundata.Rundata)
  RunProgram(*Rundata\Path, *Rundata\Output, "", #PB_Program_Wait)
  FreeMemory(*Rundata)
  NotifyMainThread(1)
EndProcedure

ProcedureDLL StartExternalProgram(path$, output$)
  *R.Rundata = AllocateStructure(Rundata)
  *R\Path = path$
  *R\Output = output$
  CreateThread(@RunProgramNotifyOnEnd(), *R)
EndProcedure


Edit: Remember that any procedure you call from a thread, is also running in that thread.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye