Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

error compiling simple threading example...

56 views
Skip to first unread message

Doug Mika

unread,
Jun 9, 2015, 3:10:25 PM6/9/15
to
I get the following error:

welcome.cc: In function 'void pause_thread(int)':
welcome.cc:8:8: error: 'std::this_thread' has not been declared
std::this_thread::sleep_for(std::chrono::seconds(n));

when I try to compile the following simply thread example.

// example for thread::join
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds

void pause_thread(int n)
{
std::this_thread::sleep_for(std::chrono::seconds(n));
std::cout << "pause of " << n << " seconds ended\n";
}

int main()
{
std::cout << "Spawning 3 threads...\n";
std::thread t1(pause_thread,1);
std::thread t2(pause_thread,2);
std::thread t3(pause_thread,3);
std::cout << "Done spawning threads. Now waiting for them to join:\n";
t1.join();
t2.join();
t3.join();
std::cout << "All threads joined!\n";

return 0;
}




Could someone hint at why?

Thanks to all in advance.
(Using MinGW with the following command:
g++ -std=c++11 -c -g -MMD -MP -MF "build/Debug/MinGW-Windows/welcome.o.d" -o build/Debug/MinGW-Windows/welcome.o welcome.cc)

Ian Collins

unread,
Jun 9, 2015, 3:13:29 PM6/9/15
to
Doug Mika wrote:
> I get the following error:
>
> welcome.cc: In function 'void pause_thread(int)':
> welcome.cc:8:8: error: 'std::this_thread' has not been declared
> std::this_thread::sleep_for(std::chrono::seconds(n));

Your compiler is too old? Which version are you using?

--
Ian Collins

Doug Mika

unread,
Jun 9, 2015, 3:14:34 PM6/9/15
to
4.8.1

Doug Mika

unread,
Jun 9, 2015, 3:20:14 PM6/9/15
to
Perhaps I should mention that compiling the code in:
http://www.tutorialspoint.com/compile_cpp11_online.php

give me the following error message:
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)

Which begs the question, where is the setting to enable threading, and is there a flag that is needed in MinGW to enable threading?

Chris M. Thomasson

unread,
Jun 9, 2015, 6:42:01 PM6/9/15
to
> "Doug Mika" wrote in message
> news:3d6437f0-f89e-4dcb...@googlegroups.com...

[...]
> Perhaps I should mention that compiling the code in:
> http://www.tutorialspoint.com/compile_cpp11_online.php

> give me the following error message:
> terminate called after throwing an instance of 'std::system_error'
> what(): Enable multithreading to use std::thread: Operation not
> permitted
> Aborted (core dumped)

> Which begs the question, where is the setting to enable threading,
> and is there a flag that is needed in MinGW to enable threading?

Try using the following:

g++ -std=c++11 -pthread -o main *.cpp

witch compiles your example. It runs fine with:

http://www.tutorialspoint.com/compile_cpp11_online.php

;^)

Doug Mika

unread,
Jun 9, 2015, 10:30:19 PM6/9/15
to
That worked absolutely seamlessly. Now what are the chances you'll know how to get the same problem fixed on MinGW 4.8.1 running on Netbeans GUI?

Chris M. Thomasson

unread,
Jun 10, 2015, 12:44:07 AM6/10/15
to
> "Doug Mika" wrote in message
> news:dc121475-2de9-4797...@googlegroups.com...

> > On Tuesday, June 9, 2015 at 5:42:01 PM UTC-5, Chris M. Thomasson wrote:
[...]

> That worked absolutely seamlessly.

> Now what are the chances you'll know how to get the
> same problem fixed on MinGW 4.8.1 running on Netbeans GUI?

The following command line works for me with mingw 4.9.2:

g++ -std=c++11 -pthread main.cpp

I have not used Netbeans in many years. The last time I
used it was when I was working in Solaris. I cannot remember
how you can change the build options. However, this may
help you out a bit:

http://stackoverflow.com/questions/25683355/how-do-i-add-std-c11-to-the-compiler-options-in-my-netbeans-ide

https://forums.netbeans.org/ntopic18439.html


Gook luck Doug!

:^)

Doug Mika

unread,
Jun 10, 2015, 11:31:00 AM6/10/15
to
So I came upon a little program to test whether MinGW supports threads on windows platforms:

#include <thread>
#include <iostream>

int main()
{
#if !defined(_GLIBCXX_HAS_GTHREADS)
std::cout << "glib doesn't have gthreads on my platform\n";
#endif
}

Unfortunately mine doesn't. So here is a question, does anyone know of a work around for this problem on MinGW or better yet, is there even a windows compiler that supports all the defined C++11 standards?

Chris M. Thomasson

unread,
Jun 10, 2015, 2:09:44 PM6/10/15
to
> "Doug Mika" wrote in message
> news:4ab0c845-49e5-4370...@googlegroups.com...

[...]

> So I came upon a little program to test whether MinGW
> supports threads on windows platforms:

