0

I'm going to keep it fast and simple.

Here's the code I've written:

#include <iostream>
#include <thread>

void myFunc()
{
    std::cout << "Hello!" << std::endl;
}

int main()
{
    std::thread myThread(myFunc);

    myThread.join();

    return 0;
}

This is the command I've used to compile the program:

g++ threads.cc -o threads.exe -std=c++11

This is the error I'm getting:

threads.cc: In function 'int main()':
threads.cc:11:6: error: 'thread' is not a member of 'std'
      std::thread myThread(myFunc);
      ^
threads.cc:13:6: error: 'myThread' was not declared in this scope
      myThread.join();
      ^

I'm running Windows 10 and as you can see I'm trying to compile the program with GCC.

Can anyone help me with this? I've tried a few different ways to do multi-threading in C++, but none have worked.

The day after tomorrow I'll attend to a programming competition at my school and to make things go faster (the programs may only take up to 5 seconds to process data, which can be a lot) I'm thinking multi-threading could really help. You might think I should know this if I learn programming at school, but I haven't started that course yet and I'm just interested in the competition because it seems fun.

EDIT:

I have now installed the packages "mingw32-libpthreadgc" and "mingw32-libpthreadgce" (both the dev and dll classes). I also installed the "mingw32-pthreads-w32" (dev, doc and lic classes).

I have tried a few different includes <thread>, <pthread>, <pthread.h>. I have also tried adding the flag "-pthread" and "-lpthread". Also, I don't think we'll be allowed to use those flags anyways.

Nothing has worked so far.

I would be awesome if anyone could give a concrete example of which package / packages must be installed, which file / files to include and a short example code (in case mine won't work). We're not allowed to use any libraries except for the standard ones, but running a linux vm is probably fine, so any linux-only example works too.

I haven't found an example like that anywhere yet, so that would be awesome! Thanks!

(Yes, I did restart my computer)

  • 2
    Is this a duplicate of https://stackoverflow.com/questions/37358856/does-mingw-w64-support-stdthread-out-of-the-box-when-using-the-win32-threading ? Seems that MinGW does not ship with threads by default, you have to install them separately. I think MinGW installer asks you about what threading model you want to use. –  Nov 25 '17 at 22:07
  • 2
    Add `-pthread` to the command – Galik Nov 25 '17 at 22:12
  • 1
    *The day after tomorrow I'll attend to a programming competition at my school and to make things go faster I'm thinking multi-threading could really help.* you are not going to learn how to use threads correctly in two days, you are just going to add new creative ways of segfaulting your program. The easiest way to make your programs go faster is probably to enable optimizations - put `-O3` on your command line. – Matteo Italia Nov 26 '17 at 11:04

1 Answers1

1

When you install mingw sepect posix threads instead of default

rexxar2
  • 97
  • 9