Thread for whole Runtime

Hi @ all,

how can I program a thread which is working over the whole runtime of application and this in background?
I need this thread to catch some network incoming message, also if a server sends me some message the thread shall notice this incoming. But if there is nothign to receive the main functions should be work on.
I hope you know what I mean.

Greetings
CrazyPlaya
@firedraco: Threads, not spawning external processes.

@CrazyPlaya:
Multi-threaded code is difficult to implement. If you can re-design your code so that you don't need it to be threaded that would be my first suggestion. You can simply poll the network at intervals for new messages and then put them into an array for other parts of the program to access etc (in much the same way game's do).

If you went down the multi-threaded path. You need to pick a threading system to use. I find Boost Threads to be quite an easy one to use, and it offers all of the components your going to require.

Now, your going to need to create a threaded class that will handle the network operations for you. You will also need to declare any area in your application for it to store the information coming in from the network. Then you have to start putting in the locks to ensure you have no concurrent read/writes of memory.

Link: http://www.boost.org/doc/libs/1_35_0
Hi Zaita,

i will explain you my problem. I call t a server to login and build a session. Then the server gives me some Message that I have to answer. At least I get an OK. If i get this OK i want to do something but in the meantime it could be the server sends me a CANCEL then I have to stop the other work. What is the best way to solve this problem? My intention was that I have a thread that runs all time and proof the incoming messages and if the session was created he gives a answer to the main thread that this can work on and if the server gets the cancel the main thread stops his work and and goes back to the begin state.
If this is a sequential set of events you could structure your program like:

-> Start Application
-> Connect To Server
-> Send Info to Start Login
-> Get message from server
-> send username/password
-> Get question from server
-> Send answer
etc etc

This wouldn't require a thread, it's a simple approach and not the most elegant way to solve the problem.

If you want to go down the more elegant path, then you should browse through this thread: http://www.cplusplus.com/forum/unices/3773/ and see how discussion on multi-threaded development. There is a lot more difficulty in this approach.

On a side note,
if this is a Window's application with a GUI. Then you can sorta cheat to achieve this. Have a timer on the form that checks for new information from the network every 1 second, and have another text box you can enter information into to send back etc. Something like that anyways, it's a simple solution and should avoid most of the multi-threading issues with concurrency.



But the adoption of timer are difficult because i would need more than one timer. One timer for every 20ms checking the socket x one that sends all specified seconds a request and one who looks every second on socket y.
The second problem is that timer supports 55ms at minimum and it is not assured that this will be exact. But for proofing socket x it must work all 20ms second with out any fluctuation. This is my intention to use threads.
Thread timing is not assured either. Even with you use Sleep(X) it's not assured that the thread will resume in X milliseconds.
Topic archived. No new replies allowed.