Here I am try to get xpc reply within a second. I don't want to block the whole application so I am try to check the result after 1 second. but it always crash in sleep(1)
. Does anyone know what is the best way to do like this?
I tried sleep(1)
, std::this_thread::sleep_for(2s);
and dispatch_group_wait
but no luck. Every time when the thread wake up it crash, following is the code I call xpc and retrive the value after 1 second.
// send message
static int result = ETIME;
xpc_connection_send_message_with_reply(mConn, msg, NULL, ^(xpc_object_t reply){
if (reply == XPC_ERROR_CONNECTION_INVALID || reply == XPC_ERROR_CONNECTION_INTERRUPTED) {
result = ENOTCONN;
}
if (xpc_get_type(reply) != XPC_TYPE_DICTIONARY) {
result = EINVAL;
}
result = (int)xpc_dictionary_get_int64(reply, sResult);
xpc_release(reply);
printf("leave\n");
});
// wait 1 second for result
//dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC);
std::this_thread::sleep_for(2s); // crash happened