2

the code at the bottom of this post compiles fine but generates an useless binary with

$ clang++ -v
clang version 3.3 (trunk 168461)
Target: x86_64-unknown-linux-gnu
Thread model: posix

when this command is given

clang++ -std=c++11 -pthread -s -O3 -DNDEBUG source.cpp -o source

the binary always generates this

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

What I don't get is:

  • why i need to link the POSIX threads library if C++11 includes a threading model in the standard, why the flag -std=c++11 it's not enough ?
  • if clang++ supports -pthread or not, according to what I have read it should support pthreads

Thanks.


#include <iostream>
#include <thread>

void f()
{
  std::cout << "Hello World\n";
}

int main()
{
  std::thread t(f);
  t.join();
}
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
user1802174
  • 273
  • 3
  • 10
  • 1
    you should use libc++ as standard library when using clang. just add `-stdlib=libc++` to your compiler args. – Stephan Dollberg Nov 23 '12 at 11:32
  • @bamboon Yes, I have already asked him to try that. – Ali Nov 23 '12 at 11:41
  • 2
    If you are using libstdc++ (not libc++), have you tried adding `-D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 -D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 -D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 -D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8` to the command line? Check if it improves the situation. Background information [here](http://llvm.org/bugs/show_bug.cgi?id=12730). – Daniel Frey Feb 27 '13 at 20:49

3 Answers3

1

you should use the command like this:

clang++ -std=c++11 -pthread -stdlib=libstdc++ threadEx.cpp

You forgot to add the library. libc++ does not work for me with ubuntu 12.04, clang3.3 but I made it work with clang3.3 and g++ 4.7.2 (g++ 4.6.3 did not work either). Both works.

drunk teapot
  • 303
  • 2
  • 13
0

In thread.cc there is the following bit of code in thread::_M_start_thread():

if (!__gthread_active_p())
  __throw_system_error(int(errc::operation_not_permitted));

This is why it's blowing up for you --- libgcc checks for the existence of pthread_cancel() and only returns 1 if it exists. You didn't specify -pthread, so there's no pthread_cancel().

Why do you need to specify -pthread when building? I'm guessing it's because the alternative is to assume -pthread all the time and that causes unnecessary overhead.

tmyklebu
  • 13,915
  • 3
  • 28
  • 57
  • overhead ? why ? I'm declaring what I'm importing and that #include it's part of the standard naming convention, the parser it's not smart enough to sum up all the libraries needed by my program ? Also this answer doesn't convince me because every compiler it's supposed to have its own implementation of the standard library and you are talking about gcc, not clang/llvm. My best guess is that the llvm c++ it's not ready yet and it's not compatible with the gnu one. – user1802174 Nov 23 '12 at 09:05
  • The parser does not decide which libraries to include in the final binary, no. That would...probably end in tears both from implementors and users. I'm talking about libgcc, which is a library that has the thread support underlying libstdc++. clang will link against both libgcc and libstdc++ by default (ldd your program) but it will only link against libpthread if you specify -pthread. – tmyklebu Nov 23 '12 at 09:21
0

Using the -pthread flag makes a huge difference when compiling the code, see gcc - significance of -pthread flag when compiling.

According to the accepted answer to the question What is g++'s -pthread equiv in clang?, clang supports -pthread.


Actually, this line you posted tells that you are using pthreads:

Thread model: posix

Pthreads won't go away, and I highly doubt that Clang / LLVM will implement a new threading library from scratch. Why would they? The platform's native library is mature enough.


Sorry, I cannot help you any further with this: I don't have clang installed on my machine, and your code runs just fine on my machine with gcc 4.6.

Community
  • 1
  • 1
Ali
  • 56,466
  • 29
  • 168
  • 265
  • you have tried to compile this with clang++ 3.3 and run it on your machine ? ( if you have linux ) – user1802174 Nov 23 '12 at 09:13
  • No, I don't have clang installed on my machine, sorry. – Ali Nov 23 '12 at 09:14
  • also the answer on that page doesn't apply here because i have a build from the svn that is just 1-2 days old, this can't be about an old release ... – user1802174 Nov 23 '12 at 09:15
  • it's about C++11, the old days you were pratically forced to use pthreads, now the threads are supposed to be part of the standard library. that's it. Also llvm it's supposed to offer its own new implementation of the standard C++ library that is one of the very few that is C++11 ready. – user1802174 Nov 23 '12 at 09:19
  • OK, I have updated my answer. You are using pthreads. I consider both of your questions answered. – Ali Nov 23 '12 at 10:16
  • this is not an answer, it's just OT, also in your edit you are testing under gcc ... I'm talking about Clang/LLVM. I have tested this under g++ too and they don't seem to provide a real threading lib for the C++11 standard, just a shortcut to the pthreads and this is why this works under g++. moreover when building clang from source i have enabled the threading support with the appropriate flag. – user1802174 Nov 23 '12 at 10:22
  • Whether you like it or not, that is the answer to your questions. I think you simply have a configuration problem or just a flag is missing. Have you tried `clang++ -std=c++0x -stdlib=libc++ file_name.cpp`? That is, linking with libc++. I cannot try it on my machine. – Ali Nov 23 '12 at 10:41
  • it's not about liking this or not, you can't expect to believe that an external library like the pthread can solve a problem about the standard library or be a part of it. By the way I on another path now and probably i have found an answer to this. – user1802174 Nov 23 '12 at 11:25