小编Ben*_*ahi的帖子

为什么在这个无锁堆栈类中"删除"节点会导致竞争条件?

在Anthony Williams的第7.2.1节中标题为"C++ Concurrency in Action"的书中,列出了一个无锁堆栈实现:

template <typename T>
class lock_free_stack {
    struct node {
        shared_ptr<T> data_;
        node* next_;
        node(const T& data) : data_(make_shared(data)) {}
    };
    atomic<node*> head_;
public:
    void push(const T& data)
    {
        node* new_node = new node(data);
        new_node->next_ = head_.load();
        while(!head.compare_exchange_weak(new_node->next_, new_node));
    }
    shared_ptr<T> pop()
    {
        node* old_head = head_.load();
        while (old_head &&
                !head_.compare_exchange_weak(old_head, head_->next_));
        return old_head ? old_head->data_ : shared_ptr<T>();
    }
};
Run Code Online (Sandbox Code Playgroud)

然后在7.2.2节中,作者说"...在pop()中,我们选择泄漏节点以避免竞争条件,其中一个线程删除一个节点,而另一个线程仍然拥有指向它的指针,它只是关于解除引用."

1)我不明白为什么会出现这种情况以及为什么以下pop()函数会导致竞争条件:

shared_ptr<T> pop()
{
    node* old_head = head_.load(); // (1)
    while (old_head &&
            !head_.compare_exchange_weak(old_head, head_->next_)); …
Run Code Online (Sandbox Code Playgroud)

c++ concurrency atomic race-condition c++11

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

如何将 2 个主题分支重新建立到一个新分支上?

这是我的 git 存储库历史记录的当前状态:

--o--o--o--o--o--o--o master
            \
             o--o--o--o--o  topic2
                   |
                 topic1
Run Code Online (Sandbox Code Playgroud)

我想将 topic1 和 topic2 重新设置为 master,并使其类似于:

--o--o--o--o--o--o--o master
                     \
                      o--o--o--o--o  topic2
                            |
                          topic1
Run Code Online (Sandbox Code Playgroud)

实现这一目标的最佳方法是什么?

git git-rebase

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

使用可变参数模板参数调用零参数模板函数指针?

以下是Functional C++博客上的帖子的代码片段,描述了如何实现通用功能评估.

我的问题是如何声明模板函数指针f像R(C ::*f)()没有参数,仍然可以用Args调用它?

// functions, functors, lambdas, etc.
template<
    class F, class... Args,
    class = typename std::enable_if<!std::is_member_function_pointer<F>::value>::type,
    class = typename std::enable_if<!std::is_member_object_pointer<F>::value>::type
    >
auto eval(F&& f, Args&&... args) -> decltype(f(std::forward<Args>(args)...))
{
    return f(std::forward<Args>(args)...);
}

// const member function
template<class R, class C, class... Args>
auto eval(R(C::*f)() const, const C& c, Args&&... args) -> R
{
    return (c.*f)(std::forward<Args>(args)...);
}

template<class R, class C, class... Args>
auto eval(R(C::*f)() const, C& c, Args&&... args) -> R
{
    return (c.*f)(std::forward<Args>(args)...);
}

// …
Run Code Online (Sandbox Code Playgroud)

c++ template-function variadic-templates c++11

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

thread :: join()阻止它不应该

为了理解如何在C++ 11中使用原子,我试着遵循代码片段:

#include <iostream>
#include <thread>
#include <atomic>

using namespace std;

struct solution {
    atomic<bool> alive_;
    thread thread_;

    solution() : thread_([this] {
        alive_ = true;
        while (alive_);
    }) { }
    ~solution() {
        alive_ = false;
        thread_.join();
    }
};

int main() {
    constexpr int N = 1; // or 2
    for (int i = 0; i < N; ++i) {
        solution s;
    }
    cout << "done" << endl;
}
Run Code Online (Sandbox Code Playgroud)

如果N等于1,则输出为done.但是,如果我将其设置为2,主线程将阻塞在thread :: join().为什么你认为我们什么done时候看不到N> 1?

注意:如果我使用以下构造函数:

    solution() : alive_(true), …
Run Code Online (Sandbox Code Playgroud)

multithreading atomic race-condition c++11

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