Thread issue

Raynor memphis007fr at yahoo.fr
Mon Jan 7 04:06:21 PST 2008


Hi, I try to make a client/server application but i have issues with 
threads.

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

class Class1
{
     Thread listeningThread;
     Socket listeningSocket;

     this()
     {
         listeningThread = new Thread(&Listen);
         listeningThread.start();
     }

     int Listen()
     {
         listeningSocket = new Socket(AddressFamily.INET, 
SocketType.STREAM, ProtocolType.TCP);
         writefln("socket created\n");
         return (0);
     }
}

void main()
{
     auto class1 = new Class1();
}

The results is a "Error: Unable to create socket"
I know its normal because my program quit imediatly but when the thread 
is destroyed it wouldn't make an error.

I know objects are destroyed by the GC in any order so i tried the 
manual method :

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

class Class1
{
     Thread listeningThread;
     Socket listeningSocket;

     this()
     {
         listeningThread = new Thread(&Listen);
         listeningThread.start();
     }

     ~this()
     {
         delete listeningThread;
     }

     int Listen()
     {
         listeningSocket = new Socket(AddressFamily.INET, 
SocketType.STREAM, ProtocolType.TCP);
         writefln("socket created\n");
         return (0);
     }
}

void main()
{
     auto class1 = new Class1();
}

But it does the same error.

Can you help me please?


More information about the Digitalmars-d-learn mailing list