How to use threading + questions

Ok so i tried copying the threading example to code blocks and it didnt work, i checked the 2 boxes in the build options for using c++11 but it still doesnt work. what else does i need to do? also can you explain threading a little bit more to me im a bit confused, i read that its used to execute process on 2 different threads so im guessing that makes performance better but im not sure.

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>       // std::cout
#include <thread>         // std::thread

void foo()
{
  // do stuff...
}

void bar(int x)
{
  // do stuff...
}

int main()
{
  std::thread first (foo);     // spawn new thread that calls foo()
  std::thread second (bar,0);  // spawn new thread that calls bar(0)

  std::cout << "main, foo and bar now execute concurrently...\n";

  // synchronize threads:
  first.join();                // pauses until first finishes
  second.join();               // pauses until second finishes

  std::cout << "foo and bar completed.\n";

  return 0;
}




errors

||=== C++ threading, Debug ===|
C:\Users\Chay Hawk\Desktop\C++ threading\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\C++ threading\main.cpp|16|error: 'thread' is not a member of 'std'|
C:\Users\Chay Hawk\Desktop\C++ threading\main.cpp|16|error: expected ';' before 'first'|
C:\Users\Chay Hawk\Desktop\C++ threading\main.cpp|17|error: 'thread' is not a member of 'std'|
C:\Users\Chay Hawk\Desktop\C++ threading\main.cpp|17|error: expected ';' before 'second'|
C:\Users\Chay Hawk\Desktop\C++ threading\main.cpp|22|error: 'first' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\C++ threading\main.cpp|23|error: 'second' was not declared in this scope|
||=== Build finished: 6 errors, 0 warnings (0 minutes, 4 seconds) ===|
Last edited on
In windows,need vs2010+sp1 or vs2012,version too low.
based on some google results, c++11 <thread> is not supported in gcc 4.7, however when i use : http://www.compileonline.com/compile_cpp0x_online.php

it perfectly works, it has gcc 4.8
ok so can i update my gcc version?
Topic archived. No new replies allowed.