std.concurrency extension

Jonas Drewsen jdrewsen at nospam.com
Wed Jun 8 01:19:23 PDT 2011


On 08/06/11 00.53, Jose Armando Garcia wrote:
> On Tue, Jun 7, 2011 at 7:07 PM, jdrewsen<jdrewsen at nospam.com>  wrote:
>> Hi,
>>
>>    Currently each thread spawned has an associated Tid when using
>> std.concurrency. When calling receive() this is always the Tid (and
>> associated messagebox) that you receive from.
>>
>> This becomes a problem when you spawn several worker threads that send
>> messages to the main thread since each call to receive must know how to
>> handle all kind on messages.
>>
>> For example: the current version of the curl wrapper I'm working on receives
>> HTTP data from worker threads using receive. The user of this wrapper can
>> also create his own worker threads that will send data to the main thread.
>> This means that the curl wrapper must be able to handle the users worker
>> thread messages.
>>
>> The GO language has solved this problem with channels. My suggestion is to
>> allow a thread to create additional Tids that can act just like channels.
>> Then by adding a receive() call that accepts a Tid as first argument we can
>> use them as channels.
>>
>> e.g.
>> -------------------------------------------------
>> void run() {
>>         auto channel1 = receiveOnly!Tid();
>>         channel1.send("Hello world);
>> }
>>
>> Tid channel1 = Tid();
>> auto tid1 = spawn(&run);
>> tid.send(channel1);
>>
>> // receive only from the specified work on the channel
>> writeln(channel1.receiveOnly!string());
>> -------------------------------------------------
>>
>> This would decouple handling of different worker threads if needed.
>>
>> This is something I need for the curl wrapper I think. I can create patch
>> for this if it is something that will be accepted upstream?
>>
>> /Jonas
>>
>
> If I was you I would build channels on top of Tid/mbox. How you do you
> efficiently wait on multiple channels?

As I see it building channels on top of the current Tid/mbox model would 
only add an extra indirection. Since there is only one Tid per thread it 
would need a central receive() to dispatch to channels. In addition it 
would have to tune the mailbox size to accommodate all channels etc.

Adding the option to provide the Tid for the receive call seems to be 
much simpler.

Waiting on multiple channels (ie. Tids) would introduce some changes to 
the MessageBox class and a new condition variable. But this is a bigger 
change that I currently do not need myself and therefore probably would 
not include in a patch.

/Jonas




More information about the Digitalmars-d mailing list