小编Cur*_*ous的帖子

为什么只能安全地从信号处理程序中调用异步信号安全功能?

我仍然有点困惑,为什么接收信号并从该信号处理程序中调用非异步安全函数是不安全的.有人可以解释这背后的原因,并可能尝试给我一些参考资料,我可以跟着自己阅读更多内容吗?

换句话说,我问为什么在信号处理程序中调用printf是不安全的.是因为内部进程问题和可能的竞争条件导致两个可能的printf没有保护调用,或者是因为进程争用到同一个资源(在本例中为stdout).假设进程A中的线程正在调用printf而另一个线程接收该信号然后调用printf.可能是因为这里的内核不知道该怎么做,因为它无法区分这两个调用.

c++ linux multithreading signals c++11

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

在React.js中调用componentWillMount的顺序

根据此页面http://busypeoples.github.io/post/react-component-lifecycle/组件的render 方法在其他位置componentWillMountcomponentDidMount方法之间调用.

但是这里的组件生命周期的react.js文档https://facebook.github.io/react/docs/component-specs.html表示componentDidMount在父项之前调用所有子活动的方法.我可以理解,componentDidMount在呈现任何子组件之后调用是可以的,但是运行时如何知道哪些子项componentWillMount在呈现之前调用该函数?或者我是否正确地假设componentWillMount首先调用父活动然后为子项调用(不像componentDidMount)?

谢谢!

html javascript facebook single-page-application reactjs

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

Git cherry-pick导致合并冲突,而合并则不会

我正在尝试学习如何使用git cherry pick,我阅读了git通过执行返回的手册页,git cherry-pick --help但似乎没有帮助.我将尝试解释下面的问题.我有两个分支masterother.

在分支上master 提交历史记录是

0x2 Second commit from master branch
0x1 Initial commit
Run Code Online (Sandbox Code Playgroud)

我正在跟踪的存储库中唯一的文件readme具有以下内容

Some text
Run Code Online (Sandbox Code Playgroud)

在分支上other 提交历史记录是

0x5 CHECKPOINT, going to cherry-pick onto master
0x4 second commit from branch other
0x3 first commit from other branch:
0x2 Second commit from master branch
0x1 Initial commit
Run Code Online (Sandbox Code Playgroud)

并且readme文件的内容是

Some text.

Adding some text from other branch.  Adding some more text.

Going to cherry-pick this …
Run Code Online (Sandbox Code Playgroud)

git version-control merge cherry-pick

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

为什么std :: thread通过转发引用接受仿函数

为什么一个std::thread对象通过转发引用接受函数参数,然后用decay_copy?复制该对象?按值接受函数对象会不会更容易?

一般来说为什么不模拟函数以便按值获取函数对象?可以引用 - 不能用reference_wrappers 模仿(这将更明确,并且还方便地有一个成员operator()来调用存储的函数)?

c++ multithreading c++11 c++14 forwarding-reference

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

删除std :: future和std :: promise的void特化

关于描述删除std :: future和std :: promise P0241R0的void 特化的论文.这可能是一个愚蠢的问题,但建议的解决方案是完全删除void专门化,但是当他们想要实例化一个std::future<void>对象时,人们期望做什么?

c++ future c++11 c++14 c++17

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

抛出表达式中是否允许使用prvalue elision

§[except.throw],标准中说抛出异常拷贝 - 从throw表达式初始化异常对象

抛出异常copy-initializes(11.6,15.8)一个临时对象,称为异常对象

为什么以下代码在C++ 17上编译?

class Exception {
public:
   Exception() = default;
   Exception(Exception&&) = delete;
   Exception(const Exception&) = delete;
};

