--- David Schwartz <[EMAIL PROTECTED]> wrote:
>
> > Ah, well, I meant that a state machine must store state
> > explicitly, whereas
> > with threaded code the state is implied in the code flow (in effect, the
> > thread system itself is a state machine.) If each thread
> > executes a simple
> > function like "void foo() {A; B; C;}", then the equivalent state
> > machine is
> > simple enough -- it just has to remember whether each "thread" is
> > at step A,
> > B, or C, using an array of state variables or some such. But now
> > throw in a
> > few nested for's, if's, and local data into the above function...
> > well, you
> > get the picture. I could certainly implement my program as a
> > state machine,
> > but that may make my code harder for others to understand/maintain.
>
> Personally, I completely disagree. It is very hard to understand control
> flow when all the state is hidden. You see a 'return' statement -- where
> does that go? What if you want to log the state of a connection to
> facilitate debugging? Hiding the state on the stack is not good practice.
I don't entirely understand what you're saying -- is this a general argument
against threads? I thought the whole point of Pth was to store thread state
on the stack...? Well, maybe it would help me understand if we used a
concrete example:
void *threadMain(void *_arg) {
char inputBuff[BUFFLEN], moreInput[BUFFLEN];
int client_fd = (int)_arg;
pth_readline(client_fd, inputBuff, BUFLEN);
if (strncmp(inputBuff, "GET ", 4) == 0) {
<do some stuff>
pth_write(client_fd, <response>);
} else if (strncmp(inputBuff, "PUT ", 4) == 0) {
pth_readline(client_fd, moreInput, BUFLEN);
<do some stuff>
pth_write(client_fd, <response>);
}
}
Okay, so this is a really simple routine executed by worker threads to
process a line or two of input from a client and send a response. Is this
bad coding style? Would you do this with a single-threaded state machine of
some sort? Or some other way?
__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com
______________________________________________________________________
GNU Portable Threads (Pth) http://www.gnu.org/software/pth/
Development Site http://www.ossp.org/pkg/lib/pth/
Distribution Files ftp://ftp.gnu.org/gnu/pth/
Distribution Snapshots ftp://ftp.ossp.org/pkg/lib/pth/
User Support Mailing List [EMAIL PROTECTED]
Automated List Manager (Majordomo) [EMAIL PROTECTED]