g++ internal compiling error with chrono and thread headers - c++

I am trying to compile a multithreading program written in C++ running on WSL.
Here's what it looks like:
#include <thread>
void f() {}
int main()
{
std::thread t(f);
t.join();
return 0;
}
When trying to compile this I receive this error:
In file included from /usr/include/c++/9/chrono:40,
from /usr/include/c++/9/thread:38,
from test.cpp:1:
/usr/include/c++/9/limits:1665:7: internal compiler error: Segmentation fault
1665 | min() _GLIBCXX_USE_NOEXCEPT { return __FLT_MIN__; }
| ^~~
0x7f7d7a46b08f ???
/build/glibc-SzIz7B/glibc-2.31/signal/../sysdeps/unix/sysv/linux/x86_64/sigaction.c:0
0x7f7d7a44c082 __libc_start_main
../csu/libc-start.c:308
I discovered the issue in the <chrono> header as this receives the same error:
#include <chrono>
int main()
{
return 0;
}
I have tried compiling with -pthread -std=c++17. Additionally, I have tried updating g++ along with uninstalling it and reinstalling. None of this had any effect and the same error persisted.

Related

Why does 'error: 'thread' is not a member of 'std' occur even after I've link thread in cmake? [duplicate]

I wrote this as a simplified version of a multithreading example to get a feel for it, but am running into some issues when compiling. My compiler says that thread is not a member of std and prompts me to add #include <thread> to my code even though I have already done so. I've been unable to find any similar problems so far, but I suspect that it is an issue with how I'm compiling it because my code is very similar to the example code.
#include <iostream>
#include <thread>
void doWork () {
std::cout << "Working...\n";
}
int main () {
std::thread worker(doWork);
work.join();
std::cout << "Finished\n";
return 0;
}
My compiler is MinGW g++ 9.2.0
I compiled with g++ main.cpp -o main and it gave me these errors:
main.cpp: In function 'int main()':
main.cpp:9:7: error: 'thread' is not a member of 'std'
9 | std::thread worker(doWork);
| ^~~~~~
main.cpp:3:1: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
2 | #include <thread>
+++ |+#include <thread>
3 |
main.cpp:11:2: error: 'work' was not declared in this scope
11 | work.join();
| ^~~~
MinGW-w64 by default comes with native (Win32) instead of POSIX threads support, and unfortunately there is currently no Win32 gthreads implementation (the threading subsystem of libstdc++), hence no threading functionality in GCC.
You need to switch from x86_64-w64-mingw32-g++-win32 to x86_64-w64-mingw32-g++-posix package to get std::thread working in MinGW-w64.
The question mingw-w64 threads: posix vs win32 discusses this issue in more detail.

Simple thread example doesn't work in MinGW

#include <iostream>
#include <thread>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
cout << i;
}
}
int main() {
thread t(thread_c);
t.join();
return 0;
}
Such simple example, but it doesn't work on my g++ (MinGW.org GCC-6.3.0-1) 6.3.0, Windows 8.1
Here's the error:
main.cpp: In function 'int main()':
main.cpp:13:5: error: 'thread' was not declared in this scope
thread t(thread_c);
^~~~~~
main.cpp:14:5: error: 't' was not declared in this scope
t.join();
^
*I've been working on a big project, and noticed about some compilation errors. I wrote this code for testing my compiler out
What's wrong with it?
Loking at your code and the compiler results I see the following:
#include <thread> is not including a file that defines thread as your code expects
GCC 6.3.0 is very old (current is 10.2.0), and chances are it was built without POSIX thread support (and probably only support for Windows threads)
I always recommend using MinGW-w64 instead of MinGW as it is more up to date and supports both 32-bit and 64-bit Windows.
When I build your example with the MinGW-w64 from http://winlibs.com/ (which has POSIX thread support) I get no errors and the example works (output is 012345678910).

Unable to compile a deliberate file on cygwin

The problem occurs when compiling the vim plugin color_coded, and can be summarized as follows:
With g++ -std=c++14, symlink can not be found in unistd.h.
With clang++ -std=c++14, mutex is not supported on cygwin.
So, how to compile this code on cygwin?
#include <unistd.h>
#include <mutex>
#include <thread>
#include <iostream>
std::mutex io_mutex;
auto foo(){
std::lock_guard<std::mutex> guard{io_mutex};
std::cout << &symlink << "\n";
}
int main(){
std::thread t1{foo}, t2{foo};
t1.join();
t2.join();
}
The following are the errors.
with gcc 5.4.0 and g++ -std=c++14:
t.cpp: In function 'auto foo()':
t.cpp:9:18: error: 'symlink' was not declared in this scope
std::cout << &symlink << "\n";
With clang 3.7.1 and clang++ -std=c++14:
In file included from t.cpp:2:
/usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/mutex:699:10: error: thread-local
storage is not supported for the current target
extern __thread void* __once_callable;
^
/usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/mutex:700:10: error: thread-local
storage is not supported for the current target
extern __thread void (*__once_call)();
^
2 errors generated.

Weird thread issue

Alright im compiling the following simple chunk of code (found on cplusplus.com) on CodeBlocks IDE 12.11 with MinGW (downloaded separately and latest version too as of today).
The thing is that it shows the following errors upon compilation:
12: error: 'thread' was not declared in this scope
12: error: expected ';' before 't1'
13: error: 't1' was not declared in this scope
#include <iostream>
#include <thread>
using namespace std;
void hello(void){
cout << "hey there!" << endl;
}
int main()
{
thread t1(hello);
t1.join();
return 0;
}
Are threads not supported by GCC completely?Do i need to add flags to my compiler, and how do i do it on a codeblocks project? thanks in advance
Add --std=c++11 -pthread to your Compiler Flags

std::thread error (thread not member of std)

I compiled & installed gcc4.4 using macports.
When I try to compile using -> g++ -g -Wall -ansi -pthread -std=c++0x main.cpp...:
#include <thread>
...
std::thread t(handle);
t.join();
....
The compiler returns:
cserver.cpp: In member function 'int CServer::run()':
cserver.cpp:48: error: 'thread' is not a member of 'std'
cserver.cpp:48: error: expected ';' before 't'
cserver.cpp:49: error: 't' was not declared in this scope
But std::cout <<... compiles fine..
Can anyone help me?
gcc does not fully support std::thread yet:
http://gcc.gnu.org/projects/cxx0x.html
http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html
Use boost::thread in the meantime.
Edit
Although the following compiled and ran fine for me with gcc 4.4.3:
#include <thread>
#include <iostream>
struct F
{
void operator() () const
{
std::cout<<"Printing from another thread"<<std::endl;
}
};
int main()
{
F f;
std::thread t(f);
t.join();
return 0;
}
Compiled with
g++ -Wall -g -std=c++0x -pthread main.cpp
Output of a.out:
Printing from another thread
Can you provide the full code? Maybe there's some obscure issue lurking in those ...s?
I had the same issue on windows using MinGW. I found wrapper classes for in on github mingw-std-threads Including
mingw.mutex.h, mingw.thread.h files to global MinGW directory fixed this issue. All I had to do is to include header file and my code stayed the same
#include "mingw.thread.h"
...
std::thread t(handle);
...
Drop -ansi, it means -std=c++98, which you obviously don't want. It also causes macro __STRICT_ANSI__ to be defined and this may change the behavior of the headers, e.g. by disabling C++0x support.