Posts

Showing posts with the label multithreading

why thread has no effect on change made on class variable?

Why does the execution of thread which eventually adds +1 to value has no effect? The std::cout is just zero. I was expecting 1. You are passing a by value, so the thread gets its own private copy that is distinct from the a in main - the thread then modifies the copy and when the thread dies that object dies with it and the change to it is nowhere to be found. The a in main is never modified, so it retains its original value. You want to pass a reference or pointer if you want to change the a in main from within the thread. You probably also want to read up on synchronization and atomic if you are going to be accessing variables from multiple threads. As @JesperJuhl says, you are passing a copy of a so the main's variables is never modified, but, besides, references cannot be passed to other threads. The thread's parameters are always passed by value, so, you need to pass a pointer or a reference_wrapper , which allows to pass a reference, but wrapped in a copyable o...

$MyInvocation in a runspace Thread

A script (foo.ps1) creates a thread, and that thread creates more threads. Foo's thread is my control thread, and it creates one or more worker threads. The worker threads run a script block. The script block calls functions from a library-script. The library script has a configuration file. The script block loads the library-script by dot-sourcing it. When the script loads, the first thing it does is find its configuration file, which is in the script's directory. The code for that looks like... My problem is $MyInvocation doesn't appear to exist. As result, the library script can't find it's configuration file. I'm running Powershell 5.1 on Windows 10. The control thread was made in a runspace. The worker threads are made in a runspace pool. Does anyone know the rules around the automatic $MyInvocation variable in runspace threads? Create a file foo.ps1 and add the following to it: Run it. You should see something like the following... And, I think I just a...