Can't use threads

Pages: 1234
Hello, I have a problem with multithreading.
I thried this minimal code:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <thread>

using namespace std;

int main ()
{
  	thread mythread([](){cout<<"Hello World!";});
	mythread.detach();
	mythread.join();
}

With MinGW 4.7.1 32-bit on the Orwell Dev-C++ 5.2.0.3 IDE, compiled with -std=c++11 -fpermissive -Wall -Wextra -pthread and it produced the following errors:
The compiler wrote:
C:\Users\Vilim\Desktop\New folder\Neimenovano2.cpp In function 'int main()':
8 4 C:\Users\Vilim\Desktop\New folder\Neimenovano2.cpp [Error] 'thread' was not declared in this scope
8 11 C:\Users\Vilim\Desktop\New folder\Neimenovano2.cpp [Error] expected ';' before 'mythread'
8 47 C:\Users\Vilim\Desktop\New folder\Neimenovano2.cpp [Error] expected primary-expression before ')' token
8 47 C:\Users\Vilim\Desktop\New folder\Neimenovano2.cpp [Error] expected ';' before ')' token
9 2 C:\Users\Vilim\Desktop\New folder\Neimenovano2.cpp [Error] 'mythread' was not declared in this scope

I read every thread tutorial I could find and I don't know what's causing this!
Are you sure C++11 is properly compiling?
If you haven't used any of the C++11 features before try this code:
1
2
3
4
5
6
7
8
9
10
#include <vector>
#include <iostream>

int main()
{
    std::vector<int> intVector = {1,2,3,4,5,6,7,8,9}; // C++11 initialiser list
    for (auto& num: intVector) // auto & range based loop
        std::cout << num << std::endl;
    std::cin.get();
}


Otherwise I'm not really sure.
Last edited on
No, I used a LOT! Here's something that works for me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <forward_list>
#include <array>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>

using namespace std;

struct foo
{
	foo(int)
	{}
	foo(foo&&)=default;
	foo(const foo&)=default;
};

int bar()=delete;

int main ()
{
  	vector<foo> a={1, 6, 9, 10, -2876};
  	for (const auto& x: a);
  	for_each(x.begin(), x.end(), [](decltype(*x.begin())){});
  	unordered_map<int, vector<double>> x;
  	unordered_set<bool> foobar;
  	cout<<alignof(foobar);
}

It's all perfect, no compile errors, no run-time rrors. But with theads...
Last edited on
The only 2 things I tired and don't work are alignas() and threads.
Does MinGW 4.7.1 contain the same functionality as GCC 4.7.1?

The code in the first post compiles for me in GCC 4.7.1 but gives a runtime error.
terminate called after throwing an instance of 'std::system_error'
what(): Invalid argument
Hello World!Aborted
Probably because you use both detach() and join().

I doubt the code in your last post works for you because it contains a few errors.
You think that I can't use threads becouse I use MinGW instead of the original GCC?
Last edited on
I don't know.
Should I download GCC and see if that works?
Depends on how MinGW was built. Check it out with:
> gcc -v

If the output shows either --enable-threads=win32 or Thread model: win32, there is no C++11 thread support.

If instead, it was built with the MinGW-w64 winpthreads library --enable-threads=winpthreads C++11 concurrency is supported to the extent that GCC supports it.

Haven't used it myself, but I'm told that this is one such build:
http://mingw-w64.sourceforge.net/
You need to link statically to libstdc++ with the -static switch, though.

AFAIK, the latest Microsoft C++ has better C++11 concurrency support than GNU.

You will not be able to run GCC on Windows. MinGW is the windows port of GCC.
Heres what it outputs with -v:
My compiler wrote:
mingw32
../gcc-4.7.0/configure --enable-languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp --disable-win32-registry --enable-libstdcxx-debug --disable-build-poststage1-with-cxx --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw
win32
--param ggc-min-expand=100 --param ggc-min-heapsize=131072
--param ggc-min-expand=100 --param ggc-min-heapsize=131072
9fc40f37d31a03e5502141ce58bdf4c7
BTW I'm on a 32-bit computer, with a 32-bit processor and 32-bit OS and I'm SURE it's a 32-bit compiler.
Last edited on
MinGW-w64 works with 32 bit too. At least they say so on their homepage.
OK, so I cna't use threads AT ALL with the regular 32-bit MinGW?
> Heres what it outputs with -v ...

That build does not seem to have any thread support (other than native win32 threads) at all. Check out these preprocessor defines:

_GLIBCXX_HAS_GTHREADS && (ATOMIC_INT_LOCK_FREE > 1)
_GLIBCXX_HAS_GTHREADS isn't defined and ATOMIC_INT_LOCK_FREE is 1.
(other than native win32 threads)

How to use those?
> How to use those?

Don't. Use Boost threads instead; a later transition to C++11 threads would then be painless.
http://www.boost.org/doc/libs/1_50_0/doc/html/thread.html
http://www.boost.org/doc/libs/1_50_0/doc/html/thread/compliance.html#thread.compliance.cpp11
Last edited on
And how to make that later transition ot C++11 threads?
> And how to make that later transition ot C++11 threads?

For the most part, by replacing boost::<whatever> with std::<whatever>
But, as I said, it doesn't work!
Pages: 1234