创建 *mut *mut 到一个结构
Create *mut *mut to a struct
我正在尝试使用指向我的结构的指针调用 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>`
是否有可能仅使用转换来实现这一点,还是我需要转化?
代码不能像写的那样工作;那是因为 C 线程并不真正 "fill in the struct" 在你指向的内存中。它负责分配自己的内存(或事先从另一个线程接收)并填充它。 C 线程 "returns" 唯一的东西是一个单一的 地址 ,这个地址由 pthread_join
.
获取
这就是 pthread_join
接收 void **
的原因,即指向 void *
的指针。这种 输出参数 使 pthread_join
能够存储 (return) 刚完成的线程提供的 void *
指针。线程可以通过将指针传递给 pthread_exit
或通过 return 从 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()
returned 指针指向的内存的内容和大小是正在加入的线程和正在加入它的线程之间的契约问题。如果工作线程是用 C 实现的并且设计为由其他 C 代码调用,那么一个明显的选择是为结果结构分配内存,填充它,并提供指向已分配内存的指针。例如:
struct ThreadResult { ... };
...
ThreadResult *result = malloc(sizeof(struct ThreadResult));
result->field1 = value1;
...
pthread_exit(result);
在这种情况下,加入线程的 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
}
这里有几个问题:
Box
是指向 heap-allocated 资源的指针,您可以使用 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 _);
}
请注意,这是内存泄漏(由于 into_raw
),通常应将内存推回到 Box
中,并为 [=17 的析构函数提供 from_raw
=] 被调用,内存被释放。
我正在尝试使用指向我的结构的指针调用 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>`
是否有可能仅使用转换来实现这一点,还是我需要转化?
代码不能像写的那样工作;那是因为 C 线程并不真正 "fill in the struct" 在你指向的内存中。它负责分配自己的内存(或事先从另一个线程接收)并填充它。 C 线程 "returns" 唯一的东西是一个单一的 地址 ,这个地址由 pthread_join
.
这就是 pthread_join
接收 void **
的原因,即指向 void *
的指针。这种 输出参数 使 pthread_join
能够存储 (return) 刚完成的线程提供的 void *
指针。线程可以通过将指针传递给 pthread_exit
或通过 return 从 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()
returned 指针指向的内存的内容和大小是正在加入的线程和正在加入它的线程之间的契约问题。如果工作线程是用 C 实现的并且设计为由其他 C 代码调用,那么一个明显的选择是为结果结构分配内存,填充它,并提供指向已分配内存的指针。例如:
struct ThreadResult { ... };
...
ThreadResult *result = malloc(sizeof(struct ThreadResult));
result->field1 = value1;
...
pthread_exit(result);
在这种情况下,加入线程的 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
}
这里有几个问题:
Box
是指向 heap-allocated 资源的指针,您可以使用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 _);
}
请注意,这是内存泄漏(由于 into_raw
),通常应将内存推回到 Box
中,并为 [=17 的析构函数提供 from_raw
=] 被调用,内存被释放。