www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - can't understand why code do not working

reply "Suliman" <evermind live.ru> writes:
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);
}
Sep 22 2014
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Sep 22 2014
parent reply "Suliman" <evermind live.ru> writes:
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)
Sep 22 2014
next sibling parent reply "Cliff" <cliff.s.hudson gmail.com> writes:
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?
Sep 22 2014
next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Sep 22 2014
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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.
Sep 22 2014
parent reply "Cliff" <cliff.s.hudson gmail.com> writes:
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.
Sep 22 2014
parent "Cliff" <cliff.s.hudson gmail.com> writes:
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.
Sep 22 2014
prev sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
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.
Sep 22 2014
prev sibling next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
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.
Sep 22 2014
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 09/22/2014 01:12 PM, Suliman wrote:

 std.concurrency.OwnerTerminated std\concurrency.d(234): Owner terminated
 ----------------
I feel bad about that uninteresting first example that lost you time. :( There are a number of options: - The main thread can call thread_joinAll() to wait for all workers to finish. - The threads can have a protocol to signal each other that they are done. (I used 'struct Exit' in that chapter.) - Threads can detect exceptions like OwnerTerminated and act accordingly. The rest of the chapter has examples of those options. Ali
Sep 22 2014
prev sibling parent "kiran kumari" <kiranfabzen gmail.com> writes:
On Monday, 22 September 2014 at 19:36:39 UTC, Suliman wrote:
 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);
    }
 }
see more example http://techgurulab.com/course/java-quiz-online/
 void worker()
 {
     int value = 0;
     value = receiveOnly!int();
     writeln(value);
     int result = value * 3;
     ownerTid.send(result);
 }
see more example http://techgurulab.com/course/java-quiz-online/
Sep 24 2014