小编Eri*_*pää的帖子

使用带有原子<bool>的std :: condition_variable

SO处理原子有几个问题,而其他问题涉及std :: condition_variable.但我的问题是,如果我在下面使用是正确的?

三个线程,一个ctrl线程,在取消其他两个线程之前进行准备工作.ctrl线程还可以在工作线程(发送器/接收器)处于紧密的发送/接收循环时暂停它们.使用原子的想法是在没有设置暂停的布尔值的情况下使紧密循环更快.

class SomeClass
{

public:
    //...                                                                                                                                                                                                                                                                                                                                                                                   
    // Disregard that data is public...                                                                                                                                                                                                                                                                                                                                                     

    std::condition_variable cv; // UDP threads will wait on this cv until allowed                                                                                                                                                                                                                                                                                                           
                                // to run by ctrl thread.                                                                                                                                                                                                                                                                                                                                   
    std::mutex cv_m;
    std::atomic<bool> pause_test_threads;
};

void do_pause_test_threads(SomeClass *someclass)
{
    if (!someclass->pause_test_threads)
    {
        // Even though we use an atomic, mutex must be held during                                                                                                                                                                                                                                                                                                                          
        // modification. See documentation of condition variable                                                                                                                                                                                                                                                                                                                            
        // notify_all/wait. Mutex does not need to be held for the actual                                                                                                                                                                                                                                                                                                                   
        // notify call.                                                                                                                                                                                                                                                                                                                                                                     
        std::lock_guard<std::mutex> …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11

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

UDP在不同的线程中发送和接收

Linux内核中同一个套接字上UDP发送和接收的处理有多独立?我的用例是一个工作线程在(最多)1000个套接字上发送UDP测试流量,并在另一个工作线程中接收UDP回复.接收器将是一个epoll循环,它还接收硬件发送和接收套接字错误队列上的时间戳.

为了澄清一下,在执行sendmsg()系统调用时,这会在接收器线程上临时阻塞(或生成EAGAIN/EWOULDBLOCK)吗?(即,如果发送和接收在时间上发生重叠)所有套接字都设置为非阻塞模式.

另一个问题是内核中的锁定粒度 - 如果我使用sendmmsg/recvmmsg发送和接收,是每个sendmmsg锁定一次该套接字的锁,还是sendmmsg中每个UDP数据报锁定一次?

更新:我看了一下Linux内核中sendmmsg的原始补丁,似乎主要的好处是避免了多个转换用户内核空间.如果完成任何锁定,可能是在__sys_sendmsg的各个调用中完成的:https://lwn.net/Articles/441169/

c++ sockets linux multithreading linux-kernel

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

Linux 上 pthread 互斥体的默认优先级继承策略?

我已经环顾四周,但没有找到答案:When create pthread mutexes in a Linux userspace program,which policy do those mutexs have by default? 请参阅https://linux.die.net/man/3/pthread_mutexattr_setprotocol

看来最好的默认策略是优先级上限,即 PTHREAD_PRIO_PROTECT,至少对于我的应用程序来说是这样;)

另外,我也可以在 C++ std::mutexes 上设置优先级继承策略吗?我怀疑 C++ std::mutex 无论如何都会是 Linux 中的 futex,就像 pthread 互斥锁一样?

c++ linux pthreads linux-kernel

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

标签 统计

c++ ×3

linux ×2

linux-kernel ×2

multithreading ×2

c++11 ×1

pthreads ×1

sockets ×1