[...]

MingW does indeed support C++11 threads on Windows Doug.

FWIW, here is the one I am using:

http://tdm-gcc.tdragon.net

Chris Vine

unread,
Jun 10, 2015, 2:59:46 PM6/10/15
to
Given the name clash it is an easy mistake to make, but your test has
nothing to do with glib, which is a C library providing cross-platform
support for various programming utilities (and which is connected with
the GTK+ UI toolkit). Instead it is an indication of whether your
g++/libc implementation supports threads for the particular gcc
distribution in question.

Clearly yours doesn't. You probably need to get a different MinGW
package. Beyond that I cannot help, as I have never used MinGW.

Chris

Chris M. Thomasson

unread,
Jun 10, 2015, 4:17:38 PM6/10/15
to
> "Chris M. Thomasson" wrote in message
> news:ml9ugq$mgj$1...@speranza.aioe.org...
Heck, it even supports thread_local keyword.

A modified version of your original example:
____________________________________________________________
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds


static thread_local int g_tls = 0;


void pause_thread_call()
{
std::cout << "pause of " << g_tls << " seconds ended\n";
}


void pause_thread(int n)
{
g_tls = n;
std::this_thread::sleep_for(std::chrono::seconds(n));
pause_thread_call();
}


int main()
{
std::cout << "Spawning 3 threads...\n";
std::thread t1(pause_thread, 1);
std::thread t2(pause_thread, 2);
std::thread t3(pause_thread, 3);
std::cout << "Done spawning threads. Now waiting for them to join:\n";
t1.join();
t2.join();
t3.join();
std::cout << "All threads joined!\n";

return 0;
}
____________________________________________________________



;^)

Chris M. Thomasson

unread,
Jun 10, 2015, 4:25:22 PM6/10/15
to
> "Doug Mika" wrote in message
> news:4ab0c845-49e5-4370...@googlegroups.com...

[...]

> So here is a question, does anyone know of a work around for
> this problem on MinGW or better yet, is there even a windows
> compiler that supports all the defined C++11 standards?

You can go for a visual studio download here:

Chris M. Thomasson

unread,
Jun 10, 2015, 4:29:04 PM6/10/15
to
> "Chris M. Thomasson" wrote in message
> news:mla6f4$cic$1...@speranza.aioe.org...
OOPS! I forgot to post the darn link:

First, think about getting a windows live account which is required
to activate the product. Then go here:

https://www.visualstudio.com

Click on the green button that says free in the upper right hand
corner of the screen.


Actually, msvc 2013 is not all that bad. Its debugger happens to be
very nice.

Chris M. Thomasson

unread,
Jun 10, 2015, 5:26:40 PM6/10/15
to
> "Chris M. Thomasson" wrote in message
> news:mla6f4$cic$1...@speranza.aioe.org...

Beware, msvc 2013 has better support for C++11 when compared to C11.

Simple example, I cannot get `_Thread_local' to work, however
`thread_local' works fine...

damn.

Chris M. Thomasson

unread,
Jun 10, 2015, 5:40:21 PM6/10/15
to
> "Chris M. Thomasson" wrote in message
> news:mlaa23$lft$1...@speranza.aioe.org...

[...]

> Beware, msvc 2013 has better support for C++11 when compared to C11.

> Simple example, I cannot get `_Thread_local' to work, however
> `thread_local' works fine...

> damn.

Sorry for all of the pesky posts... However, I made a mistake wrt the
information above. MSVC 2013 does appear to support the `thread_local'
keyword. There is a rather ugly workaround, something like:
________________________________________________
// `thread_local' keyword "workaround" for MSVC, damn!
#if defined (_MSC_VER)
# if (_MSC_VER <= 1800)
# define thread_local_storage __declspec(thread)
# else
// assume more recent versions of msvc has `thread_local'...
# define thread_local_storage thread_local
# endif
#else
# define thread_local_storage thread_local
#endif
________________________________________________

YIKES!

;^o

Richard

unread,
Jun 12, 2015, 6:48:00 PM6/12/15
to
[Please do not mail me a copy of your followup]

Doug Mika <doug...@gmail.com> spake the secret code
<4ab0c845-49e5-4370...@googlegroups.com> thusly:

>Unfortunately mine doesn't. So here is a question, does anyone know of a
>work around for this problem on MinGW or better yet, is there even a
>windows compiler that supports all the defined C++11 standards?

Clang for Windows (either in MinGW style mode or using CL.EXE
compatible command-line arguments) supports all of C++11.
<http://llvm.org/releases/download.html>

Visual Studio 2013 Community Edition (2015 will be out soon) supports a
good chunk of C++11:
<https://www.visualstudio.com/>

See <http://en.cppreference.com/w/cpp/compiler_support> for a breakdown
of support by feature. If you consult that page, please be sure to
check the Discussion link as well for additional information.
<http://en.cppreference.com/w/Talk:cpp/compiler_support>
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
0 new messages