我试图pthread_join
用指向我的结构的指针调用,以便C线程可以将结构填充到我指向它的内存中.(是的,我知道这是非常不安全的..)
功能签名pthread_join
:
pub unsafe extern fn pthread_join(native: pthread_t,
value: *mut *mut c_void)
-> c_int
Run Code Online (Sandbox Code Playgroud)
我这样做是为了将C代码从一本书移植到Rust.C代码:
pthread_t tid1;
struct foo *fp;
err = pthread_create(&tid1, NULL, thr_fn1, NULL);
err = pthread_join(tid1, (void *)&fp);
Run Code Online (Sandbox Code Playgroud)
我想出了这段代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
但我看到的错误是:
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>`
Run Code Online (Sandbox Code Playgroud)
甚至可以使用演员表实现这一点,还是我需要转换?
代码不能像写的那样工作;那是因为 C 线程并没有真正在您指向的内存中“填充结构”。它负责分配自己的内存(或预先从另一个线程接收)并填充它。C 线程“返回”的唯一内容是单个地址,并且该地址由pthread_join
.
这就是为什么pthread_join
接收 a void **
,即指向 a 的指针void *
。这种输出参数能够pthread_join
存储(返回)void *
由新完成的线程提供的指针。线程可以通过将指针传递给pthread_exit
或从start_routine
传递给返回指针来提供指针pthread_create
。在 Rust 中,可以使用如下代码接收原始指针:
let mut c_result: *mut libc::c_void = ptr::null_mut();
libc::pthread_join(tid1, &mut c_result as *mut _);
// C_RESULT now contains the raw pointer returned by the worker's
// start routine, or passed to pthread_exit()
Run Code Online (Sandbox Code Playgroud)
返回的指针指向的内存的内容和大小是被连接的线程和正在连接它的线程之间的契约问题。如果工作线程是用 C 实现的并设计为由其他 C 代码调用,那么明显的选择是它为结果结构分配内存,填充它,并提供一个指向已分配内存的指针。例如:
struct ThreadResult { ... };
...
ThreadResult *result = malloc(sizeof(struct ThreadResult));
result->field1 = value1;
...
pthread_exit(result);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,加入线程的 Rust 代码可以通过复制 C 结构并获取其所有权来解释结果:
// obtain a raw-pointer c_result through pthread_join as
// shown above:
let mut c_result = ...;
libc::pthread_join(tid1, &mut c_result as *mut _);
#[repr(C)]
struct ThreadResult { ... } // fields copy-pasted from C
unsafe {
// convert the raw pointer to a Rust reference, so that we may
// inspect its contents
let result = &mut *(c_result as *mut ThreadResult);
// ... inspect result.field1, etc ...
// free the memory allocated in the thread
libc::free(c_result);
// RESULT is no longer usable
}
Run Code Online (Sandbox Code Playgroud)
这里有几个问题:
Box
是指向堆分配资源的指针,您可以使用Box::into_raw(some_box)
,c_void
,类型推断可能能够做到这一点让我们让它工作:
// pthread interface, reduced
struct Void;
fn sample(_: *mut *mut Void) {}
// actual code
struct Foo {}
fn main() {
let mut p = Box::into_raw(Box::new(Foo{})) as *mut Void;
sample(&mut p as *mut _);
}
Run Code Online (Sandbox Code Playgroud)
请注意,这是泄漏内存(由于into_raw
),通常应该将内存推回Box
withfrom_raw
以便Foo
调用析构函数并释放内存。