Re: std::deque Thread Saftey Situtation
This is not so easy to understand - even with correct indentation. And
it is not exception-safe.
Ack, if it is not exception-safe I definally can't use it... Would
you be able to give a situation where it would fail, if you get some
free time. It can be just very basic order of flow, I should be able
to grasp it. Everything I've written is pseudo-code, not my actual
code. I can't get the Indentations right here, and would be too hard
to follow if I just paste, but I can outline the basics:
--Pumping Thread--
WaitForReadSignle();
ReadTCPIP(Buffer, size, ...);
//Copies Buffer, and makes myStruct
m_Q[TCPIP].qLock.Enter();
q.push_back(myStruct);
m_Q[TCPIP].qLock.Leave();
SetEvent(recvieEvent);
--Processing Thread--
WaitForObjects(recvieEvent);
m_Q[TCPIP].qLock.Enter();
while(q.size() > 0) {
myStruct = q.front();
q.pop_front();
m_Q[TCPIP].qLock.Leave();
ProcessData(myStruct);
m_Q[TCPIP].Enter();
}
m_Q[TCPIP].Leave();
Both threads are in loops, and there are stuff I left out for clarity,
but this, more-than-less, is the structure. I do have try{} finally{}
blocks set up (to ensure that for every "Enter()" there is a
"Leave()"). I left them out for clarity. When you said exception-
safe you ment this? Or you ment that the "q" above isn't thread-
safe? Sorry for the confustion, I did leave things out thinking I'd
make it easier for people to read (my confusion was more so the
internal implementation so left out what I thought wasn't needed).
If you see something though I'd be happy to hear about it. I'm very
good with understanding concepts by example (quick, rough, pseudo code
things if you don't mind writing 5 or 10 lines). But, unless you see
a problem, I think the implementation I have is working.
As I said, you should look for condition variables. Perhaps boost has
something for you. I am not sure that they have a producer-consumer
class, but they have the building blocks you need, and quite likely
also an example that you can use as a skeleton.
I am confident that there are also other solutions out there.
I'm unsure what boost is, I do know the basic producer-consumer
models, which I thought I was following (just using critical sections
to protect the "q", in a more basic way; the models usally have the
locking inside the Class, I just wanted to use the std:: class, since
I only use it in two places).
Thanks for your help :)