Re: FCGX_Accept_r bad design?

Robert Jordan <[email protected]>
Newsgroups gmane.comp.web.fastcgi.devel
Message-ID <[email protected]>
stefano marengo wrote:
> I am new to fastcgi, I'm trying to run a simple threaded application
> like described in:
> http://www.fastcgi.com/archives/fastcgi-developers/2005-March/003627.html
> 
> I want a single accept loop and then some worker threads (or thread-pool).
> 
> FCGX_Request FcgiReq;
> FCGX_InitRequest(&FcgiReq, 0, 0);
> while (FCGX_Accept_r(&FcgiReq) >= 0)
> {
>      pcreate_thread(&WorkerThread, 0, (void*)&WorkerThreadFunc, 0);
> }
> ...
> WorkerThreadFunc(...)
> {
>      // Do the work.
> }
> 
> But this can't work because FCGX_Accept_r overwrites FCGX_Request being
> processed by the worker!


Try this:

for (;;) {
     FCGX_Request *req = calloc (1, sizeof (FCGX_Request));
     FCGX_InitRequest (req, 0, 0);

     if (FCGX_Accept_r (req) < 0)
	break;

     pcreate_thread (&WorkerThread, 0, (void*)&WorkerThreadFunc, req);
}

The code assures that "req" is always local to the current request.

In your WorkerThreadFunc you must call

	FCGX_Finish_r (req);
	free (req);

upon termination.

Robert

___________________________________
fastcgi-developers mailing list
http://fastcgi.com/fastcgi-developers/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.