Thread overview
Threads
Mar 22, 2023
Tim
Mar 22, 2023
Kagamin
Mar 22, 2023
Tim
Mar 22, 2023
Tim
Mar 22, 2023
Ali Çehreli
March 22, 2023

Hello! I am trying to make a simple multi-threading application. But I get an error when I run the main thread of the program in a thread. The question is: "How to pass arguments from the main thread to the newly created thread of itself". Here is a piece of code:

module main;
import app;
import core.thread;

int main(string[] args)
{

static int result;
static string[] args_copy;

static void app_thread()
{
    App app = new App();
    result = app.run(args_copy); //<-- Exception; static int App.run(string[] args)
    //object.Exception@/usr/include/dmd/phobos/std/getopt.d(423):
    // Invalid arguments string passed: program name missing
}

args_copy = args; //Why program name is missing while copy arguments?

// Running app interface in a thread;
Thread thread = new Thread(&app_thread).start();
thread.join();

return result;

}

March 22, 2023

static is thread local by default.

module main;
import app;
import core.thread;

int main(string[] args)
{
     static shared int result;
     static shared string[] args_copy;

     static void app_thread()
     {
         App app = new App();
         result = app.run(args_copy);
     }

     args_copy = cast(shared)args;

     // Running app interface in a thread;
     Thread thread = new Thread(&app_thread).start();
     thread.join();

     return result;
}
March 22, 2023

On Wednesday, 22 March 2023 at 07:16:43 UTC, Kagamin wrote:

>

static is thread local by default.

module main;
import app;
import core.thread;

int main(string[] args)
{
     static shared int result;
     static shared string[] args_copy;

     static void app_thread()
     {
         App app = new App();
         result = app.run(args_copy); // <-- Stucked here!
     }

     args_copy = cast(shared)args;

     // Running app interface in a thread;
     Thread thread = new Thread(&app_thread).start();
     thread.join();

     return result;
}

I've changed "int App.run(string[] args)" to "int App.run(shared string[] args)" in app module. Now I've got an error in std.getopt.getopt in the same module.

Error: none of the overloads of template std.getopt.getopt are callable using argument types !()(shared(string[]), string, string, string*)

Am I doing something wrong?

March 22, 2023

On Wednesday, 22 March 2023 at 07:16:43 UTC, Kagamin wrote:

>

static is thread local by default.

module main;
import app;
import core.thread;

int main(string[] args)
{
     static shared int result;
     static shared string[] args_copy;

     static void app_thread()
     {
         App app = new App();
         result = app.run(args_copy);
     }

     args_copy = cast(shared)args;

     // Running app interface in a thread;
     Thread thread = new Thread(&app_thread).start();
     thread.join();

     return result;
}

It seems work, but I have to change "int App.run(string[] args)" to "App.run(shared string[] args)" in app module and std.getopt.getopt throws me an error in the same module:

Error: none of the overloads of template std.getopt.getopt are callable using argument types !()(shared(string[]), string, string, string*)
/usr/include/dmd/phobos/std/getopt.d(420,14): Candidate is: getopt(T...)(ref string[] args, T opts)

March 22, 2023
On 3/21/23 22:30, Tim wrote:
> to make a simple multi-threading application.

Unless there is a reason not to, I recommend std.concurrency and std.parallelism modules. They are different but much more simpler compared to the low-level core.thread.

>      args_copy = args; //Why program name is missing while copy arguments?

That doesn't copy. Both args_copy and args will be references to the same elements. (They are both array slices.)

The following may work to get a shared argument:

    immutable args_copy = args.idup;

idup makes an immutable copy and immutable is implicitly shared.

Even if that works, I still find std.concurrency much easier to deal with. :)

Ali