FastCGI的并发处理——官网threaded例子

http://andylin02.iteye.com/blog/650609

官网源码:http://www.fastcgi.com/devkit/examples/threaded.c

我还没找到异步处理的方式。如果有异步的实现方式,那就可以单线程异步处理多个并发请求了。


不过我在FastCGI的sample里面找到了多线程的方式,多线程的方式封装一下应该也可以达到异步的效果的。比如,doit()线程把FCGX_Request request丢给另一个线程A处理,然后doit()线程阻塞的等待线程A的返回结果。那么线程A实际上就可以采取异步的方式来处理请求了。


以下是FastCGI的sample里面的多线程实现:


Cpp代码   收藏代码
  1. /* 
  2.  * threaded.c -- A simple multi-threaded FastCGI application. 
  3.  */  
  4.   
  5. #ifndef lint  
  6. static const char rcsid[] = "$Id: threaded.c,v 1.9 2001/11/20 03:23:21 robs Exp $";  
  7. #endif /* not lint */  
  8.   
  9. #include "fcgi_config.h"  
  10.   
  11. #include <pthread.h>  
  12. #include <sys/types.h>  
  13.   
  14. #ifdef HAVE_UNISTD_H  
  15. #include <unistd.h>  
  16. #endif  
  17.   
  18. #include "fcgiapp.h"  
  19.   
  20.   
  21. #define THREAD_COUNT 20  
  22.   
  23. static int counts[THREAD_COUNT];  
  24.   
  25. static void *doit(void *a)  
  26. {  
  27.     int rc, i, thread_id = (int)a;  
  28.     pid_t pid = getpid();  
  29.     FCGX_Request request;  
  30.     char *server_name;  
  31.   
  32.     FCGX_InitRequest(&request, 0, 0);  
  33.   
  34.     for (;;)  
  35.     {  
  36.         static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER;  
  37.         static pthread_mutex_t counts_mutex = PTHREAD_MUTEX_INITIALIZER;  
  38.   
  39.         /* Some platforms require accept() serialization, some don't.. */  
  40.         pthread_mutex_lock(&accept_mutex);  
  41.         rc = FCGX_Accept_r(&request);  
  42.         pthread_mutex_unlock(&accept_mutex);  
  43.   
  44.         if (rc < 0)  
  45.             break;  
  46.   
  47.         server_name = FCGX_GetParam("SERVER_NAME", request.envp);  
  48.   
  49.         FCGX_FPrintF(request.out,  
  50.             "Content-type: text/html "  
  51.             " "  
  52.             "<title>FastCGI Hello! (multi-threaded C, fcgiapp library)</title>"  
  53.             "<h1>FastCGI Hello! (multi-threaded C, fcgiapp library)</h1>"  
  54.             "Thread %d, Process %ld<p>"  
  55.             "Request counts for %d threads running on host <i>%s</i><p><code>",  
  56.             thread_id, pid, THREAD_COUNT, server_name ? server_name : "?");  
  57.   
  58.         sleep(2);  
  59.   
  60.         pthread_mutex_lock(&counts_mutex);  
  61.         ++counts[thread_id];  
  62.         for (i = 0; i < THREAD_COUNT; i++)  
  63.             FCGX_FPrintF(request.out, "%5d " , counts[i]);  
  64.         pthread_mutex_unlock(&counts_mutex);  
  65.   
  66.         FCGX_Finish_r(&request);  
  67.     }  
  68.   
  69.     return NULL;  
  70. }  
  71.   
  72. int main(void)  
  73. {  
  74.     int i;  
  75.     pthread_t id[THREAD_COUNT];  
  76.   
  77.     FCGX_Init();  
  78.   
  79.     for (i = 1; i < THREAD_COUNT; i++)  
  80.         pthread_create(&id[i], NULL, doit, (void*)i);  
  81.   
  82.     doit(0);  
  83.   
  84.     return 0;  
  85. }  
 


另外,FCGX_Accept_r()可以在FCGX_Finish_r()之前连续accept多个request,这应该也代表着一种异步的方法。不过需要构造多个request对象给FCGX_Accept_r()。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值