1. Roland Smith
  2. PowerBuilder
  3. Thursday, 7 December 2023 20:11 PM UTC

PB 2022

I have a multi-threaded app (using SharedObject functions) that is non-visual, it receives requests via Winsock and creates a background thread to process the request.

I'm getting hang-ups when it gets a lot of requests all at once. I suspect it has to do with notifying the main thread that the background thread is done.

Here is how it works:

I have a 'callback' object that is defined as a global variable with the name gnv_callback.

This is the code that launches the background thread. Callback is a function argument name. I encapsulated the SharedObjectRegister/SharedObjectGet into the CreateSharedObject function in the Callback object. In the lnv_Request object, SetCallback saves a reference to Callback in an instance variable.

			// register the shared object
			ler_result = Callback.CreateSharedObject(lnv_Request, ls_InstanceName)
			If ler_result = Success! Then
				// call functions in the shared object
				lnv_Request.Post Process_Request(ls_InstanceName, RequestNbr)

The Process_Request function will perform the business code and when done it has the following:

// Shutdown the client socket
shutdown(ClientSocket, SD_SEND)

// Notify the main thread that the request is done
Callback.Post NotifyDone(InstanceName)

NotifyDone is just this:


Now I am wondering if there is a better way to unregister the shared object without causing blocking of the main thread.

John Fauss Accepted Answer Pending Moderation
  1. Thursday, 7 December 2023 21:15 PM UTC
  2. PowerBuilder
  3. # 1

Is the instance name for each registered shared object unique? I couldn't tell from your description. I can foresee where things could get dicey if the same instance name is being used repeatedly.

If the callback function does not pass any information back to the main (GUI) thread, then yes, I would try posting the SharedObjectUnregister() function immediately after posting the Process_Request method in the shared object, eliminating the need for the callback object.

  1. Roland Smith
  2. Thursday, 7 December 2023 21:26 PM UTC
The instance name is unique:

InstanceName = "Request" + String(RequestNbr)
  1. Helpful
  1. Roland Smith
  2. Thursday, 7 December 2023 21:27 PM UTC
Immediately posting SharedObjectUnregister seems to work well. I used Process Explorer to monitor the threads and Resource Monitor to watch memory and CPU.
  1. Helpful 1
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Thursday, 7 December 2023 20:19 PM UTC
  2. PowerBuilder
  3. # 2

Should I just call SharedObjectUnregister immediately? From what the help topic says, the object won't be destroyed until the reference variable goes out of scope.

There are no comments made yet.
mike S Accepted Answer Pending Moderation
  1. Thursday, 7 December 2023 20:19 PM UTC
  2. PowerBuilder
  3. # 3

do you need to unregister the object? 

can you keep it around to process future requests and just get rid of it on application shutdown?

  1. Roland Smith
  2. Thursday, 7 December 2023 20:45 PM UTC
To re-use, I would need to know if it is in use or not.
  1. Helpful
  1. Benjamin Gaesslein
  2. Friday, 8 December 2023 08:41 AM UTC
You're already letting the main thread know that it's done, no? Keeping track of running and available threads is definitely worth it because the overhead of setting up a new thread every time just eats performance unnecessarily.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1

There are no replies made for this question yet.
However, you are not allowed to reply to this question.