小编Joe*_*Miu的帖子

c ++中的细粒度锁定队列

这是Anthony Williams在6.2.3 C++ Concurrency in Action中引入的细粒度锁定队列.

/*
    pop only need lock head_mutex and a small section of tail_mutex,push only need
    tail_mutex mutex.maximum container concurrency.
*/
template<typename T> class threadsafe_queue
{
    private:
    struct node
    {
        std::shared_ptr<T> data;
        std::unique_ptr<node> next;
    }
    std::mutex head_mutex;   //when change the head lock it.
    std::unique_ptr<node> head;  
    std::mutex tail_mutex;   //when change the tail lock it.
    node* tail;
    std::condition_variable data_cond;

    node* get_tail()
    {
        std::lock_guard<std::mutex> tail_lock(tail_mutex);
        return tail;
    }

    public:
    /* 
        create a dummy node
    */
    threadsafe_queue():
        head(new node),tail(head.get())
    {} …
Run Code Online (Sandbox Code Playgroud)

c++ concurrency multithreading stl

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

为什么c局部变量需要转换为void?

最近我在github上学习一个项目,我发现了这个.为什么到底有一个"(void)n"?

void CurrentThread::cacheTid()
{
  if (t_cachedTid == 0)
  {
    t_cachedTid = detail::gettid();
    int n = snprintf(t_tidString, sizeof t_tidString, "%5d ", t_cachedTid);
    assert(n == 6); (void) n;
  }
}
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×2

concurrency ×1

multithreading ×1

stl ×1