相关疑难解决方法(0)

使用pthreads进行进程间互斥

我想使用一个互斥锁,它将用于同步访问驻留在内存中的一些变量,这两个变量共享两个不同的进程.我怎样才能做到这一点.要执行的代码示例将非常感激.

c linux pthreads

3
推荐指数
2
解决办法
1万
查看次数

当使用`fork`创建的多个C线程的回调函数时,Rust Mutex不起作用

我正在使用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)

multithreading mutex ffi rust

3
推荐指数
1
解决办法
250
查看次数

标签 统计

c ×1

ffi ×1

linux ×1

multithreading ×1

mutex ×1

pthreads ×1

rust ×1