Thread issue

Raynor memphis007fr at yahoo.fr
Wed Jan 9 14:10:26 PST 2008


Sean Kelly a écrit :
> Raynor wrote:
>> Sean Kelly a écrit :
>>> Raynor wrote:
>>>> 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();
>>>           listeningThread.wait();
>>>
>>>
>>> Try adding this.  Phobos applications doesn't bother to wait for 
>>> executing threads before terminating.
>>
>> If i add listeningThread.wait();, my application is blocked.
> 
> If you don't wait for the thread then your application will leave main 
> and exit, possibly without the thread ever running.  This isn't the case 
> in Tango however, as it has daemon and non-daemon threads, much like Java.
> 
> 
> Sean

class Class1
{
     Thread listeningThread;
     Socket listeningSocket;

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

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

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

It works with listeningThread.wait(100). I dont understand why it makes 
an error if the application quit without the thread ever running. If the 
thread never run, normaly it woudn't throw a socket error.


More information about the Digitalmars-d-learn mailing list