• Welcome to Valhalla Legends Archive.
 

C++ thread synchronization

Started by l2k-Shadow, June 30, 2007, 01:52 PM

Previous topic - Next topic

l2k-Shadow

in my program i have one thread which handles incoming data from the socket and one other thread which processes the data. the problem is that the incoming data thread is going faster than the processing thread and therefore calls the processing function before it has a chance to finish.. that results in the outcome to be shit. i need advice on how to optimally model the threading system. If i should implement a semaphore to only call the other thread when it gives an ok signal, or create another thread to process the data, or process the data in the same thread as where i call recv() on the socket.
Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.

brew

Or you could just not use multiple threads at all, hook the socket and call that function on the callback message for receving data.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

l2k-Shadow

#2
lol

that's also not the question though, what if i want to run multiple connections, not just one.
Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.

warz

Quote from: l2k-Shadow on June 30, 2007, 01:52 PM
in my program i have one thread which handles incoming data from the socket and one other thread which processes the data. the problem is that the incoming data thread is going faster than the processing thread and therefore calls the processing function before it has a chance to finish.. that results in the outcome to be shit. i need advice on how to optimally model the threading system. If i should implement a semaphore to only call the other thread when it gives an ok signal, or create another thread to process the data, or process the data in the same thread as where i call recv() on the socket.

Well, in any data handling situation you'll need to know when it's alright to process received data. Create a method of storing unprocessed data in either the receiving thread, or the processing thread, and when you know it's fine to process a complete message, pass the message to the processing function, and clean up the unprocessed data container as necessary. This is the same logic used in just about every current client emulator for bnet - multi-threaded or not.

This avoids one thread sending data to another thread that is trying to read the data as it's received.

l2k-Shadow

yeah, i created a queue system, but i'm not sure why it's not working properly. i'll look through it again. i just wanted ideas.

EDIT: yeah it was an error in my handling of the queue, thx.
Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.

MyndFyre

Why not use a pool for incoming packets?

Doing so allows you the benefit of not allocating and deallocating memory a lot, but you can flag them to be done or re-insert them into queues for reuse and dequeue when necessary.  Then all you need to worry about is synchronous access to your global queue/linked-list, and that's much easier to manage with things like critical sections and spinlocks.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Banana fanna fo fanna

i/o thread to read, buffer, and parse your data. threadsafe queue (with an atomic get() operation) to pass messages from the i/o thread to the logic threads.

Win32

If the problem is that if the data takes considerable time (CPU wise) to process, creating a seperate thread to process it is not going to change a thing. Most likely it will make things worse because you'll have to queue data.

The only thing I can think of is have the processing queue multitask between received data.

For example, I have the same sort thing setup with my D2GS. One thread manages processing incomming messages, one thread manages collision detection/game movement mechanics.

MyndFyre

Quote from: Win32 on July 31, 2007, 07:34 PM
If the problem is that if the data takes considerable time (CPU wise) to process, creating a seperate thread to process it is not going to change a thing. Most likely it will make things worse because you'll have to queue data.

That's not true.  If you have a single thread receiving the data and running it through the logic and updating the UI, you're going to take a lot of overhead in processing each packet.

Queueing is an exceptionally lightweight task.  It's a linked list.  Woooo....
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.