Prev: Posting a functor to a window - strange rare crash.
Next: Declaring a dynamic pointer to an array of char pointers
From: Larry on 29 Jan 2010 08:53 "Mihajlo Cvetanović" <mcvetanovic(a)gmail> ha scritto nel messaggio news:eNHKJFNoKHA.5344(a)TK2MSFTNGP04.phx.gbl... > Actually "next time" has just come. When Consumer leaves its main loop it > erases the element from eventi map and then also from users map. But in > between the Producer may just find this element in its loop, feed some > data into users map, and call SetEvent with non-existing element! SetEvent > may or may not crash, I don't know and would dare not to test it in > working environment. Ok! what about this? while(1) { // CALLBACK EVENT WaitForSingleObject(hevent, INFINITE); EnterCriticalSection(&users_mutex); int flag = users[threadid].cb.at(0).flag; string line = (char*)users[threadid].cb.at(0).data; ResetEvent(hevent); LeaveCriticalSection(&users_mutex); if(flag == BUFF_DONE) { int ret = s->SendBytes(line + CRLF); if(SOCKET_ERROR == ret) break; } } // Close && Remove "event" and "users" from their maps // EnterCriticalSection(&users_mutex); users.erase(threadid); LeaveCriticalSection(&users_mutex); EnterCriticalSection(&eventi_mutex); eventi.erase(threadid); LeaveCriticalSection(&eventi_mutex); CloseHandle(hevent); > And one more problem connected with previous one, a race condition: > Producer may produce faster than Consumer can consume (even with > Sleep(1000) you are not really safe). The effect: some data is not sent, > because the Consumer sends only lastly produced data. The fix: in Consumer > erase all consumed data, and consume from the end instead from beginning, > and don't ResetEvent if there are more data to consume. Well, since I am using pushing back in the circula buffer what I push is the newest data and so forth. On the Consumer I need to get oldest data...or maybe I ma getting that wrong...
From: Mihajlo Cvetanović on 29 Jan 2010 10:00 Larry wrote: > Ok! what about this? Good :-) > Well, since I am using pushing back in the circula buffer what I push is > the newest data and so forth. On the Consumer I need to get oldest > data...or maybe I ma getting that wrong... When Consumer gets the oldest data (cb[0]) it goes for another spin, and what it gets then? Same thing: cb[0]. Is this what you intended? Maybe you want to discard oldest data after you consume it?
From: Larry on 29 Jan 2010 11:06 "Mihajlo Cvetanović" <mcvetanovic(a)gmail> ha scritto nel messaggio news:O5yaAPPoKHA.5776(a)TK2MSFTNGP04.phx.gbl... > When Consumer gets the oldest data (cb[0]) it goes for another spin, and > what it gets then? Same thing: cb[0]. Is this what you intended? Maybe you > want to discard oldest data after you consume it? well, consider this: cb->push_back(); 1 cb->push_back(); 2 cb->push_back(); 3 cb->push_back(); 4 old->new 1,2,3,4 so I will need in order 1,2,3,4 1 is at index 0, then 2 will be at index 0 etc... that's my view. Also, I should consider calling pop_front() after reding from it (front is index 0)
From: Larry on 30 Jan 2010 15:41 "Larry" <dontmewithme(a)got.it> ha scritto nel messaggio news:4b6307a3$0$825$4fafbaef(a)reader5.news.tin.it... >> When Consumer gets the oldest data (cb[0]) it goes for another spin, and >> what it gets then? Same thing: cb[0]. Is this what you intended? Maybe >> you want to discard oldest data after you consume it? by the way, I was thinking about different approach what about the follwoing? circular_buffer<buffer> cb_local(numbuff); while(1) { WaitForSingleObject(hevent, INFINITE); EnterCriticalSection(&users_mutex); cb_local.swap(users[threadid]); LeaveCriticalSection(&users_mutex); while (!cb_local.empty()) { int ret = s->SendBytes(cb_local.at(0).data); if (SOCKET_ERROR == ret) return ret; cb_local.pop_front(); } } thanks
From: Ulrich Eckhardt on 1 Feb 2010 03:28 Larry wrote: > by the way, I was thinking about different approach what about the > follwoing? > > circular_buffer<buffer> cb_local(numbuff); > > while(1) > { > WaitForSingleObject(hevent, INFINITE); > > EnterCriticalSection(&users_mutex); > cb_local.swap(users[threadid]); > LeaveCriticalSection(&users_mutex); > > while (!cb_local.empty()) > { > int ret = s->SendBytes(cb_local.at(0).data); > if (SOCKET_ERROR == ret) > return ret; > cb_local.pop_front(); > } > } Yep, good approach. Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
First
|
Prev
|
Pages: 1 2 3 4 Prev: Posting a functor to a window - strange rare crash. Next: Declaring a dynamic pointer to an array of char pointers |