2

I am following this tutorial. I can compile the example by command line:

g++ -std=gnu++0x AtomicCounter.cpp -o AtomicCounter -lpthread

It works fine but when I run

./AtomicCounter

It gives the error:

terminate called after throwing an instance of 'std::system_error' what(): Enable multithreading to use std::thread: Operation not permitted Aborted (core dumped)

I posted the code of AtomicCounter.cpp

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

struct AtomicCounter {
    std::atomic<int> value;

    AtomicCounter() : value(0) {}

    void increment(){
        ++value;
    }

    void decrement(){
        --value;
    }

    int get(){
        return value.load();
    }
};

int main(){
    AtomicCounter counter;

    std::vector<std::thread> threads;
    for(int i = 0; i < 10; ++i){
        threads.push_back(std::thread([&counter](){
            for(int i = 0; i < 500; ++i){
                counter.increment();
            }
        }));
    }

    for(auto& thread : threads){
        thread.join();
    }

    std::cout << counter.get() << std::endl;

    return 0;
}

EDIT:

Thank @Kerrek SB, it works when I changed to pthread instead of lpthread.

But I don't understand why. Can someone explain for me?

For more informations, I ran strace ./AtomicCounter

    access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/i386-linux-gnu/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\320]\0\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=134614, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7685000
mmap2(NULL, 111276, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7669000
mmap2(0xb7681000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x18000) = 0xb7681000
mmap2(0xb7683000, 4780, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb7683000
close(3)     

                       = 0
GAVD
  • 1,977
  • 3
  • 22
  • 40
  • What if you try compiling with `-std=gnu++11`, since you're obviously using C++11 features. – Peter Cordes Nov 16 '16 at 03:10
  • 1
    Try `-pthread` instead of `-lpthread`. – Kerrek SB Nov 16 '16 at 03:11
  • Works for me with `g++ -std=gnu++11 -O2 AtomicCounter.cpp -lpthread && ./a.out`, on x86-64 Ubuntu 15.10 (gcc 5.2). With/without `-O2`, and with `-lpthread` or `-pthread`. – Peter Cordes Nov 16 '16 at 03:12
  • I tried two other options `-std=gnu++11` and `-std=c++11`, but it doesn't work – GAVD Nov 16 '16 at 03:12
  • `Operation not permitted` is weird: try running `strace ./a.out` to see what system call is failing. Do you have a ulimit setting that doesn't allow starting 10 threads?? On my system, I see `mmap()` / `mprotect()` / `clone()` 10 times. If you traced into child processes/threads with `-f`, you'd see what was happening inside them. – Peter Cordes Nov 16 '16 at 03:15
  • I found [this bug](https://bugs.launchpad.net/ubuntu/+source/gcc-defaults/+bug/1228201) – Danh Nov 16 '16 at 03:21
  • What version of what OS are you using? What g++ version are you using? Apparently you're using a 32-bit system? That's weird, it's 2016... Anyway, your `strace` output doesn't show any system calls that encountered any errors (excluding `access("...hwcap")`, because it's normal for that not to exist), or even the `write()` that prints the error message, or the `exit_group` that ends your process. i.e. it's useless because you left out too much. – Peter Cordes Nov 16 '16 at 03:32
  • For your new question: http://stackoverflow.com/questions/23250863/difference-between-pthread-and-lpthread-while-compiling – Danh Nov 16 '16 at 03:32
  • I use Ubuntu 14 32 bit, gcc 4.8.4. – GAVD Nov 16 '16 at 03:41
  • @Danh Thanks for your comment. – GAVD Nov 16 '16 at 03:42
  • 1
    @PeterCordes This program crash because of [this fix in gcc](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52681) – Danh Nov 16 '16 at 03:44
  • @Danh: Yup, looks like. Nice find. – Peter Cordes Nov 16 '16 at 03:50

0 Answers0