Could someone explain the Runspace PSThreadOptions? - multithreading

My main question is does 1 RunSpace represent 1 thread.
I read this post:
Process vs Instance vs Runspace in PowerShell
And originally I understood it as:
You have a currently running powershell process.
A runspace object is created within powershell's process address space.
It sets up and manages it's own thread within the main process.
You would create an object that represent the script you want to run in that thread with [powershell]::create()
Then pass it to the runspace to be executed within the thread it manages.
However after looking at the PSThreadOptions
https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.psthreadoptions?view=pscore-6.2.0
I'm a little confused - if (as I originally thought) 1 runspace = 1 thread why does it have an option to create a new thread for each invocation (I'm asuming they mean for each time you call .invoke() on an already created object and not just if you reinstanciate it) and also in what instance might someone use the current thread?
Any clarification on how these options work any corrections you can offer to my current understanding of things would be very much appreciated
Many Thanks
Nick

Related

Julia: spawnat always runs on the same threads even if asked to run for multipleprocesses

I am trying to run the same simulation on different threads in Julia. My computer has 4 cores.
I checked that Threads.nthreads() =4, and added processes so that nprocs() = 4.
Now I am trying to run the same simulation:simulation() in parallel.
My code is
#spawnat 1 simulation()
#spawnat 2 simulation()
#spawnat 3 simulation()
#spawnat 4 simulation()
the simulation function displays as well on which threads it runs thanks to a println(Threads.threadid())
The problem is that all processes run on the same thread - thread number 1.
I thought that creating workers and using the #spawnat macro should be enough for the simulation to run on all threads.
Would you have any idea on what to do, and what I did wrong? Thanks in advance
I think you might be confusing multi-threading and multi-processing? From the docs:
help?> #spawnat
#spawnat p expr
Create a closure around an expression and run the closure asynchronously on process p
So your code is executed on one of theworker processes you created with addprocs. Whether or not the code that is being executed uses more than one thread though depends on whether the code itself is multithreaded, so e.g. is using the #threads macro.
The Julia manual discusses multi-threading here and multi-processing here.
EDIT to add: there's a discussion on the Julia Discourse here which includes an example of running multi-threaded worker processes - to do so you need to set an environment variable to ensure worker processes are started with multiple threads.

How to modelize a threads pool usage?

Hy,
I tried to modelize a threads pool usage with the following scenario:
Request a db to retrieve a set of instances
Create a set of threads, each one doing the same calls set
start a loop for all retrieved instances
wait that one thread is availablle
Provides the instance to the available thread that executes its two tasks
enf of loop
I see this concurrency question but it does not modelize a pool.
Any idea ? A fork is like to create a thread so i can not figure how to modelize my goal.
Maybe something like, but this is not correct because StructuredAction does not get a isSynchronous attribute (i did not find one in StructuredAction generalizations)
The schema provided in the question is in fact my answer.

Why a Delphi application has more than one thread?

I have seen that my Delphi app has more than one thread - 7 more exactly. OF COURSE I am not creating my own threads and I am only using 'classic' VCL controls. The program is idle. It just displays the main form. No dialogs are active, no personal/Indy/etc threads running.
Wasn't supposed to have just one thread (the VCL main thread)?
How do I know which of these (already running threads) is the main thread?
Note 1: This is related to Program freezes but the CPU utilization is zero
Note 2: I just realized that 2 or 3 of those threads are from the debugger (extra threads appear when I pause the program and invoke the 'CPU view' window).
As you see in the comments an answer to the first part of your question is broad. Just creating a new 'VCL Forms Application' project in Delphi XE7 and running it I had 4 Threads in the 'Thread Status' view. Others have mentioned TThreadedQueue, file dialogs, VCL hint code and more as a source for unaccounted threads.
For the second part of your question I suggested naming the main thread at startup with something like:
TThread.NameThreadForDebugging('Main Thread', MainThreadID);
As you mention in the comments you might not be able to name it beforehand, so in an existing debug session you could do it ad hoc:
Invoke the Evaluate / Modify window
Ctrl+F7 or
Context Menu > Debug > Evaluate / Modify
Type in MainThreadID as expression to eveluate, resulting in the ID you are looking for.
If you want you can use the context menu entry 'Name Thread' in the Thread view to name the thread with that ID.
Identifying other threads with the IDE is possible, too. A double click on a thread in the Threads view will open the current call stack. Skimming over that you might already be able to tell if this is a native Delphi thread or an external.
For example one of the 'unidentified' - external - threads I did not create has following call stack:
While a Delphi thread I created for testing purposes has following, where the Delphi units are recognizable:

Runspaces and Shared Objects with Methods

There are lots of QA's and articles online explaining how to work with runspaces and shared objects' data in PowerShell using synchronization, locking, etc. But I couldn't find anything about executing method of PSObject, passed by [reference] from the main thread to another runspace. When called from within the runspace, I found that the method of the passed object executes on the main thread.
Is there a way to make the method run in the runspace, other than rewriting it as a separate function and adding that function to the runspace via [powershell]::Create().AddScript()?

Provide updates to Qt GUI from sub thread

I know very similar questions have been asked before, but I am unable to find an answer for my specific problem. I have a main (GUI) thread which upon button press initializes a worker thread to perform some analysis. I am using signals and slots to communicate between my worker thread and my GUI thread (i.e. when the thread starts and when it finishes), but I need to go deeper than that. My worker thread actually calls another class in a separate implementation file which then iterates through a series of calculations which are sent to std::cout for each iteration (as the code used to be a console application for which I am now writing a GUI). I am trying to feed those outputs for each iteration back into my GUI thread so that my text browser is updated in real time as my code iterates. The problem is, when I emit a signal from the class my worker thread calls, it is not picked up by the GUI thread. I do not get any errors. Does anyone have any suggestions on how to transmit a signal to the GUI from a class that my worker thread is calling? I can post code as required, but I'm not sure what would be most helpful to see and my code is quite extensive (it's an aircraft performance application). Any help would be greatly appreciated. Thank you kindly!
1) Make sure the connect() call for connecting you signal returns true. If it does not, qdebug output usually tells you what is wrong
2) You should use the QueuedConnection type (the default (Auto) should work also)

Resources