int main() {
    throw Exception{};
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

(https://wandbox.org/permlink/R3WfzfnBAORTLVSy)

复制初始化不包括符合prvalue elision条件的任何情况(从我的看来).为什么上面的代码在C++ 17中编译?

c++ exception-handling exception c++17 prvalue

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

lambda 中值捕获的 std::move() 上的 decltype() 导致类型不正确

似乎 Clang (9.0.0) 或我对如何decltype()指定在标准中工作的理解有问题。参考以下代码,

#include <utility>
#include <string>

template <typename...> class WhichType;

template <typename T>
std::remove_reference_t<T>&& move_v2(T&& t) {
    WhichType<std::remove_reference_t<T>&&>{};
    return static_cast<std::remove_reference_t<T>&&>(t);
}

int main() {
    auto x = std::string{"a"};
    [v = x]() { 
        // move_v2(v);
        // WhichType<decltype(move_v2(v))>{};
        WhichType<decltype(std::move(v))>{};
    }();
}
Run Code Online (Sandbox Code Playgroud)

上面的代码有编译器输出implicit instantiation of undefined template 'WhichType<std::__1::basic_string<char> &&>',而不是预期const std::__1::basic_string<char> &&在模板参数WhichType。使用move_v2WhichTypemove_v2本身似乎输出正确的事情,但。

但是,Clang 似乎也std::move(v)按照我的预期对表达式进行了重载解析https://wandbox.org/permlink/Nv7yXnCbqxjJMVvX。这让我的一些担忧消失了,但我仍然不了解decltype()lambda 内部的行为。

在这种特殊情况下,GCC 似乎没有这种不一致https://wandbox.org/permlink/5mhrOzLn5XZO8LNB


如果我对我的理解有误decltype()或指出此错误在 …

c++ clang decltype language-lawyer c++17

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

boost::future .then() 继续返回 boost::future

我正在阅读关于改进std::futurestd::promise这里的新 C++ 提案http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3857.pdf它说

一种常见的情况是 then 函数对象的主体本身将是一个基于 future 的操作,这导致 then() 返回一个 future>。在这种情况下,几乎总是你真正关心的是内在的未来,所以 then() 在返回之前执行一个隐式的 unwrap() (见下文)。

因此在下面的代码中

auto promise = std::promise<int>{};
auto another_future = promise.get_future().then([](auto future) {
    return std::async([]() { return 1; });
});
Run Code Online (Sandbox Code Playgroud)

another_futurestd::future<int>和不是的类型std::future<std::future<int>>

我试图用它boost::future来实现同样的事情,但似乎 boost 延续并没有隐含地解开未来。我可以做些什么来使升压期货具有相同的行为?似乎也没有unwrap()可用于生成的功能的功能。

我是否不得不通过构造函数手动打开未来?同样在尝试时出现以下错误,我该怎么办?

inline explicit BOOST_THREAD_FUTURE(
    BOOST_THREAD_RV_REF(
        BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R> >) other); // EXTENSION
Run Code Online (Sandbox Code Playgroud)

c++ boost asynchronous future c++11

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

std :: optional的转发引用构造函数的约束

std::optional截至此日期有8个构造函数,如下所示(另请参见http://en.cppreference.com/w/cpp/utility/optional/optional)

/* (1) */ constexpr optional() noexcept;
/* (1) */ constexpr optional( std::nullopt_t ) noexcept;

/* (2) */ constexpr optional( const optional& other );

/* (3) */ constexpr optional( optional&& other ) noexcept(/* see below */);

template < class U >
/* (4) */ /* EXPLICIT */ optional( const optional<U>& other );

template < class U >
/* (5) */ /* EXPLICIT */ optional( optional<U>&& other );

template< class... Args > 
/* (6) */ constexpr explicit optional( …
Run Code Online (Sandbox Code Playgroud)

c++ overloading optional c++17

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

std :: memory_order_relaxed相对于同一原子变量的原子性

关于内存命令的cppreference文档说

放宽内存排序的典型用法是递增计数器,例如std :: shared_ptr的引用计数器,因为这只需要原子性,但不需要排序或同步(请注意,递减shared_ptr计数器需要与析构函数进行获取 - 释放同步)

这是否意味着宽松的内存排序实际上不会导致相同变量的原子性?但更确切地说,结果是与其他放松的内存负载和/或compare_exchanges相关的最终一致性?使用std::memory_order_seq_cst是与配对时看到一致结果的唯一方法std::memory_order_relaxed吗?

我假设std::memory_order_relaxed对于相同的变量仍然是原子的,但是没有提供关于其他数据的加载和存储的任何其他约束.

c++ multithreading atomicity memory-barriers c++11

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