digitalmars.D - threads: Waiting for event & shutdown socket
- Benjamin Schulte (9/9) May 30 2007 Hi!
- Sean Kelly (8/15) May 30 2007 Depends. Thread.sleep() in Tango calls SleepEx(INFINITE,TRUE) on Win32,...
- Benjamin Schulte (17/17) May 30 2007 Well, for question 1: I want to have the thread to be paused until I set...
- Regan Heath (11/38) May 30 2007 You make the socket non-blocking and change your loop to something like:
- Regan Heath (5/44) May 30 2007 Slight correction, your loop should be
Hi! Just two other problems - now coming with threads. First: Is there something like the WinAPI method 'WaitForSingleObject'? Some methods like while( threadEventIsSet ) { Sleep(10); } is not the best solution I guess. Well, and another problem. I've created a server socket in a thread. I set blocking to 'true' and now I'm waiting for connections in "accept()". Well, however it's possible that the thread should be closed while it's accepting. How can I cancel that waiting-for-users?
May 30 2007
Benjamin Schulte wrote:Hi! Just two other problems - now coming with threads. First: Is there something like the WinAPI method 'WaitForSingleObject'?Depends. Thread.sleep() in Tango calls SleepEx(INFINITE,TRUE) on Win32, which will be interrupted when IOCP events occur and such. For something more general I think you will probably want something like a condition variable.I've created a server socket in a thread. I set blocking to 'true' and now I'm waiting for connections in "accept()". Well, however it's possible that the thread should be closed while it's accepting. How can I cancel that waiting-for-users?Why would the thread be closed while it's accepting? Or did you mean the socket would be closed? Sean
May 30 2007
Well, for question 1: I want to have the thread to be paused until I set a variable to TRUE (for example). Don't exactly know how the SleepEx method should work. And as soon as I use winAPI methods, I also could use WaitForSingleObject. Question 2: I have the methods: startServer( ) which creates a thread that runs the server and handles everything there. closeServer( ) this should KILL the thread. Wherever it is at the moment. the thread itself just has a structure like (it's a one-client-server, cause I never would need more in this case): - create socket - bind socket and listen while( true ) { - accept - handle messages } And for the case, no user connected to the server it will block at "accept". When I call 'closeServer' it should close the thread, even if it's in the accept method. Hopefully I was able to explain it a bit exactlier now. I know, I have a bad english ;)
May 30 2007
Benjamin Schulte Wrote:Question 2: I have the methods: startServer( ) which creates a thread that runs the server and handles everything there. closeServer( ) this should KILL the thread. Wherever it is at the moment. the thread itself just has a structure like (it's a one-client-server, cause I never would need more in this case): - create socket - bind socket and listen while( true ) { - accept - handle messages } And for the case, no user connected to the server it will block at "accept". When I call 'closeServer' it should close the thread, even if it's in the accept method. Hopefully I was able to explain it a bit exactlier now. I know, I have a bad english ;)You make the socket non-blocking and change your loop to something like: while(true) { try { - accept - handle messages } catch(SocketAcceptException e) { Sleep(1); //prevent a hard-loop when there is no socket to accept } } Regan
May 30 2007
Regan Heath Wrote:Benjamin Schulte Wrote:Slight correction, your loop should be while(!stopping) ..etc. and closeServer should set stopping = true; and then wait for the thread to exit normally with join(); ReganQuestion 2: I have the methods: startServer( ) which creates a thread that runs the server and handles everything there. closeServer( ) this should KILL the thread. Wherever it is at the moment. the thread itself just has a structure like (it's a one-client-server, cause I never would need more in this case): - create socket - bind socket and listen while( true ) { - accept - handle messages } And for the case, no user connected to the server it will block at "accept". When I call 'closeServer' it should close the thread, even if it's in the accept method. Hopefully I was able to explain it a bit exactlier now. I know, I have a bad english ;)You make the socket non-blocking and change your loop to something like: while(true) { try { - accept - handle messages } catch(SocketAcceptException e) { Sleep(1); //prevent a hard-loop when there is no socket to accept } }
May 30 2007