Jump to page: 1 2
Thread overview
can't understand why code do not working
Sep 22, 2014
Suliman
Sep 22, 2014
Ali Çehreli
Sep 22, 2014
Suliman
Sep 22, 2014
Cliff
Sep 22, 2014
monarch_dodra
Sep 22, 2014
Cliff
Sep 22, 2014
Cliff
Sep 22, 2014
monarch_dodra
Sep 22, 2014
monarch_dodra
Sep 22, 2014
Ali Çehreli
September 22, 2014
Already 1 hour I am looking at example from http://ddili.org/ders/d.en/concurrency.html and my modification of it, and can't understand what's difference? Why it's output only:
1
3

and then do not do nothing!

import std.stdio;
import std.concurrency;
import core.thread;


void main()
{
   Tid wk = spawn(&worker);
   foreach(v; 1..5)
   {
    wk.send(v);
    int result = receiveOnly!int();
    writeln(result);
   }
}

void worker()
{
    int value = 0;
    value = receiveOnly!int();
    writeln(value);
    int result = value * 3;
    ownerTid.send(result);
}
September 22, 2014
On 09/22/2014 12:36 PM, Suliman wrote:

> void worker()
> {
>      int value = 0;
>      value = receiveOnly!int();
>      writeln(value);
>      int result = value * 3;
>      ownerTid.send(result);
> }

Your worker terminates after receiving one int and sending one response. It should be waiting in a loop.

Ali

September 22, 2014
void worker()
{
    int value = 0;
    while (value <=10)
    {

        value = receiveOnly!int();
        writeln(value);
        int result = value * 3;
        ownerTid.send(result);
    }
}

give me:

Running .\app1.exe
2
6
3
9
4
12
5
15
6
18
std.concurrency.OwnerTerminated@std\concurrency.d(234): Owner terminated
----------------
0x00405777 in pure @safe void std.concurrency.receiveOnly!(int).receiveOnly().__
lambda3(std.concurrency.OwnerTerminated) at C:\DMD\dmd2\windows\bin\..\..\src\ph
obos\std\concurrency.d(730)
0x0040B88D in @safe void std.concurrency.Message.map!(pure @safe void function(s
td.concurrency.OwnerTerminated)*).map(pure @safe void function(std.concurrency.O
wnerTerminated)*) at C:\DMD\dmd2\windows\bin\..\..\src\phobos\std\concurrency.d(
158)
0x0040B0B6 in D3std11concurrency10MessageBox151__T3getTDFNbNfiZvTPFNaNfC3std11co
ncurrency14LinkTerminatedZvTP8047E12172B30CAF110369CD57C78A37 at C:\DMD\dmd2\win
dows\bin\..\..\src\phobos\std\concurrency.d(1159)

September 22, 2014
On Monday, 22 September 2014 at 20:12:28 UTC, Suliman wrote:
> void worker()
> {
>     int value = 0;
>     while (value <=10)
>     {
>
>         value = receiveOnly!int();
>         writeln(value);
>         int result = value * 3;
>         ownerTid.send(result);
>     }
> }
>
> give me:
>
> Running .\app1.exe
> 2
> 6
> 3
> 9
> 4
> 12
> 5
> 15
> 6
> 18
> std.concurrency.OwnerTerminated@std\concurrency.d(234): Owner terminated
> ----------------
> 0x00405777 in pure @safe void std.concurrency.receiveOnly!(int).receiveOnly().__
> lambda3(std.concurrency.OwnerTerminated) at C:\DMD\dmd2\windows\bin\..\..\src\ph
> obos\std\concurrency.d(730)
> 0x0040B88D in @safe void std.concurrency.Message.map!(pure @safe void function(s
> td.concurrency.OwnerTerminated)*).map(pure @safe void function(std.concurrency.O
> wnerTerminated)*) at C:\DMD\dmd2\windows\bin\..\..\src\phobos\std\concurrency.d(
> 158)
> 0x0040B0B6 in D3std11concurrency10MessageBox151__T3getTDFNbNfiZvTPFNaNfC3std11co
> ncurrency14LinkTerminatedZvTP8047E12172B30CAF110369CD57C78A37 at C:\DMD\dmd2\win
> dows\bin\..\..\src\phobos\std\concurrency.d(1159)

Is stdout threadsafe?
September 22, 2014
On 9/22/14 4:37 PM, Cliff wrote:

>
> Is stdout threadsafe?

Yes, stdout is thread safe, it's based on C's stdout which is thread safe.

-Steve
September 22, 2014
On Monday, 22 September 2014 at 20:37:42 UTC, Cliff wrote:
> Is stdout threadsafe?

Yes. All io operations lock in D, so are thread safe. You will never have interlaced io. If you want to do several opeations, then you can use LockingTextWriter, which is faster, s it locks once until you are finished. Though to be honest, the io itself is slow enough that I've never needed it.
September 22, 2014
On Monday, 22 September 2014 at 20:12:28 UTC, Suliman wrote:
> std.concurrency.OwnerTerminated@std\concurrency.d(234): Owner terminated

You need to make sure your main waits for its workers before exiting. When main "dies", it sends an exception message to anyone still working.
September 22, 2014
On Monday, 22 September 2014 at 21:19:37 UTC, Steven Schveighoffer wrote:
> On 9/22/14 4:37 PM, Cliff wrote:
>
>>
>> Is stdout threadsafe?
>
> Yes, stdout is thread safe, it's based on C's stdout which is thread safe.
>
> -Steve

Techinallly, though thread "safe", concurrent writes will create garbled text. D goes one step further in the sense that it prevents concurrent writes entirely.
September 22, 2014
On Monday, 22 September 2014 at 21:24:58 UTC, monarch_dodra wrote:
> On Monday, 22 September 2014 at 21:19:37 UTC, Steven Schveighoffer wrote:
>> On 9/22/14 4:37 PM, Cliff wrote:
>>
>>>
>>> Is stdout threadsafe?
>>
>> Yes, stdout is thread safe, it's based on C's stdout which is thread safe.
>>
>> -Steve
>
> Techinallly, though thread "safe", concurrent writes will create garbled text. D goes one step further in the sense that it prevents concurrent writes entirely.

The reason I ask is that the first iteration of his loop does not show up in stdout as presented.  I would expect 1 and 3 to be the first two lines of the output.
September 22, 2014
On Monday, 22 September 2014 at 21:28:25 UTC, Cliff wrote:
> On Monday, 22 September 2014 at 21:24:58 UTC, monarch_dodra wrote:
>> On Monday, 22 September 2014 at 21:19:37 UTC, Steven Schveighoffer wrote:
>>> On 9/22/14 4:37 PM, Cliff wrote:
>>>
>>>>
>>>> Is stdout threadsafe?
>>>
>>> Yes, stdout is thread safe, it's based on C's stdout which is thread safe.
>>>
>>> -Steve
>>
>> Techinallly, though thread "safe", concurrent writes will create garbled text. D goes one step further in the sense that it prevents concurrent writes entirely.
>
> The reason I ask is that the first iteration of his loop does not show up in stdout as presented.  I would expect 1 and 3 to be the first two lines of the output.

For that matter I also don't expect the 6 and 18 - nevermind, there must be something else going on with that loop.  He must have changed the loop limits.
« First   ‹ Prev
1 2