nishu2901
Posts: 4
Joined: Wed Apr 10, 2019 1:43 pm

std::thread not working with clang in rpi3

Wed Apr 10, 2019 1:48 pm

I have below code :

Code: Select all

#include <iostream>
#include <thread>

int main()
{
    std::thread th([]() {
        std::cout << "Hello, World!" << std::endl;
    });
    
    th.join();
    
    return 0;
}
and i am compiling with : clang++ -o demo demo.cpp -lpthread -std=c++11

and when i execute it i get below unexpected output :

Code: Select all

[email protected] ~ $ ./demo
pure virtual method called
terminate called without an active exception
Aborted

my clang version is :

Code: Select all

Raspbian clang version 3.5.0-10+rpi1 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: arm-unknown-linux-gnueabihf
Thread model: posix
Can anyone help me with how to resolve this ?

User avatar
topguy
Posts: 5566
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: std::thread not working with clang in rpi3

Wed Apr 10, 2019 4:27 pm

Since its CLang 3.5 it matches my Raspbian Jessie installation. Raspbian stretch should have CLang 3.8 and 4.0 so maybe an upgrade to the newest Raspbian version helps.. ( I dont have a way to test that, but someone else might have. )

swampdog
Posts: 196
Joined: Fri Dec 04, 2015 11:22 am

Re: std::thread not working with clang in rpi3

Wed Apr 10, 2019 11:23 pm

I don't know much about clang. However, I needed to build LLVM and built clang (6.01) as most of the work was done. Mine isn't "installed" properly and thus requires LD_LIBRARY_CONFIG which *might* help with your problem. I also have multiple "gcc" versions and for this I used gcc (8.2.0) and they again are not "installed" properly. "c.cc" is your posted code.

Code: Select all

[email protected]:/wrk/T $ /usr/local/GCC/8.2.0/bin/g++ -o c c.cc -lpthread
[email protected]:/wrk/T $ LD_LIBRARY_PATH=/usr/local/GCC/8.2.0/lib ./c
Hello, World!
Okay, now try clang..

Code: Select all

[email protected]:/wrk/T $ LD_LIBRARY_PATH=/usr/local/GCC/8.2.0/lib /usr/local/QT/clang/bin/clang++ -o cl c.cc -lpthread
[email protected]:/wrk/T $ LD_LIBRARY_PATH=/usr/local/GCC/8.2.0/lib ./cl
Hello, World!
Initially I did this..

Code: Select all

[email protected]:/wrk/T $ ./cl
pure virtual method called
terminate called without an active exception
Aborted (core dumped)
..'cos it had linked against "/usr/lib/arm-linux-gnueabihf/libstdc++.so.6" rather than "/usr/local/GCC/8.2.0/lib/libstdc++.so.6".

Maybe you have a similar issue?

There are bugs posted though..
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65255
viewtopic.php?t=131856

User avatar
Paeryn
Posts: 2561
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: std::thread not working with clang in rpi3

Thu Apr 11, 2019 12:31 am

Clang 6.0.1 is in the Raspbian Stretch repo (did this get added at some point? I had 4.0 installed, I'm sure I wouldn't have installed that if 6.0 was there).

Code: Select all

[email protected]:~/Programming/asm/thr $ clang++-6.0 --version
clang version 6.0.1-10+rpi1~bpo9~rpt1 (tags/RELEASE_601/final)
Anyway, compiling with this version works out-of-the-box (it did with 4.0 too though 4.0 has to be told to use the c++11 standard, 6.0 defaults to c++14)

Code: Select all

[email protected]:~/Programming/asm/thr $ clang++-6.0 -o hello hello.cc -pthread
[email protected]:~/Programming/asm/thr $ ./hello
Hello, World!
She who travels light — forgot something.

Return to “C/C++”