thread does not name a type

Hi

I am trying to create a thread:

1
2
3
4
5
6
7
#include <thread>

using namespace std;

void f();

thread t1 {f};


However, I get the following error:


error: 'thread' does not name a type


I thought this was standard code. What seems to be the problem?

I'm using GCC with C++11.

Thanks
Steven
Hi,

I don't have much experience with threads, but I couldn't help thinking this might work:

std::thread t1(f);

The function object and it's arguments are arguments to the thread constructor (you can see in the template), brace initialisation doesn't seem appropriate here.

http://en.cppreference.com/w/cpp/thread/thread/thread

Also, you really shouldn't have using namespace std; it's just asking for trouble in lots of ways.

Hope this helps :+)
Thanks for the reply.

However, I think it may be a problem related to the local GCC installation . Perhaps something is

missing.

The following code should work:

1
2
3
#include <thread>

thread t1 (f);


However, the compiler issues an error that it doesn't recognize the thread type.
This works, using cpp.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <thread>

void f();

int main()
{
  std::thread t1(f);
  t1.join();
}

void f() {
    std::cout << "Inside f() function \n";
}


Inside f() function


In your code you didn't have std:: on line 3

Are you sure you have -std=c++11 or -std=c++14 as an argument when compiling?

Edit: If you have errors, it pays to post them all, verbatim.
Last edited on
Yes, -std=c++11 switch is applied.

I tried your program from within C::B.

I get the following compilation error:


'thread' is not a member of 'std'


If I omit std:: in the thread statement (and use a using namespace std statement instead), I get a similar compilation error, saying thread' is not a recognized type.

That's why I suspect a local GCC installation problem. I've been using GCC since some time now, and so far it has been working perfectly (but not for threads).

Thanks.


ok, what version of gcc and C::B do you have, and are you on windows?

I tried searching for std::thread gcc codeblocks but the posts were several years old, not sure whether some windows versions do now have this capability. They probably do, but I am a Linux guy :+)

I also recommend getting clang from llvm, it's better than gcc (for c++ anyway).

g++ 4.8.1

C::B 13.12. In Settings > Compiler, I have manually set the option "Use c++11".

Yes, I'm using Windows 10.

The same error occurs even if I build the program directly using a makefile, bypassing C::B.

I had downloaded both minGW and C::B separately. I think the C::B download had also separately downloaded minGW (the same version).

Thanks.
Last edited on
> I've been using GCC since some time now, and so far it has been working perfectly (but not for threads).

Standard threads do not work in many builds of MinGW. With the GNU library, standard C++ threads are only available on platforms that support for POSIX threads natively. On Windows, a MinGW implementation which has implemented a POSIX threads emulation layer is required.

TDM-GCC comes with a winpthreads implementation: http://tdm-gcc.tdragon.net/
Right, so have a go at searching for your version of minGW and std::thread to see if it actually does have that capability.

Found this:

http://stackoverflow.com/questions/25951617/thread-is-not-a-member-of-std-in-gcc-4-8

See the notes about the threading models.

Do you have the -lpthread -mthreads -pthread options for the compiler?

Not sure what the latest version of gcc minGw will support, gcc 4.8 is a little old now, current version of gcc is 6.1

I probably can't help much more than that - not sure if I have been that much help at all :+) It's also late at my end, could be heading off to stack ZZZZZZzzzzzz....... soon :+)
Topic archived. No new replies allowed.