7

I followed the instruction here on setting up CodeBlocks to use GCC 4.7.1 .the sample provided at the page i mentioned is compiled just fine,But when i tried to compile the following code it generated the errors afterwards.I must say that i even set the compilers C++11 flag through the compiler settings (-std=c++11) but still no luck. Code which fails to compile:

#include <iostream>
#include <thread>
#include <vector>

//This function will be called from a thread

void func(int tid) {
    std::cout << "Launched by thread " << tid << std::endl;
}

int main() {
    std::vector<std::thread> th;

    int nr_threads = 10;

    //Launch a group of threads
    for (int i = 0; i < nr_threads; ++i) {
        th.push_back(std::thread(func,i));
    }

    //Join the threads with the main thread
    for(auto &t : th){
        t.join();
    }

    return 0;
}

Errors:

 main.cpp||In function 'int main()':|
 main.cpp|13|error: 'thread' is not a member of 'std'|
 main.cpp|13|error: 'thread' is not a member of 'std'|
 main.cpp|13|error: template argument 1 is invalid|
 main.cpp|13|error: template argument 2 is invalid|
 main.cpp|13|error: invalid type in declaration before ';' token|
 main.cpp|19|error: request for member 'push_back' in 'th', which is of non-class type 'int'|
 main.cpp|19|error: 'thread' is not a member of 'std'|
 main.cpp|23|error: no matching function for call to 'begin(int&)'|
 main.cpp|23|note: candidates are:|
 \include\c++\4.7.1\initializer_list|89|note: template<class _Tp> constexpr const _Tp* std::begin(std::initializer_list<_Tp>)|
 \include\c++\4.7.1\initializer_list|89|note:   template argument deduction/substitution failed:|
 main.cpp|23|note:   mismatched types 'std::initializer_list<_Tp>' and 'int'|
 \include\c++\4.7.1\bits\range_access.h|87|note: template<class _Tp, unsigned int _Nm> _Tp* std::begin(_Tp (&)[_Nm])|
 \include\c++\4.7.1\bits\range_access.h|87|note:   template argument deduction/substitution failed:|
 main.cpp|23|note:   mismatched types '_Tp [_Nm]' and 'int'|
 \include\c++\4.7.1\bits\range_access.h|58|note: template<class _Container> decltype (__cont.begin()) std::begin(const _Container&)|
 \include\c++\4.7.1\bits\range_access.h|58|note:   template argument deduction/substitution failed:|
 main.cpp|23|required from here|
 \include\c++\4.7.1\bits\range_access.h|58|error: request for member 'begin' in '__cont', which is of non-class type 'const int'|
 \include\c++\4.7.1\bits\range_access.h|48|note: template<class _Container> decltype (__cont.begin()) std::begin(_Container&)|
 \include\c++\4.7.1\bits\range_access.h|48|note:   template argument deduction/substitution failed:|
 main.cpp|23|required from here|
 \include\c++\4.7.1\bits\range_access.h|48|error: request for member 'begin' in '__cont', which is of non-class type 'int'|
 main.cpp|23|error: no matching function for call to 'end(int&)'|
 main.cpp|23|note: candidates are:|
 \include\c++\4.7.1\initializer_list|99|note: template<class _Tp> constexpr const _Tp* std::end(std::initializer_list<_Tp>)|
 \include\c++\4.7.1\initializer_list|99|note:   template argument deduction/substitution failed:|
 main.cpp|23|note:   mismatched types 'std::initializer_list<_Tp>' and 'int'|
 \include\c++\4.7.1\bits\range_access.h|97|note: template<class _Tp, unsigned int _Nm> _Tp* std::end(_Tp (&)[_Nm])|
 \include\c++\4.7.1\bits\range_access.h|97|note:   template argument deduction/substitution failed:|
 main.cpp|23|note:   mismatched types '_Tp [_Nm]' and 'int'|
 \include\c++\4.7.1\bits\range_access.h|78|note: template<class _Container> decltype (__cont.end()) std::end(const _Container&)|
 \include\c++\4.7.1\bits\range_access.h|78|note:   template argument deduction/substitution failed:|
 main.cpp|23|required from here|
 \include\c++\4.7.1\bits\range_access.h|78|error: request for member 'end' in '__cont', which is of non-class type 'const int'|
 \include\c++\4.7.1\bits\range_access.h|68|note: template<class _Container> decltype (__cont.end()) std::end(_Container&)|
 \include\c++\4.7.1\bits\range_access.h|68|note:   template argument deduction/substitution failed:|
 main.cpp|23|required from here|
 \include\c++\4.7.1\bits\range_access.h|68|error: request for member 'end' in '__cont', which is of non-class type 'int'|
 main.cpp|23|error: unable to deduce 'auto&' from '<expression error>'|
||=== Build finished: 14 errors, 4 warnings (0 minutes, 6 seconds) ===|
Community
  • 1
  • 1
Hossein
  • 24,202
  • 35
  • 119
  • 224
  • 3
    On Unix yes, but with mingw on windows no. – Jesse Good Aug 31 '12 at 06:40
  • 1
    Exactly. GCC probably implemented `std::thread` over `pthread`s, which means they wouldn't work over Windows Win32 threads. Get a UNIX system. – Linuxios Aug 31 '12 at 06:42
  • 2
    Yes, on Linux it relies on pthread. Btw this means that, on Linux, you also need to add a linker flag `-lpthread` to the command line. Otherwise it will compile, but either not link or produce run-time errors. – jogojapan Aug 31 '12 at 06:45
  • 1
    @jogojapan I think it is recommended to use the ``-pthread`` option, at least on linux. – juanchopanza Aug 31 '12 at 06:53
  • @juanchopanza You mean it's recommended, but not necessary? Sorry I don't understand how your statement is different from mine. (I made that statement because it wasn't mentioned in the compiler flags the OP gave, which might mislead other people, who use threads on Linux rather than mingw/Windows.) – jogojapan Aug 31 '12 at 07:00
  • 1
    @jogojapan ``-pthread`` did more than just link libpthread, but I don't recall right now what exactly "more" is. – juanchopanza Aug 31 '12 at 07:01
  • @juanchopanza Ah. Got it. `-pthread` rather than `-lpthread`. Yes, you are right. I should have said `-pthread`. – jogojapan Aug 31 '12 at 07:05
  • 1
    @jogojapan,@juanchopanza: There is a relevant [SO question](http://stackoverflow.com/questions/2127797/gcc-significance-of-pthread-flag-when-compiling). – Jesse Good Aug 31 '12 at 07:06

3 Answers3

7

With mingw, the thread support of gcc depends on which build you get. Some builds don't support threads, others do.

I believe that the mingw build at http://code.google.com/p/mingw-builds/downloads/list supports threads.

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
2

MinGW simply won't compile with '-std=c++0x'. Strange enough, '-std=gnu++0x' works.

Akhil Thayyil
  • 9,263
  • 6
  • 34
  • 48
0

Try MinGw builds:

http://sourceforge.net/projects/mingwbuilds/

This installer will allow you to choose whatever MinGW you want and also includes c++11 thread functionality.

cmd
  • 11,622
  • 7
  • 51
  • 61