How to pack types with variables in one message to send it to another thread? [tuple]

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Sep 7 04:41:38 PDT 2014


On 7/09/2014 10:42 p.m., MarisaLovesUsAll wrote:
> Hi!
> I'm trying to make my program multithreaded, and I was stuck at
> messaging between threads.
> I need to pack types and variables into one message. Will I use Tuples
> or something?
>
> e.g.
>
> class Sprite {};
>
> send(tid, Sprite, "create", myInt);

Don't worry about the packing when calling send.
It'll automatically be converted into a "tuple".
Also you should only be using immutable or primitive types. Strings are 
immutable so thats ok. A class instance that isn't immutable isn't.

Note Sprite is a class type not a class instance.

> ................
>
> Also I don't understand how to use Variant. Messages can be different,
> and I don't know how to extract data from variant.
>
> send(tid, "One", "Two", myInt);
>
> receive(
>      (Variant args)
>      {
>          /*
>          args contains Tuple!(string, string, int)("One", "Two", 42);
>          I need simple access to data, e.g. args[0] args[1] args[2]
>          but I don't know how to do this
>          because `.get` method need precise type of Tuple
>          */
>      }
> );

Don't worry about it.
Just have separate receiving functions per the data type.
You'll probably be better off.

In other words Variant is overkill.
It basically just wraps a piece of data so that it can be passed around 
without knowing its type. Which in this case is bad.
You would end up having to know the datatype to do anything with it anyway.


More information about the Digitalmars-d-learn mailing list