Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7787731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:45:48+00:00 2026-06-01T20:45:48+00:00

Code: #include <iostream> #include <thread> void hello() { std::cout << Hello World << std::endl;

  • 0

Code:

#include <iostream>
#include <thread>

void hello()
{
    std::cout << "Hello World" << std::endl;
}

int main()
{
    try
    {
        std::cout << "creating thread" << std::endl;
        std::thread t(hello);
        std::cout << "waiting" << std::endl;
        t.join();
        std::cout << "done" << std::endl;
    }
    catch(std::exception& ex)
    {
        std::cout << ex.what() << std::endl;
    }
}

Build:

g++ -Wall -fexceptions -std=c++0x -pthread  -g     -c /home/alex/tmp/thread_test/main.cpp -o obj/Debug/main.o
g++  -o bin/Debug/thread_test obj/Debug/main.o    
Output size is 106.62 KB
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings

Result:

creating thread
Operation not permitted

How this can be fixed?

Edit:
Running the program with strace:

alex@alex-64:~/tmp/thread_test/bin/Debug$ strace ./thread_test
execve("./thread_test", ["./thread_test"], [/* 38 vars */]) = 0
brk(0)                                  = 0x189a000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7c60cbc000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=121299, ...}) = 0
mmap(NULL, 121299, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f7c60c9e000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/libstdc++.so.6", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\244\5\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=991424, ...}) = 0
mmap(NULL, 3171440, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7c60797000
mprotect(0x7f7c6087f000, 2097152, PROT_NONE) = 0
mmap(0x7f7c60a7f000, 40960, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xe8000) = 0x7f7c60a7f000
mmap(0x7f7c60a89000, 83056, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f7c60a89000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libgcc_s.so.1", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260(\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=88384, ...}) = 0
mmap(NULL, 2184216, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7c60581000
mprotect(0x7f7c60596000, 2093056, PROT_NONE) = 0
mmap(0x7f7c60795000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x14000) = 0x7f7c60795000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0 \24\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1685816, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7c60c9d000
mmap(NULL, 3801960, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7c601e0000
mprotect(0x7f7c60377000, 2093056, PROT_NONE) = 0
mmap(0x7f7c60576000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x196000) = 0x7f7c60576000
mmap(0x7f7c6057b000, 21352, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f7c6057b000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libm.so.6", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\360>\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=538928, ...}) = 0
mmap(NULL, 2633960, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7c5ff5c000
mprotect(0x7f7c5ffdf000, 2093056, PROT_NONE) = 0
mmap(0x7f7c601de000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x82000) = 0x7f7c601de000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7c60c9c000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7c60c9a000
arch_prctl(ARCH_SET_FS, 0x7f7c60c9a740) = 0
mprotect(0x7f7c601de000, 4096, PROT_READ) = 0
mprotect(0x7f7c60576000, 16384, PROT_READ) = 0
mprotect(0x7f7c60795000, 4096, PROT_READ) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7c60c99000
mprotect(0x7f7c60a7f000, 32768, PROT_READ) = 0
mprotect(0x603000, 4096, PROT_READ)     = 0
mprotect(0x7f7c60cbe000, 4096, PROT_READ) = 0
munmap(0x7f7c60c9e000, 121299)          = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7c60cbb000
write(1, "creating thread\n", 16creating thread
)       = 16
brk(0)                                  = 0x189a000
brk(0x18bb000)                          = 0x18bb000
write(1, "Operation not permitted\n", 24Operation not permitted
) = 24
exit_group(0)                           = ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:45:49+00:00Added an answer on June 1, 2026 at 8:45 pm

    Your problem is that you forgot to specify -lpthread or -pthread flag to the compiler. So it assumes a single-threaded mode when building your program.

    The exception is thrown by the standard C++ library:

    (gdb) catch throw
    Function "__cxa_throw" not defined.
    Catchpoint 1 (throw)
    (gdb) run
    Starting program: /tmp/test 
    creating thread
    Catchpoint 1 (exception thrown), 0x00007ffff7b8eff0 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
    (gdb) where
    #0  0x00007ffff7b8eff0 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
    #1  0x00007ffff7b3ba3e in std::__throw_system_error(int) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
    #2  0x00007ffff7b45cb7 in std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
    #3  0x00000000004012d4 in std::thread::thread<void (&)()>(void (&&&)()) ()
    #4  0x0000000000400f0e in main ()
    (gdb)  quit
    

    I don’t have any desire to check source code for that, but it is most likely they use “lazy” dynamic linking to determine if POSIX threads family of functions are available. And throw exceptions otherwise. That way, you get that exception unless linking with pthread library.

    That has nothing to do with virtual memory or other resource limits as I initially thought might be happening because system calls do not report any errors. So just do:

    g++ -std=c++0x -o test ./test.cpp -pthread
    

    … and it will work.

    UPDATE:

    As @ildjaRN pointed out, you already specify -pthread. I suggest you specify it after your object files (source files for a single invocation compile & link), otherwise it might not get picked up.

    Here is how to make sure it is picked up – you can run ldd and make sure that pthread.so makes it in:

    $ g++ -std=c++0x -lpthread -o test ./test.cpp
    $ ldd ./test | grep pthread
    $ g++ -std=c++0x -o test ./test.cpp -lpthread
    $ ldd ./test | grep pthread
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff2a9073000)
    $ 
    

    Hope it helps. Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When compiling the following code: #include <iostream> #include <thread> using namespace std; void hello()
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
The following code #include <iostream> using namespace std; int main() { const char* const
Consider the following code: #include <iostream> using namespace std; int main() { int x,
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
MinGw 4.7.0 Boost 1.47 code: #include <iostream> #include <boost/thread.hpp> int main(int argc, char *argv[])
Code: #include <iostream> #include stdafx.h #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> using namespace std; boost::mutex mut;
When trying to compile the following code #include <thread> #include <iostream> void foo() {
Here is my code : #include<iostream> class foo; template<typename T> void bar(T a) {
In the following code: #include <iostream> template <typename T, size_t N> void cal_size(T (&a)[N])

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.