我想使用一个互斥锁,它将用于同步访问驻留在内存中的一些变量,这两个变量共享两个不同的进程.我怎样才能做到这一点.要执行的代码示例将非常感激.
我正在使用C库Cuba,该库使用从C中创建的多个线程调用的回调函数。Cuba并行化基于fork/ waitPOSIX函数而不是pthreads(arxiv.org/abs/1408.6373)。它在core参数中给出当前线程。
我正在尝试将此回调函数的结果记录到屏幕和文件中。如果我使用println!,则会得到预期的输出,但是如果slog使用Mutex漏极,则输出会被扭曲。如果使用async漏极,则根本没有输出。
Mutex因为无法看到该函数实际上是从另一个线程调用的,所以不锁定吗?我试图用Rust线程重新创建问题,但是不能。最好是我想花async些功夫。
下面是提供有问题的行为的示例程序。回调获取vegas函数的最后一个参数作为其参数之一。这是记录器克隆的向量。这样,每个内核都应拥有自己的记录器副本:
#[macro_use]
extern crate slog;
extern crate cuba;
extern crate slog_term;
use slog::Drain;
// this function is called from different c threads
// `core` indicates which thread
fn integrand(
_x: &[f64],
_f: &mut [f64],
loggers: &mut Vec<slog::Logger>,
_nvec: usize,
core: i32,
) -> Result<(), &'static str> {
info!(loggers[core as usize], "A\nB\nC");
Ok(())
}
fn main() { …Run Code Online (Sandbox Code Playgroud)