0

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

我的线程函数

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.1\r\n"\
            "HOST: %s\r\n" \
            "Accept: */*\r\n"\
            "Connection: keep-alive\r\n"\
            "Content-Length: %d\r\n"\
            "Content-Type: application/x-www-form-urlencoded\r\n\n%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;
}
4

1 回答 1

0

如果您的异步请求完成不会阻止当前请求的完成,则不能使用 r 或 r->pool。否则你的内存很可能会被重用,导致段错误。

(如果它确实阻止了请求完成,则可能没有理由异步进行)

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

于 2016-08-01T01:51:43.090 回答