相关疑难解决方法(0)

Meyers对Singleton模式线程的实现是否安全?

以下实现,使用延迟初始化Singleton(Meyers'Seingleton)线程安全吗?

static Singleton& instance()
{
     static Singleton s;
     return s;
}
Run Code Online (Sandbox Code Playgroud)

如果没有,为什么以及如何使其线程安全?

c++ multithreading design-patterns

129
推荐指数
5
解决办法
5万
查看次数

在linux下的GCC中使用std :: thread的正确链接选项是什么?

嗨,我正在尝试使用std::threadG ++.这是我的测试代码

#include <thread>
#include <iostream>

int main(int, char **){
    std::thread tt([](){ std::cout<<"Thread!"<<std::endl; });
    tt.join();
}
Run Code Online (Sandbox Code Playgroud)

它编译,但当我尝试运行它时,结果是:

terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted 
Aborted
Run Code Online (Sandbox Code Playgroud)

我的编译器版本:

$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

我的测试代码出了什么问题?

更新:我使用以下命令行来编译和运行我的代码.

$ g++ -std=c++0x test.cpp
$ ./a.out
Run Code Online (Sandbox Code Playgroud)

我试过 …

c++ multithreading g++ c++11

87
推荐指数
3
解决办法
5万
查看次数

std :: call_once抛出std :: system_error(未知错误-1)

我使用的是C ++ OpenCL包装程序,我想知道为什么我的程序崩溃了。我发现对的任何调用std::call_once均引发错误。

#include <mutex>

int main() {
    static std::once_flag f;
    std::call_once(f, []{});
}
Run Code Online (Sandbox Code Playgroud)

程序输出:

terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
Run Code Online (Sandbox Code Playgroud)

这是的输出g++ -v

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/8.1.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp
Thread model: posix
gcc version 8.1.1 20180531 (GCC)
Run Code Online (Sandbox Code Playgroud)

c++ linux gcc

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

标签 统计

c++ ×3

multithreading ×2

c++11 ×1

design-patterns ×1

g++ ×1

gcc ×1

linux ×1