小编Ego*_*sin的帖子

并发std :: call_once调用

可以请有人解释我为什么这个程序中的两个线程(当使用Visual Studio 2012/2013附带的编译器编译时)被阻塞,直到两个std::call_once调用都被执行?另一个Visual Studio错误(假设它在使用GCC编译时表现如预期)?有人可以想出一个解决方法吗?想象一下我所经历的所有痛苦,以解决问题,并请,仁慈.

#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>

namespace
{
    std::once_flag did_nothing;

    void do_nothing()
    { }

    void sleep_shorter_and_do_nothing_once()
    {
        std::this_thread::sleep_for(std::chrono::seconds(3));
        std::cout << "1\n";
        std::call_once(did_nothing, do_nothing);
        std::cout << "2\n";
    }

    std::once_flag sleeped_longer;

    void sleep_longer()
    {
        std::this_thread::sleep_for(std::chrono::seconds(10));
    }

    void sleep_longer_once()
    {
        std::cout << "3\n";
        std::call_once(sleeped_longer, sleep_longer);
        std::cout << "4\n";
    }
}

int main()
{
    std::thread t1(sleep_shorter_and_do_nothing_once);
    std::thread t2(sleep_longer_once);
    t1.join();
    t2.join();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

详细说明,它在使用GCC编译时表现如预期:

  • 打印"3",
  • 等了3秒,
  • 打印"1",
  • 立即打印"2",
  • 再等6-7秒,
  • 并打印"4".

使用Visual Studio 2012/2013附带的编译器进行编译时,其行为如下:

  • 打印"3",
  • 等了3秒,
  • 打印"1", …

c++ c++11

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

C++异常继承歧义

为什么这样做?

#include <exception>
#include <iostream>
#include <stdexcept>

#include <boost/exception/all.hpp>

struct foo_error : virtual boost::exception, public std::runtime_error
{
    explicit foo_error(const char* what)
        : std::runtime_error(what)
    { }

    explicit foo_error(const std::string& what)
        : std::runtime_error(what)
    { }
};

struct bar_error : virtual boost::exception, public std::runtime_error
{
    explicit bar_error(const char* what)
        : std::runtime_error(what)
    { }

    explicit bar_error(const std::string& what)
        : std::runtime_error(what)
    { }
};

struct abc_error : virtual foo_error, virtual bar_error
{
    explicit abc_error(const char* what)
        : foo_error(what), bar_error(what)
    { }

    explicit abc_error(const std::string& what) …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance

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

如何使用vim的netrw文件浏览器对像`sort`这样的文件进行排序?

我希望我的文件以这种方式排序:

abc.c
Makefile
readme.txt
Run Code Online (Sandbox Code Playgroud)

但是netrw文件浏览器会像这样对它们进行排序(使用空排序):

Makefile
abc.c
readme.txt
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

顺便说一句,通过键入它的名字的前几个字母来跳转到文件/目录也是很好的.那可能吗?

vim netrw

5
推荐指数
2
解决办法
1925
查看次数

如何将std :: unique_ptr <T>与返回int的接口一起使用?

我想将open/ closePOSIX API 包装成与RAII兼容的对象std::unique_ptr.但是open函数返回一个int(即不是a HANDLE,它是指针void),我不知道如何使用std::unique_ptr模板类int.有人能帮帮我吗?

c++ shared-ptr c++11

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

这个算法是O(1)吗?

以下算法是简单的O(1),还是其复杂性难以定义?

for (i = 0; i < n; ++i)
    if (i > 10)
        break;
Run Code Online (Sandbox Code Playgroud)

当n <= 10时,我很惊讶它显然是O(n).

complexity-theory

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

标签 统计

c++ ×3

c++11 ×2

complexity-theory ×1

inheritance ×1

netrw ×1

shared-ptr ×1

vim ×1