digitalmars.D.learn - Thread issue
- Raynor (61/61) Jan 07 2008 Hi, I try to make a client/server application but i have issues with
- Sean Kelly (5/21) Jan 07 2008 listeningThread.wait();
- Raynor (2/27) Jan 09 2008 If i add listeningThread.wait();, my application is blocked.
- Sean Kelly (5/30) Jan 09 2008 If you don't wait for the thread then your application will leave main
- Raynor (26/59) Jan 09 2008 class Class1
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?
Jan 07 2008
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. Sean
Jan 07 2008
Sean Kelly a écrit :Raynor wrote:If i add listeningThread.wait();, my application is blocked.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. Sean
Jan 09 2008
Raynor wrote:Sean Kelly a écrit :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. SeanRaynor wrote:If i add listeningThread.wait();, my application is blocked.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.
Jan 09 2008
Sean Kelly a écrit :Raynor wrote: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.Sean Kelly a écrit :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. SeanRaynor wrote:If i add listeningThread.wait();, my application is blocked.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.
Jan 09 2008