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

后端 未结 3 1258
盖世英雄少女心
盖世英雄少女心 2020-12-03 04:47

I compiled & installed gcc4.4 using macports.

When I try to compile using -> g++ -g -Wall -ansi -pthread -std=c++0x main.cpp...:

 #include 

        
相关标签:
3条回答
  • 2020-12-03 05:12

    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?

    0 讨论(0)
  • 2020-12-03 05:16

    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.

    0 讨论(0)
  • 2020-12-03 05:28

    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);
    ...
    
    0 讨论(0)
提交回复
热议问题