Thread overview
Unsafe, unchecked message-passing with std.concurrency
Aug 27, 2020
Elronnd
Aug 27, 2020
Simen Kjærås
Aug 27, 2020
Elronnd
August 27, 2020
Is there any way to do unsafe, unchecked message-passing using std.concurrency's API?  I want to be able to share mutable data without having to qualify it as shared.  It looks like the check happens in send(), before passing off data to _send(), which does no checking; but _send is private.  Is there any analogous functionality elsewhere in phobos--send_unchecked or similar?
August 27, 2020
On Thursday, 27 August 2020 at 08:39:49 UTC, Elronnd wrote:
> Is there any way to do unsafe, unchecked message-passing using std.concurrency's API?  I want to be able to share mutable data without having to qualify it as shared.  It looks like the check happens in send(), before passing off data to _send(), which does no checking; but _send is private.  Is there any analogous functionality elsewhere in phobos--send_unchecked or similar?

Since you're fine with unsafe, unchecked - why not just lie to the compiler? Cast the data to shared and send it on its merry way?

--
  Simen
August 27, 2020
On Thursday, 27 August 2020 at 08:46:31 UTC, Simen Kjærås wrote:
> Since you're fine with unsafe, unchecked - why not just lie to the compiler? Cast the data to shared and send it on its merry way?

It's bothersome and error-prone.  I have to cast the data to shared.  I also have to remember to receive a shared object on the other side (and if I forget, I won't get a compile error, stuff will just silently break).  Once I've received it, I have to cast away the shared again if I want to be able to use the object for anything.  I'll do that if I have to, (or write my message-passing library, or repurpose one from C), but would be nice if there were another solution.
August 27, 2020
On Thursday, 27 August 2020 at 08:59:45 UTC, Elronnd wrote:
> On Thursday, 27 August 2020 at 08:46:31 UTC, Simen Kjærås wrote:
>> Since you're fine with unsafe, unchecked - why not just lie to the compiler? Cast the data to shared and send it on its merry way?
>
> It's bothersome and error-prone.  I have to cast the data to shared.  I also have to remember to receive a shared object on the other side (and if I forget, I won't get a compile error, stuff will just silently break).  Once I've received it, I have to cast away the shared again if I want to be able to use the object for anything.  I'll do that if I have to, (or write my message-passing library, or repurpose one from C), but would be nice if there were another solution.

The simplest solution is to write your own wrappers of `send` and `receive` and have them do the casting for you.