- About C++ and real-time OSs - 3 Updates
- Custom Socket streambuf and Non-blocking IO - 1 Update
- Custom Socket streambuf and Non-blocking IO - 1 Update
| Ramine <toto1@toto1.net>: Apr 02 05:12PM -0400 Hello, I have been working on a queue with a condition variable, but i have noticed that a condition variable does not work correctly, because the threads must be waiting to be able to be signaled, if they are not waiting the signal will be lost, so the way that many are using a condition variable with a FIFO queue is not correct, they are for example using this logic on the consumer side: while(the_queue.empty()) { the_condition_variable.wait(lock); } and this is not correct, because if the_queue_empty() is true , and the last producer thread signals before the consumer thread is waiting for the condition variable , so this will deadlock, so i have avoided condition variables and i have designed and implemented an efficient real-time and thread-safe and bounded FIFO Queue and Stack an i am actually finishing them and they are working well, and i will soon posted them on internet. Thank you, Amine Moulay Ramdane. |
| Ramine <toto1@toto1.net>: Apr 02 05:31PM -0400 On 4/2/2017 5:12 PM, Ramine wrote: > Thank you, > Amine Moulay Ramdane. To solve this problem with a condition variable , of course the signaling of the condition variable must be inside the critical section of the producers. But there is still a problem, it is a problem with the latency in real-time systems, if you use only one pthreads mutex for the producer side and the consumer side , the latency will get much bigger, because the consumers have to wait for the producers in a FIFO priority manner, so you have to wrap the producer side inside another pthreads mutex to lower the latency for the condition variable case, or you have to use smartly a pthreads semaphore. Thank you, Amine Moulay Ramdane. |
| Ramine <toto1@toto1.net>: Apr 02 06:09PM -0400 On 4/2/2017 5:31 PM, Ramine wrote: > Thank you, > Amine Moulay Ramdane. The problem with the latency on real-time systems, can be solved with the condition variable by using two pthreads mutexes, one for the consumer side, and one for the producer side, not just one as is using many. Thank you, Amine Moulay Ramdane. |
| ram@zedat.fu-berlin.de (Stefan Ram): Apr 02 08:04PM >like to have is a read return nothing if no input is pending >(empty string via >> or read return 0), but without the EOF >flag set. I am by no means an expert in this field. But did you check out »27.6.3 Class template basic_streambuf« and »27.6.3.2.3 Get area« for »streamsize in_avail();«? |
| ron.rwsoft@gmail.com: Apr 02 11:29AM -0700 I've created my own socket stream library and I'm now adding a few stream manipulators to set a few of the socket options. Things like keepalive/nokeepalive and block/nonblock. The problem I'm having is with non blocking reading. What I'd like to have is a read return nothing if no input is pending (empty string via >> or read return 0), but without the EOF flag set. Here's my underflow method within my socket streambuf: sockets::socketbuf::int_type sockets::socketbuf::underflow() { if (gptr() >= egptr()) { // The buffer has been exhausted, read more in from the socket. int res = read(sockfd, &_ibuf.front(), _ibuf.size()); if (res == 0) return traits_type::eof(); if (res < 0) { if (errno == EAGAIN or errno == EWOULDBLOCK) { // Non blocked IO. // Reset the input buffer and return ??? setg(&_ibuf.front(), &_ibuf.front(), &_ibuf.front()); return 0; // How is this interrupted by the stream? } else { throw sockets::exception("socket read error"); } } setg(&_ibuf.front(), &_ibuf.front(), &_ibuf.front() + res); } return traits_type::to_int_type(*gptr()); } // Possible example of it's usage (a bit of a bad example). sockets::iocstream httpclient("www.some.server", "http"); httpclient << sockets::nonblock; httpclient << "GET /index.html" << std::endl; std::string results; httpclient >> results; while (result.empty() && !httpclient.eof()) { // Do other stuff here. httpclient >> results; } This functionality is 100% necessary for this library, but could be useful in other places. I do have an have an has_input_pending() method that uses poll, so if it's not possible then no worries. |
| You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment