September 07, 2010
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");
}
September 07, 2010
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.
September 07, 2010
> Please show your environment. Windows?

WinXP sp2, phenom quad core.
September 07, 2010
This is what I get:

"std.socket.SocketException: Unable to create socket"
September 07, 2010
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");
> }

September 07, 2010
Errr... sorry guys.
Joke is on me.

Main threads exits before socket is created in new thread. Simple call to sleep() fix this.
September 07, 2010
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.
September 09, 2010
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){}
}

Top | Discussion index | About this forum | D home