Advertisement

how to comunicate between threads

Started by August 24, 2001 10:00 PM
5 comments, last by wrathgame 23 years, 5 months ago
Hiya all, Just a quick question.. im still relativly new to threads and i was wondering.. I have a thread which recieves data into a buffer. I then want the main thread to read that data out and remove the data from the queue my question is how do i tell the main thread to read the buffer ?? the only thing i can really think of is sending a message sendMessage() to the apps hwnd which when it recieves it then acuires a critical section and reads the global buffer and de-queues it. please let me know if thats the correct way or if there is a better way ;-) Thanks -Tim
~ Tim
If the main thread is the UI thread, just poll the buffer every 50ms or so. I usually just do SetTimer(1, 50) and check for UI updates in the timer handler. The GDI is rather lackluster in its performance, and the user can''t really see anything happen faster than every 50ms anyway, so you''re wasting CPU time if you update more frequently.

If it''s a graphics rendering thread, again poll the buffer - check once a frame if it''s really important, or keep track of the elapsed time and check once every 20ms or so.

If it''s some other high performance thread, you can create an event and signal it whenever there is new data in the buffer – the other thread can then wait on the event and will wake-up immediately to handle the new data when it’s signaled.


Magmai Kai Holmlor
- Not For Rent
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
thanks for that..

the problem is ..

its a server so i want it to be as quick as possible (and use as little resources as pos) and also its being sent to the main thread which will be doing other things so i cant put it on wait

Thanks in advance

Tim
~ Tim
In that case, you probably want a named event that can be shared across applications--your server & client are two applications, correct? Even if not, an event is probably what you want. Look up CreateEvent and WaitForSingleObject/WaitForMultipleObjects.

If you''re going to use events, you''ll probably want a "kill" event too to signal your server thread to shutdown. In that case, the main loop of your server thread would look like this (pseudocode):
while (WaitForMultipleObject (KillEvt & WakeupEvt) != KillEvt)
{
// got the WakeupEvt
doServerStuff;
}

The actual syntax of WaitForMultipleObjects makes you build an array and compare the return value against an offset to see which event was signalled, but I''m sure you can piece it together from the docs. BTW, make sure you wait for any event, not all events, if you use this.

If you don''t want the Kill event in there, just have the loop WaitForSingleObject. I think they''re both the same speed, but haven''t tested it. Anyway, this will give you very fast response time as long as your server thread is a higher priority than client threads. In my experience, parallel threads with the same priority will run for quite a while until they hit a WaitForSomething. In other words, unless your client thread rests after signalling this event, the server thread might have to wait for as long as a timeslice before running.
Hi... thanks for the responce.. but again its not what i need,

possibly i have not explained enough...

* This is on the server (not the client and the client is seperate on other machines.. its a game server BTW)

* This is a component im writing so others can use it (its a IOCP tcp server socket BTW)

* There are X worker threads which wait for data to be recieved from a socket

* When a worker thread recieves data it grabs a critical section for the buffer concerned and stores it there.

I then need to fire an event on the main component in the main apps thread (could be anything because its user defined so i cant rely on it being thread safe.. which is why it should be executed in the main thread)

Currently i send a windows message to the component from the wroker thread.. when the main thread recieves the user defined message it knows that LParam of the message points to a Buffer which it then passes to the users "OnClientWrite" event which can use the data how ever it wishes

Please please let me know if there is a good way i can do that .. i cant put the thread on wait because its the main thread... and the user could be doing 101 other things in it :-/ thanks

Tim
~ Tim
Then re-read my message, and replace the word "client" with "worker thread", and that should be what you need.
Advertisement
quote:
Original post by Stoffel
Then re-read my message, and replace the word "client" with "worker thread", and that should be what you need.


Hi Stoffel,

yeh i did read the post the 1st time and again ;-) i have used WaitForMultipleObject etc ;-)

the prob is where would i put that code.. this is a component so if the component did have a wait on an event it would block the rest of the thread.. so thats no use and i also dont want the user to have to call a polling procedure every x millisecs to check for data

Thanks

~Tim
~ Tim

This topic is closed to new replies.

Advertisement