我试图使用指向我的结构体的指针调用
"我正在做这个练习,将一本书中的C代码移植到Rust。C代码如下:"
我想出了这段代码:
但我看到的错误是:
这是否只使用强制类型转换就能实现,还是需要使用 transmute 函数?
pthread_join
函数,以便C线程可以将结构体填充到我指向的内存中。(是的,我知道这非常不安全...)
pthread_join
的函数签名如下:pub unsafe extern fn pthread_join(native: pthread_t,
value: *mut *mut c_void)
-> c_int
"我正在做这个练习,将一本书中的C代码移植到Rust。C代码如下:"
pthread_t tid1;
struct foo *fp;
err = pthread_create(&tid1, NULL, thr_fn1, NULL);
err = pthread_join(tid1, (void *)&fp);
我想出了这段代码:
extern crate libc;
use libc::{pthread_t, pthread_join};
struct Foo {}
fn main() {
let tid1:pthread_t = std::mem::uninitialized();
let mut fp:Box<Foo> = std::mem::uninitialized();
let value = &mut fp;
pthread_join(tid1, &mut value);
}
但我看到的错误是:
error[E0308]: mismatched types
--> src/bin/11-threads/f04-bogus-pthread-exit.rs:51:24
|
51 | pthread_join(tid1, &mut value);
| ^^^^^^^^^^ expected *-ptr, found mutable reference
|
= note: expected type `*mut *mut libc::c_void`
found type `&mut &mut std::boxed::Box<Foo>`
这是否只使用强制类型转换就能实现,还是需要使用 transmute 函数?