我正在尝试在使用 pthread_create() 创建的新线程中使用 newSVpvn() 创建 SV。此时我的程序崩溃了。发生崩溃的 C 函数如下所示
void *_inet_aton(void *v_arg) {
SV* rv = &PL_sv_undef;
struct thread_arg *arg = (struct thread_arg *)v_arg;
struct hostent *rslv = gethostbyname(arg->host);
if (!rslv) {
goto RET;
}
if (rslv->h_addrtype == AF_INET && rslv->h_length == 4) {
// !!!CRASH HERE!!!
rv = newSVpvn((char *)rslv->h_addr, rslv->h_length);
}
RET:
free(arg->host);
free(arg);
}
和 XSUB
void
inet_aton(Net_DNS_Native *self, char *host)
CODE:
pthread_t tid;
struct thread_arg *t_arg = malloc(sizeof(struct thread_arg));
t_arg->self = self;
t_arg->host = strdup(host);
pthread_create(&tid, &self->thread_attrs, _inet_aton, (void *)t_arg);
测试示例
use blib;
use Net::DNS::Native;
my $dns = Net::DNS::Native->new();
$dns->inet_aton("google.com");
# wait for a thread
sleep 10;
完整代码可以在这里找到:https ://github.com/olegwtf/p5-Net-DNS-Native/blob/fbc57dbe9e6832afed8d46cd369db6930bbd53bc/Native.xs
那么,有没有可能做我想做的事?