www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - send doesn't work

reply unDEFER <undefer gmail.com> writes:
Hello!
In my thread I'm sending the Tid of the thread to the parent to 
inform it about finishing:
send(tid, thisTid);

The parent thread has the next code to receive it and handle:
receiveTimeout( dur!"hnsecs"(1),
                 (Tid tid)
                 {//the message from child thread handler} );

It is good works for my first child thread.
But, some times ago I have added the second child thread (no, no, 
the first thread and the second thread works not simultaneously) 
and have found that the receiving message doesn't work! The 
parent doesn't get the message although the child sends it.
So in my code have appeared the next insertion in the start of 
thread:
/* Strange, but without sending message
        at start thread makes not working
        sending at end thread */
     send(tid, "Hello");

Now I have added the third child thread. At the first it have 
worked good, but now it again doesn't receive the message. And 
"send hello"-hack doesn't help.
What to do? Why the sending of the message may doesn't work?
Nov 15 2016
next sibling parent reply Daniel Kozak via Digitalmars-d <digitalmars-d puremagic.com> writes:
Dne 15.11.2016 v 17:36 unDEFER via Digitalmars-d napsal(a):

 Hello!
 In my thread I'm sending the Tid of the thread to the parent to inform 
 it about finishing:
 send(tid, thisTid);

 The parent thread has the next code to receive it and handle:
 receiveTimeout( dur!"hnsecs"(1),
                 (Tid tid)
                 {//the message from child thread handler} );

 It is good works for my first child thread.
 But, some times ago I have added the second child thread (no, no, the 
 first thread and the second thread works not simultaneously) and have 
 found that the receiving message doesn't work! The parent doesn't get 
 the message although the child sends it.
 So in my code have appeared the next insertion in the start of thread:
 /* Strange, but without sending message
        at start thread makes not working
        sending at end thread */
     send(tid, "Hello");

 Now I have added the third child thread. At the first it have worked 
 good, but now it again doesn't receive the message. And "send 
 hello"-hack doesn't help.
 What to do? Why the sending of the message may doesn't work?
Yous should post this into Learn news group not in General. And it would be better if you provided full (not)working example. and I guess you have this receiveTimeout( dur!"hnsecs"(1), (Tid tid) {//the message from child thread handler} ); in some while(true) loop?
Nov 15 2016
parent unDEFER <undefer gmail.com> writes:
On Tuesday, 15 November 2016 at 18:11:38 UTC, Daniel Kozak wrote:

 Yous should post this into Learn news group not in General. And 
 it would be better if you provided full (not)working example.
If the problem will appears again I will try to make short not working example..
 and I guess you have this

 receiveTimeout( dur!"hnsecs"(1),
                 (Tid tid)
                 {//the message from child thread handler} );

 in some while(true) loop?
Of course there is while(!gs.finish) :-)
Nov 15 2016
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 11/15/16 11:36 AM, unDEFER wrote:
 Hello!
 In my thread I'm sending the Tid of the thread to the parent to inform
 it about finishing:
 send(tid, thisTid);

 The parent thread has the next code to receive it and handle:
 receiveTimeout( dur!"hnsecs"(1),
                 (Tid tid)
                 {//the message from child thread handler} );

 It is good works for my first child thread.
 But, some times ago I have added the second child thread (no, no, the
 first thread and the second thread works not simultaneously) and have
 found that the receiving message doesn't work! The parent doesn't get
 the message although the child sends it.
 So in my code have appeared the next insertion in the start of thread:
 /* Strange, but without sending message
        at start thread makes not working
        sending at end thread */
     send(tid, "Hello");

 Now I have added the third child thread. At the first it have worked
 good, but now it again doesn't receive the message. And "send
 hello"-hack doesn't help.
 What to do? Why the sending of the message may doesn't work?
Without more complete code, it's difficult to say what is happening. -Steve
Nov 15 2016
parent unDEFER <undefer gmail.com> writes:
On Tuesday, 15 November 2016 at 18:12:32 UTC, Steven 
Schveighoffer wrote:
 Without more complete code, it's difficult to say what is 
 happening.

 -Steve
Code is big. I of course can return to the question when the code will available on the git. But now it is not available.
Nov 15 2016
prev sibling next sibling parent reply unDEFER <undefer gmail.com> writes:
Now I'm sending "hello"-message from child thread in the loop 
every iteration, and the last message again working.
What the magic??? The send-subsystem "becomes rotten" if don't 
use it???
Nov 15 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 11/15/16 1:13 PM, unDEFER wrote:
 Now I'm sending "hello"-message from child thread in the loop every
 iteration, and the last message again working.
 What the magic??? The send-subsystem "becomes rotten" if don't use it???
1. I'm sure it's not "magic" but likely something you are missing. 2. Nobody here can help you without a working demonstration. i.e., this isn't a known issue, and others have used the concurrency subsystem without such problems. If you construct a small example that demonstrates the problem, that is helpful for diagnosis (or better yet, may demonstrate to you why it's not working in your full codebase). -Steve
Nov 15 2016
parent reply unDEFER <undefer gmail.com> writes:
On Tuesday, 15 November 2016 at 18:23:16 UTC, Steven 
Schveighoffer wrote:
 1. I'm sure it's not "magic" but likely something you are 
 missing.
 2. Nobody here can help you without a working demonstration. 
 i.e., this isn't a known issue, and others have used the 
 concurrency subsystem without such problems.

 If you construct a small example that demonstrates the problem, 
 that is helpful for diagnosis (or better yet, may demonstrate 
 to you why it's not working in your full codebase).

 -Steve
So, the problem still here. Sometimes it is working with hello-messages, but sometimes doesn't work. I will try to make minimal application. But I don't know still what it must do. Trying to minimize the main thread to starter of the second thread shows that the problems goes away.. So it will really not easy, but I will try... Thank you.
Nov 15 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 11/15/16 1:56 PM, unDEFER wrote:
 On Tuesday, 15 November 2016 at 18:23:16 UTC, Steven Schveighoffer wrote:
 1. I'm sure it's not "magic" but likely something you are missing.
 2. Nobody here can help you without a working demonstration. i.e.,
 this isn't a known issue, and others have used the concurrency
 subsystem without such problems.

 If you construct a small example that demonstrates the problem, that
 is helpful for diagnosis (or better yet, may demonstrate to you why
 it's not working in your full codebase).
So, the problem still here. Sometimes it is working with hello-messages, but sometimes doesn't work. I will try to make minimal application. But I don't know still what it must do. Trying to minimize the main thread to starter of the second thread shows that the problems goes away.. So it will really not easy, but I will try... Thank you.
From experience, this smells like a race condition. I'd try removing features of your program until it starts working, then you may have your answer. I'd suggest dustmite may help, but in the case of non-deterministic failure, it's more difficult to have it work. -Steve
Nov 15 2016
next sibling parent unDEFER <undefer gmail.com> writes:
Really situation is much more interesting:
Sometimes I got Tid not the same as sent.
Sometimes I receive 2 TID although sent only 1.
Nov 16 2016
prev sibling parent reply unDEFER <undefer gmail.com> writes:
On Tuesday, 15 November 2016 at 20:46:59 UTC, Steven 
Schveighoffer wrote:

 From experience, this smells like a race condition.
And I have found the error! And you are right! It is really race condition. The problem was that I had not one receiveTimeout() but 3 receiveTimeout() for each thread. I don't know how I expect that each send must go to his own receiveTimeout, but of course it was false. Big thanks to all!
Nov 16 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 11/16/16 4:35 AM, unDEFER wrote:
 On Tuesday, 15 November 2016 at 20:46:59 UTC, Steven Schveighoffer wrote:

 From experience, this smells like a race condition.
And I have found the error! And you are right! It is really race condition. The problem was that I had not one receiveTimeout() but 3 receiveTimeout() for each thread. I don't know how I expect that each send must go to his own receiveTimeout, but of course it was false. Big thanks to all!
Glad you figured it out! -Steve
Nov 16 2016
prev sibling parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
On Tuesday, 15 November 2016 at 16:36:00 UTC, unDEFER wrote:
 The parent thread has the next code to receive it and handle:
 receiveTimeout( dur!"hnsecs"(1),
                 (Tid tid)
                 {//the message from child thread handler} );
I just have to wonder every time I look at this... how long is 1 hnsecs? I'd assume not very long. Either blocking unless there's no more messages, or having a longer timeout period seems a better bet. Might make it 1-5ms.
Nov 16 2016
parent reply unDEFER <undefer gmail.com> writes:
On Wednesday, 16 November 2016 at 10:54:32 UTC, Era Scarecrow 
wrote:
  I just have to wonder every time I look at this... how long is 
 1 hnsecs? I'd assume not very long. Either blocking unless 
 there's no more messages, or having a longer timeout period 
 seems a better bet.

  Might make it 1-5ms.
Really I have wanted to write 0ms, but don't sure that it is possible.
Nov 16 2016
parent reply Anonymouse <asdf asdf.net> writes:
On Wednesday, 16 November 2016 at 13:10:06 UTC, unDEFER wrote:
 On Wednesday, 16 November 2016 at 10:54:32 UTC, Era Scarecrow 
 wrote:
  I just have to wonder every time I look at this... how long 
 is 1 hnsecs? I'd assume not very long. Either blocking unless 
 there's no more messages, or having a longer timeout period 
 seems a better bet.

  Might make it 1-5ms.
Really I have wanted to write 0ms, but don't sure that it is possible.
receiveTimeout(0.seconds, /* ... */ );
Nov 16 2016
parent reply unDEFER <undefer gmail.com> writes:
On Wednesday, 16 November 2016 at 20:20:07 UTC, Anonymouse wrote:
 receiveTimeout(0.seconds,
     /* ... */
 );
Thanks!
Nov 17 2016
parent Is it possible to store different generic types? <jj_1337 live.dk> writes:
On Thursday, 17 November 2016 at 11:37:37 UTC, unDEFER wrote:
 On Wednesday, 16 November 2016 at 20:20:07 UTC, Anonymouse 
 wrote:
 receiveTimeout(0.seconds,
     /* ... */
 );
Thanks!
You can also do -1 for instant receive.
Nov 18 2016