www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - sending the address of a struct

reply Johann Lermer <johann.lermer elvin.eu> writes:
Hi,

I have a struct in a separate thread and want to pass it's 
address back to the main thread. This is how I think it should 
work:

import std.concurrency;

struct Env {}

void run ()
{
	shared Env env;
	ownerTid.send (&env);
	for (;;) {}
}

void main ()
{
	spawn (&run);
	auto e = receiveOnly!(shared Env*);
}

but I'm getting an error when main tries to receive the pointer. 
The error message says:

std.concurrency.MessageMismatch std/concurrency.d(237): 
Unexpected message type: expected 'shared(Env*)', got 
'shared(test.Env)*'

Now, how can I pass that pointer back to main?
Sep 06 2020
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Sunday, 6 September 2020 at 09:58:54 UTC, Johann Lermer wrote:
 pointer. The error message says:

 std.concurrency.MessageMismatch std/concurrency.d(237): 
 Unexpected message type: expected 'shared(Env*)', got 
 'shared(test.Env)*'
The error message gives you all the information you need - notice the position of the asterisk inside the parens on one, outside on the other? The pointer itself is not shared, only the pointee - the data pointed to. This works: auto e = receiveOnly!(shared(Env)*); -- Simen
Sep 06 2020
parent Johann Lermer <johann.lermer elvin.eu> writes:
On Sunday, 6 September 2020 at 11:10:14 UTC, Simen Kjærås wrote:
     auto e = receiveOnly!(shared(Env)*);
Oh, thanks. Seems, that I just missed that bit with the pranetheses.
Sep 06 2020