Re: Piping between 2 threads
From: Willem (willem_at_stack.nl)
Date: 02/06/05
- Next message: Bill Godfrey: "Re: A function that reads a webpage?"
- Previous message: Randy Howard: "Re: Algorithm book recommendation?"
- In reply to: Aslan Kral: "Piping between 2 threads"
- Next in thread: Aslan Kral: "Re: Piping between 2 threads"
- Reply: Aslan Kral: "Re: Piping between 2 threads"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Bill Godfrey: "Re: A function that reads a webpage?"
- Previous message: Randy Howard: "Re: Algorithm book recommendation?"
- In reply to: Aslan Kral: "Piping between 2 threads"
- Next in thread: Aslan Kral: "Re: Piping between 2 threads"
- Reply: Aslan Kral: "Re: Piping between 2 threads"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|