Threading in Apache2.2



我正在使用线程在服务器上发布数据。线程工作正常,数据在服务器上接收,但每次我都会收到每个请求的分段故障/核心转储异常

我的线程函数

void* APR_THREAD_FUNC thread_init(apr_thread_t *thd,void *data)
{
DATA_INFO_SOCK *dt=(DATA_INFO_SOCK*)data;
apr_status_t result;
result=apr_socket_sendto(dt->new_sock,dt->addr,0,dt->payload,dt->len);
if(result==APR_SUCCESS)
{
 result=apr_socket_recvfrom(dt->addr,dt->new_sock,0,dt->resp,dt->rec_len);
}
result=apr_socket_close(dt->new_sock);
apr_thread_exit(thd, APR_SUCCESS);
}

我的线程启动C代码

int post_data_async(request_rec *r, char *host,char *encoded_data,int timout)
{
apr_status_t result;
apr_thread_t *thrd;
apr_threadattr_t *thrdattr;
char *final_msg;
apr_socket_t *new_sock;
apr_sockaddr_t *addr;
DATA_INFO_SOCK *dt=apr_palloc(r->pool,sizeof(DATA_INFO_SOCK));
char *hostname=host;
char *msg = "POST /getRequestData HTTP/1.1rn"
            "HOST: %srn" 
            "Accept: */*rn"
            "Connection: keep-alivern"
            "Content-Length: %drn"
            "Content-Type: application/x-www-form-urlencodedrnn%s";
apr_port_t port_no=80;
apr_interval_time_t tmout=timout;
apr_size_t *size=apr_palloc(r->pool,sizeof(apr_size_t));
apr_size_t *rcv=apr_palloc(r->pool,sizeof(apr_size_t));
final_msg=apr_psprintf(r->pool,msg,host,strlen(encoded_data),encoded_data);
result=apr_socket_create(&dt->new_sock,APR_INET,SOCK_STREAM,APR_PROTO_TCP,r->pool);
result=apr_sockaddr_info_get(&dt->addr,hostname,APR_INET,port_no,0,r->pool);
result=apr_socket_connect(dt->new_sock,dt->addr);
//result=apr_socket_timeout_set(dt->new_sock,tmout);
*size=strlen(final_msg);
//data preparation for thread
dt->r=r;
dt->resp=apr_palloc(r->pool,sizeof(char)*1024);
dt->payload=final_msg;
dt->len=size;
dt->rec_len=rcv;
result=apr_thread_create(&thrd,NULL,thread_init,(void*)dt,r->pool);
ap_rputs("resule after thread",r);
return 0;
}

如果异步请求完成没有阻止当前请求的完成,则不能使用r或r->池。否则,您的内存可能会被重用,从而导致segfault。

(如果它确实阻止了请求完成,那么可能没有什么理由异步完成)

如果你需要在异步工作项中使用APR代码,你应该为它创建一个(非r->池子池)池。

相关内容

  • 没有找到相关文章

最新更新