Re: Piping between 2 threads

From: Willem (willem_at_stack.nl)
Date: 02/06/05


Date: Sun, 6 Feb 2005 13:59:51 +0000 (UTC)

Aslan wrote:
) I have implemented piping between 2 threads of the same process. It is
) working the way described below but I would like to see if there is a better
) way to do it.
)
) I have the following pipe buffer shared between threads.
)
) struct PipeBuf
) {
) char* pBuf;
) unsigned int len;
) };
) Here len = 0 indicates the end of input for the reader side.
)
) I create two event objects (I call them read and write event) to synchronise
) the threads.
) I will use R for reader side and W for writer side. The events objects are
) initialized as non-signaled and automatically set to non-signaled when the
) thread waiting on them is released right after they are set to signaled by
) the other thread.
)
) The following happens:
) 1 - R starts waiting on write event.
) 2 - W sets the pipe buffer members and sets the write event and starts
) waiting on the read event. R wakes up and processes the pipe data and sets
) the read event and starts waiting on the write event again if the pipe
) buffer length is not zero.
) 3 - W wakes up and repeat the step 2 if it has more data to pipe, otherwise
) W sets the pipe buffer length to zero and sets the write event to terminate
) the R.
)
) My restiction is that the pipe buffer definition should not be altered. That
) way it avoids using an intermediary buffer. I mean W may call a library
) function to fill it and R also may call a library function to use it. The
) buffer pointed to by the pointer (pBuf member) can be set to anything by W.
) A pointer to PipeBuffer instance is shared between W and R. So when W sets
) pBuf and len members and signals the write event, it is ready to be used by
) R right away.
)
) Now my question: Can this be done some other (better) way? Any suggestion?

I think so. Use a (small) ringbuffer of buffer pointers.

That way, W can go on with its business instead of having to wait on R.
If you don't make the ringbuffer too large, then W won't get too far
ahead of R, and won't have too much memory allocated.

Restriction: All buffer pointersshould be from malloc() calls, so that R
can free them. Alternatively, you could add a destructor function pointer
to the struct so that R can call that to clean up.

SaSW, Willem

-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


Relevant Pages

  • Re: serving a file to a client --- background fcopy read fileevent event puts socket cha
    ... invokes a callback when there's both data waiting and somewhere to ... probably not -size (although having -size pre-load the input buffer ... you can't even observe the progress of the [fcopy] from outside (ie. ... The division of functionality I mentioned should have negligible impact ...
    (comp.lang.tcl)
  • Completion ports and sockets - scaling my app
    ... First chunk 1 is put on his way by WSASend. ... Such an application must, however, take great care to ensure that it posts multiple overlapped sends, instead of waiting for one overlapped send to complete before posting another. ... If it had another buffer already posted, the transport would be able to use that buffer immediately and ... By implementing some kind of sequence numbering scheme? ...
    (microsoft.public.win32.programmer.networks)
  • XFS related hang (was Re: How to send a break? - dump from frozen 64bit linux)
    ... we're waiting here on a synchronisation variable that'll ... that'd be called by the I/O completion handler on the buffer ... waiting for the driver to wake us up when its done. ... we aren't seeing the I/O completion here for some reason... ...
    (Linux-Kernel)
  • [RFC 0/6] TCP socket splice
    ... It allows a TCP socket to be spliced to a file via a pipe buffer. ... Using TCP socket splice: ...
    (Linux-Kernel)
  • Re: [PATCH] sys_vmsplice
    ... On Fri, 21 Apr 2006, Jens Axboe wrote: ... Here's the first implementation of sys_vmsplice. ... I think we'll have to think a bit more about the buffer size issues: ... Ie there may be more buffering after the local pipe buffer - other pipes, ...
    (Linux-Kernel)