小编gek*_*mad的帖子

C++ 14 shared_timed_mutex VS C++ 11互斥锁

我有一个8个线程之间的共享哈希表(我是一个8核PC),每个线程在哈希表中读写.

在示例1中,我使用了经典的互斥锁,并且在示例2中所有8个核心都是100%我使用了shared_timed_mutex,因为读取访问可以在竞争中但是所有8个核心都在40%

问题出在哪儿?

example 1:
mutex mutex_hash;

-- thread --
mutex_hash.lock();
//read
mutex_hash.unlock();
..
mutex_hash.lock();
//write
mutex_hash.unlock();
Run Code Online (Sandbox Code Playgroud)

============================

example 2:
shared_timed_mutex mutex_hash;

-- thread --
mutex_hash.lock_shared();
//read
mutex_hash.unlock_shared();
..
mutex_hash.lock();
//write
mutex_hash.unlock();
Run Code Online (Sandbox Code Playgroud)

c++ multithreading mutex c++14

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

Apache Kafka - 自动提交 = false 并且没有提交

我的生产者用键 0,1,2,3,4,5,6,7,8,9 写了 10 条消息

我的消费者(使用自动提交 = false)在第一次循环期间读取消息但是当读取消息“2”时我强制异常所以我不提交。(异常仅在第一个循环中抛出 - 有标志 forceError)

在第二个循环中,消费者应该读取所​​有消息 0,1,2,3,4,5,6,7,8,9 并提交,但事实并非如此,为什么?

日志是这样的:

write message with key: 0
write message with key: 1
write message with key: 2
write message with key: 3
write message with key: 4
write message with key: 5
write message with key: 6
write message with key: 7
write message with key: 8
write message with key: 9

1 loop, consumerRecords.count: 10
read message with key: 0
read message with key: 1
read …
Run Code Online (Sandbox Code Playgroud)

apache-kafka kafka-consumer-api

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

带模板的 C++ Lambda

我想用 const 模板值调用 lambda,在下面的示例中,我应该lambdaTemplate正确编写

#include <iostream>

using namespace std;

template<int bar, int baz>
int foo(const int depth) {
    return bar + baz + depth;
}

int main() {
    ////// call function
    cout << foo<1, 2>(10) << endl; // 13

    ///// call lambda without template value
    const auto lambda1 = [&](const int v) {
        return foo<1, 2>(v);
    };
    cout << lambda1(10) << endl; // 13

    ///// call lambda with template value
    const auto lambdaTemplate = [&]<A,B ??? >(const int …
Run Code Online (Sandbox Code Playgroud)

c++ lambda templates c++20

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

共享线程数组

slaves_th是一个共享的线程数组,下面我想join在每个线程上调用,但编译器抛出错误slaves_th.lock().unwrap()不是迭代器。

let slaves_th: Arc<Mutex<Vec<JoinHandle<()>>>> = Arc::new(Mutex::new(Vec::new()));

for _ in 0..8 {
    slaves_th
        .lock()
        .unwrap()
        .push(thread::spawn(move || println!("Hi")));
}

for th in slaves_th.lock().unwrap() {
    //   ^^^^^^^^^^^^^^^^^^^^^^^^^^ `MutexGuard<'_, Vec<JoinHandle<()>>>` is not an iterator
    match th.join() {
        Ok(_) => {}
        Err(_) => {
            println!("err join");
            ()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

arrays multithreading rust

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