1

I wanted to practice with standard C++ threads instead of UNIX ones, but soon encountered a problem, whenever I write std::thread CLion underlines it with red and says Can't resolve namespace member 'thread'. I checked my CMake file it's set for C++11. I reinstalled the latest version of MinGW (6.3.0) and ticked a box with G++ compiler. I have been told by my friend that he uses Cygwin and everything works. But is it still possible to make it work with MinGW?

#include <iostream>
#include <thread>

#define BUFFER_SIZE 3
#define PROD_NUM 3
#define CONS_NUM 2



void produce(){
    //production
}

void consume(){
    //consumption
}

int main() {
    std::cout << "Hello, World!" << std::endl;
    int i,j;
        std::thread producer(produce);
        std::thread consumer (consume);
    return 0;
}

The code itself has literally nothing

EDIT in thread library there is

#pragma GCC system_header

#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else

#include <chrono>
#include <functional>
#include <memory>
#include <cerrno>
#include <bits/functexcept.h>
#include <bits/functional_hash.h>
#include <bits/gthr.h>

#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

  /**
   * @defgroup threads Threads
   * @ingroup concurrency
   *
   * Classes for thread support.
   * @{
   */

  /// thread
  class thread
  {
  public:
    // Abstract base class for types that wrap arbitrary functors to be
    // invoked in the new thread of execution.
    struct _State
    {
      virtual ~_State();
      virtual void _M_run() = 0;
    };
FirePapaya
  • 509
  • 5
  • 21
  • 4
    _CLion underlines_ that isn't relevant. Does it compile? –  Feb 06 '18 at 12:20
  • What error message do you get? – Vertexwahn Feb 06 '18 at 12:21
  • 1
    OT: `#define BUFFER_SIZE 3`don't define integer constants. Use `const int`. –  Feb 06 '18 at 12:21
  • This code is wrong since you neither `join` nor `detach` joinable `thread` objects prior to their destruction. – Daniel Langr Feb 06 '18 at 12:22
  • @Vertexwahn Producer-Consumer_GUI\main.cpp:20:9: error: 'thread' is not a member of 'std' std::thread producer(produce); i get this error – FirePapaya Feb 06 '18 at 12:28
  • 1
    @DanielLangr you are right but i am sure that the error is caused not because of lack of the `join` or `detach` – FirePapaya Feb 06 '18 at 12:30
  • Can you look inside included and see if it has the class thread inside std namespace and it is not disabled by some macro? – Killzone Kid Feb 06 '18 at 12:53
  • @KillzoneKid `class thread { public: // Abstract base class for types that wrap arbitrary functors to be // invoked in the new thread of execution. struct _State { virtual ~_State(); virtual void _M_run() = 0; };` so yes, it is there, but i dont how to check whether it is disabled – FirePapaya Feb 06 '18 at 12:57
  • Have you tried adding switch `-std=c++11` to compiler? – bartop Feb 06 '18 at 13:00
  • @TeamBeam does it have `namespace std {` in front of it? In VS2017 for example namespace is defined in _STD_BEGIN macro just before the class – Killzone Kid Feb 06 '18 at 13:03
  • @bartop No, I haven't because in my CMake file there is already `set(CMAKE_CXX_STANDARD 11)` – FirePapaya Feb 06 '18 at 13:03
  • @KillzoneKid it has `namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION` right before the one i posted minutes ago. I guess it means it is in std, doesn't it? – FirePapaya Feb 06 '18 at 13:06
  • @TeamBeam Strange indeed. Maybe this topic could help: http://www.cplusplus.com/forum/beginner/169434/ – Killzone Kid Feb 06 '18 at 13:17
  • The debugging steps I would do is: 1. Get it compiling from the commandline first explicitly passing the `-std=c++11` flag. Once that is working, 2. Get it working in CMake – Gillespie Feb 06 '18 at 14:05

3 Answers3

0

can you make sure if the library is available in the CLion toolchain? For example Cygwin does have the include.

0

CLion shows things red when it can't link codes with the library. It is possibly a host environment variable error. Make sure your CMakeLists.txt is working and your environment variables, standard library linkage is correct as well as your compiler setup. Compiler version and and standard libraries compatible. (e.g. you are using a cross-compiler (RasPi, Android) but environment vars shows host library etc. will make it fail)

Check this relevant post, it may help.

C++11 std::threads vs posix threads

O Dundar
  • 90
  • 1
  • 6
  • Checked everything, headers are linked, but can't compile. I also tried to compile on eclipse the same error. Btw once I write `#include ` it appears in the outline. Also I would like to mention that there is no error hightlighting on `std::cout` so now I am thinking about downloading cygwin. I will try to compile the code on Linux a bit later as well. – FirePapaya Feb 06 '18 at 14:56
  • I am working on Linux, and I have countered similar problems when I had link errors with libraries. It seems your assumption with cygwin installation makes more sense. You may try a command line build on Linux to see if that works, if so you would try installing additional packages on Cygwin or reinstall it. – O Dundar Feb 06 '18 at 15:00
0

Ok, so I finally solved the problem. I installed Cygwin and in CLion Settings I manually linked C/C++ compilers (for some reason CLion was unable to auto-detect them). Cleared all and re-indexed the project. Now it shows no errors and code compiles.

Regarding MinGW, I read on cplusplus.com some posts regarding the issue but they were about previous versions of MinGW and it was said that they finally fixed it, however I tell: No, they didn't. Here there is a nice repository and its README file suggests that thread of win32 rely on gthreads, however i found gthread file in my libraries everything seemed ok... so still need to investigate the issue. Write your ideas and experience here if you know more.

As for now solution is Cygwin, use it instead of MinGW.

P.S. Thanks @KillzoneKid for links

FirePapaya
  • 509
  • 5
  • 21