Multi-threading

forum,

Windows 7 Professional, Mingw-w64 compiler.

I'm having trouble getting multi-threading working. I'm using a very
simple example off the internet. It will compile but the thread
never runs. If I try to use "first" of "join()" I get an run time
error message about an "entry point" involving libstdc++-6.dll.

Here's the code:

#include <windows.h>
#include <iostream>
#include <thread>
using namespace std;

void foo()
{
cout << endl << " in foo " << endl << endl;
}


int main()
{
thread (foo);

return (0);
}

Any suggestions?

Jerry D.

You are declaring an anonymous thread. You need to declare a thread object, then join with the main thread before the process ends.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <thread>

void foo()
{
   std::cout<<"inside the thread\n";
}


int main()
{
  std::thread t1(foo);
  
  t1.join();
  return (0);
}
Last edited on
You need to declare the std::thread object constructed with foo() and call join() with that object:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <windows.h>
#include <iostream>
#include <thread>
using namespace std;

void foo()
{
cout << endl << " in foo " << endl << endl;
}


int main()
{
thread t(foo);
t.join();

return (0);
}
GoldenLizard: your program throws:
1
2
3
4
5
6
7
8
9
10
t erminate called after throwing an instance of 'in foo std::system_error
'

  what():  Invalid argument

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 255 (0xFF)   execution time : 4.445 s
Press any key to continue. 
Golden Lizard wrote:
You need to declare a thread object, detach it, then join with the main thread before the process ends.

A thread may not be "joined" once it is "detached." One should do one or the other, not both.

http://en.cppreference.com/w/cpp/thread/thread/detach
http://stackoverflow.com/a/22804813
Oh damn, sorry, I don't know why I had daemon threads in my mind. Fixed it.
Goldern Lizard,
Thanks for the replies.

I changed the line:
std::thread (foo);
to:
std::thread t1(foo);


It compiles but I get the runtime error message:

The procedure entry point
_ZNSt6thread15_M_start_threadESt10unique_ptrINS_6_StateESt14d
efault_deleteIS1_EEPFvvE could not be located in the dynamic link
library libstdc++-6.dll

Jerry D.
What's your build command line look like? Have you added the option -pthread?
See here and search other related for procedure entry point error messages:
http://stackoverflow.com/questions/25524620/the-procedure-entry-point-could-not-be-located-in-dynamic-link-library
Cplusplus forum,

My build command line was:
-g;-O0;-Wall

I added -pthread but it still failed. Then I enabled -std=c++11 for
-g;-O0;-std=c++11;-Wall -pthread Same error.

Based on some of the replies here I tried a different compiler and
it worked. I removed -pthread and it still worked but if I removed
-std=c++11 the error was "no rule to make target".

So the build line that works is -g;-O0;-std=c++11;-Wall

The compiler that worked is x86_64-w64-mingw32-g++.exe that came
with Code::Blocks 16.01.

Thanks for the help.

Jerry D.
Hi,

Should also have at least -Wextra -pedantic-errors compile options. It's a good idea to compile with as many warnings turned on as possible - warnings are your friend.

I use these as well:

http://www.cplusplus.com/forum/general/183731/#msg899203

Try to use the latest std the compiler supports: at the moment c++14, but c++17 is due out soon. g++ 6.3.1 already supports some of c++17, as does clang++ 3.9

So putting that together, you could have:

g++ -std=c++17 -Wall -Wextra -pedantic-errors -O0 -pthread *.cpp -o yourexename

That's not including the extra options I mentioned.

Try to get a better IDE than Code Blocks: there is no version control, and it doesn't do background compilation. There are lots of IDE's: I like QtCreator or KDevelop; could try Eclipse. Try a few see what you like.

Topic archived. No new replies allowed.