www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Thread + socket = (shared) problem? (2.048)

reply Bane <branimir.milosavljevic gmail.com> writes:
This will throw exception on trying to create socket in derived thread. Socket
created in main thread is ok. Is it some shared issue or... ? I have been
trying to find something info in docs and mailing list but no result.

import std.stdio;
import core.thread;
import std.socket;

class MyThread : Thread {
  Socket sock;
  this(){
    super(&run);
  }
  void run(){
    writeln("thread start");
    sock = new TcpSocket; // this will throw exception on 2.047, 2.048
    writeln("thread end");
  }
}

void main(){
  writeln("main start");
  auto s = new TcpSocket;
  writeln("socket in main thread created");
  auto t = new MyThread;
  t.start;
  writeln("main end");
}
Sep 07 2010
next sibling parent reply "Masahiro Nakagawa" <repeatedly gmail.com> writes:
On Tue, 07 Sep 2010 17:51:53 +0900, Bane  
<branimir.milosavljevic gmail.com> wrote:

 This will throw exception on trying to create socket in derived thread.  
 Socket created in main thread is ok. Is it some shared issue or... ? I  
 have been trying to find something info in docs and mailing list but no  
 result.

 import std.stdio;
 import core.thread;
 import std.socket;

 class MyThread : Thread {
   Socket sock;
   this(){
     super(&run);
   }
   void run(){
     writeln("thread start");
     sock = new TcpSocket; // this will throw exception on 2.047, 2.048
     writeln("thread end");
   }
 }

 void main(){
   writeln("main start");
   auto s = new TcpSocket;
   writeln("socket in main thread created");
   auto t = new MyThread;
   t.start;
   writeln("main end");
 }
Please show your environment. Windows? On my Mac, No problem.
Sep 07 2010
parent reply Bane <branimir.milosavljevic gmail.com> writes:
 Please show your environment. Windows?
WinXP sp2, phenom quad core.
Sep 07 2010
next sibling parent Bane <branimir.milosavljevic gmail.com> writes:
This is what I get:

"std.socket.SocketException: Unable to create socket"
Sep 07 2010
prev sibling parent reply "Masahiro Nakagawa" <repeatedly gmail.com> writes:
On Tue, 07 Sep 2010 18:48:22 +0900, Bane  
<branimir.milosavljevic gmail.com> wrote:

 Please show your environment. Windows?
WinXP sp2, phenom quad core.
Current std.socket uses "static ~this" for WSACleanup. In this case, main thread calls "static ~this" before MyThread executes "new TcpSocket". std.socket workarounds: 1. static ~this -> shared static ~this Main thread calls WSAStartup and WSACleanup. 2. Create reference count sample code - http://ideone.com/vRMO0 3. other? I don't know the best way.
Sep 07 2010
parent Bane <branimir.milosavljevic gmail.com> writes:
Thank you for your reply. I have been migrating some socket/thread code from D1
to D2 lately. One of changes is that Thread.this() must have super(&run) in it.
It seems when one of the thread doesn't have that line, strange things happen.
Can anyone confirm this code to reproduce error:

import std.stdio;
import std.socket;
import core.thread;

class SaboteurThread : Thread {
  this(){
    // super(&run); // comment this line for exception to be thrown when socket
is created in other thread
  }
  void run(){
    writeln("Saboteur running");
    while(true){}
  }
}

class SockThread : Thread {
  this(){
    super( &run );
  }

  void run(){
    try {
      auto s = new TcpSocket; // will throw wsock error WSANOTINITIALISED
(10093) on windows if SaboteurThread.this() has no super(&run) in it
      writeln("socket created");
    } catch (Exception e){
      writeln(e);
    }
    while(true){}
  }

}

void main(){
  auto x = new SaboteurThread;
  x.start;
  
  auto s = new SockThread;
  s.start;
  
  while(true){}
}
Sep 09 2010
prev sibling next sibling parent Bane <branimir.milosavljevic gmail.com> writes:
I triple checked it. 2.0.48 has problems with this, 2.040, 2.045 & 2.047 don't.

 This will throw exception on trying to create socket in derived thread. Socket
created in main thread is ok. Is it some shared issue or... ? I have been
trying to find something info in docs and mailing list but no result.
 
 import std.stdio;
 import core.thread;
 import std.socket;
 
 class MyThread : Thread {
   Socket sock;
   this(){
     super(&run);
   }
   void run(){
     writeln("thread start");
     sock = new TcpSocket; // this will throw exception on 2.047, 2.048
     writeln("thread end");
   }
 }
 
 void main(){
   writeln("main start");
   auto s = new TcpSocket;
   writeln("socket in main thread created");
   auto t = new MyThread;
   t.start;
   writeln("main end");
 }
Sep 07 2010
prev sibling parent Bane <branimir.milosavljevic gmail.com> writes:
Errr... sorry guys.
Joke is on me.

Main threads exits before socket is created in new thread. Simple call to
sleep() fix this.
Sep 07 2010