Delaying a function

Pages: 123
Because the program I'm making only works on Windows.
I'm planning to re-write it with NCurses in the future for cross-platform support, when I manage to set it up (I might start a new topic for that, please give me a link if someone has asked this before - I couldn't find any).
Anyway, is Clang 3.2 broken, or did I mess something up?

I'll try to get it working on Linux, with the example in the third post.
Last edited on
I'm running Clang 3.2 with MinGW on several Windows machines and have not had the problem you describe on any of them (XP, Vista, 7). I'm really not sure why you're having that issue. Are you absolutely sure MinGW is in C:/MinGW/ and clang is in C:/clang/ ? They can't be in subfolders.
> This seems like just what I need, but it doesn't work!

Instead of struggling for days on end, why don't you just use boost::thread and boost::chrono and be done with it?

The code is identical, except std:: is replaced with boost::
Compile with -lpthread, link with -lboost_system -lboost_thread -lboost_chrono

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
30
31
32
33
34
35
#include <iostream>
#include <boost/thread.hpp>
#include <boost/chrono.hpp>

void f1(int n);
void f2(int &n);

int main()
{
    int n = 0;
    boost::thread t1; // t1 is not a thread
    boost::thread t2(f1, n + 1); // pass by value
    boost::thread t3(f2, std::ref(n)); // pass by reference
    boost::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
    t2.join();
    t4.join();
    std::cout << "Final value of n is " << n << '\n';
}

void f1(int n)
{
    for (int i = 0; i < 5; i++) {
        std::cout << "Thread " << n << " executing\n";
        boost::this_thread::sleep_for(boost::chrono::milliseconds(10));
    }
}

void f2(int& n)
{
    for (int i = 0; i < 5; i++) {
        std::cout << "Thread 2 executing\n";
        n++;
        boost::this_thread::sleep_for(boost::chrono::milliseconds(10));
    }
}
MinGW is in C:\Program Files\CodeBlocks\MinGW\ (it's there by default if you install Code::Blocks and the compiler in a bundle that Code::Blocks provides)
Clang is in C:\Clang-3.2\

Do you mean that clang++ needs MinGW to be in C:\MinGW\ ?
I can re-install Code::Blocks and the compiler separately, and then put MinGW in C:\MinGW\ if I have to...
@Vidminas: You have to install MinGW to a path with no spaces in it ("Program Files" has spaces in it so clang can't find it)
Last edited on
@JLBorges the problem is not with Clang, mostly it's just me (I'm really blunt sometimes and I'm very sorry about that) and possibly it's my computer's fault too.

I re-installed everything. (downloaded new files, in case my last ones were broken)
Code::Blocks is in C:\CodeBlocks
MinGW is in C:\MinGW32
Clang is in C:\Clang
(I managed to set up PDCurses in C:\PDCurses-3.4 as well ^_^)

Clang still doesn't work, giving me the same error message I described before.
That's it, I'm moving on to Boost now.
I downloaded Boost 1.53.0 from boost.org and I tried setting it up, well Boost doesn't like me either.
I get something about STACK ITERATION OVERLOAD in cmd...
I think this laptop isn't capable of building Boost.

I managed to set up Clang on Linux and it works, but gives me errors when trying to compile the example.

vidminas@vidmino-ThinkPad-R50e:~/Desktop$ clang++ -std=c++11 -o multi main.cpp
In file included from main.cpp:3:
In file included from /usr/bin/../lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/thread:37:
/usr/bin/../lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/chrono:534:6: error: 
      no matching constructor for initialization of 'duration' (aka
      'std::chrono::duration<long long, std::ratio<1, 1000000> >')
          : __d(__t.time_since_epoch())
            ^   ~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/condition_variable:105:42: note: 
      in instantiation of function template specialization
      'std::chrono::time_point<std::chrono::system_clock,
      std::chrono::duration<long long, std::ratio<1, 1000000> >
      >::time_point<std::chrono::duration<long long, std::ratio<1, 1000000000> >
      >' requested here
        const __clock_t::time_point __s_atime = __s_entry + __delta;
                                                ^
/usr/bin/../lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/chrono:233:12: note: 
      candidate constructor not viable: no known conversion from
      'duration<[...], ratio<[...], 1000000000>>' to 'duration<[...],
      ratio<[...], 1000000>>' for 1st argument
        constexpr duration(const duration&) = default;
                  ^
/usr/bin/../lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/chrono:239:23: note: 
      candidate template ignored: couldn't infer template argument ''
          constexpr explicit duration(const _Rep2& __rep)
                             ^
/usr/bin/../lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/chrono:246:14: note: 
      candidate template ignored: couldn't infer template argument ''
          constexpr duration(const duration<_Rep2, _Period2>& __d)
                    ^
/usr/bin/../lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/chrono:231:12: note: 
      candidate constructor not viable: requires 0 arguments, but 1 was provided
        constexpr duration() : __r() { }
                  ^
1 error generated.


I'll try this on another computer running Windows XP.

And LB, thank you so much for your time.
> I downloaded Boost 1.53.0 from boost.org and I tried setting it up, well Boost doesn't like me either.
> I get something about STACK ITERATION OVERLOAD in cmd...
> I think this laptop isn't capable of building Boost.

The Nuwen MinGW build includes pre-built binaries for Boost 1.53.
http://nuwen.net/mingw.html


It's a relief that the Nuwen distro was so simple to set up.
I added "C:\MinGW\bin" to PATH as well.
Code::Blocks couldn't auto-detect the installation, so I selected the toolchain executables myself.
I tried compiling the code that JLBorges posted above.
I got 3 errors, this is the Build log:

-------------- Build: Release in function delay (compiler: GNU GCC Compiler)---------------

g++.exe -Wall -fexceptions  -O2    -IC:\PDCurses-3.4 -IC:\PDCurses-3.4\win32  -c "C:\Documents and Settings\Vidminas\My Documents\C++ work\function delay\main.cpp" -o obj\Release\main.o
g++.exe -LC:\PDCurses-3.4 -LC:\PDCurses-3.4\win32  -o "bin\Release\function delay.exe" obj\Release\main.o   -s  
obj\Release\main.o:main.cpp:(.text+0x40f): undefined reference to `boost::chrono::steady_clock::now()'
obj\Release\main.o:main.cpp:(.text+0x421): undefined reference to `boost::chrono::system_clock::now()'
obj\Release\main.o:main.cpp:(.text+0x46c): undefined reference to `boost::chrono::steady_clock::now()'
C:\MinGW\bin/ld.exe: obj\Release\main.o: bad reloc address 0x2 in section `.text$_ZN5boost16exception_detail10clone_baseD1Ev[__ZN5boost16exception_detail10clone_baseD1Ev]'
C:\MinGW\bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 19 seconds)
3 errors, 0 warnings (0 minutes, 19 seconds)

It says it's a linking error, but I doubt it, because of
undefined reference to `boost::chrono::steady_clock::now()'
I think this is causing the trouble.

And Clang 3.2 didn't work on the other machine either...
Vidminas wrote:
It says it's a linking error, but I doubt it, because of
undefined reference to `boost::chrono::steady_clock::now()'
I think this is causing the trouble.
That is a linking error. Does boost require you to link to a library for that function? (Otherwise you wouldn't "build" boost, it's just headers for most things)
Last edited on
Thanks LB you helped me solve this.
In Settings -> Compiler -> Search directories,
under "Compiler" I added "C:\MinGW\bin" and under "Linker" "C:\MinGW\lib"
Then in the project's "build options" I had to link the libraries "libboost_system.a", "libboost_thread.a" and "libboost_chrono.a".
Then the code worked! Yay! Thank you all so much for bearing with me!

Another thing :P
Can I have the step-by-step instructions for setting up Clang again, please?
I'm almost sure I made a mistake somewhere, because I get the same error on my 2 computers. I'll do some research about that error.
OK:
1. Install MinGW to C:/MinGW/
2. Add C:/MinGW/bin to your %PATH% environment variable
3. Download clang version for MinGW and place it in C:/clang
4. Add C:/clang/bin to your %PATH% environment variable
5. Open a command prompt window and test "clang++ -std=c++11 -o HelloWorld.exe HelloWorld.cpp" (assuming you have a HelloWorld.cpp to test with)

I've done this in several computers without issue. Someone else who had your issue had installed MinGW and clang to a subfolder and didn't add them to the path variable properly, after installing them as instructed and adding them to %PATH% it worked for them. It's a mundane process because I have to do it repeatedly on a system that constantly gets re-imaged.
@LB: I hope that windows is not so broken, so you can escape the spaces
@ne555 escape as you wish, it doesn't help that somewhere between clang and MinGW they calculuate paths themselves and don't escape or put quotes.
I did exactly as you said LB... I get the error that pthreadGC2.dll is missing. I did some research and in one of the articles it said to combine Clang and MinGW (like copy Clang's bin into C:\MinGW\bin and so on...), I did that, but I still keep getting the same error when just entering clang or clang++ in cmd. (I also tried cd'ing into the MinGW directory, but it was the same)

Then I found another article (http://mingw-users.1079350.n2.nabble.com/missing-pthreadGC2-dll-td5533063.html) where it says that the "pthreadGC2.dll" was re-named to "libpthread-2.dll" in later MinGW releases.
LB which version of MinGW are you using?
I set up mine from the Nuwen distro and it is 4.8.0
Ah, try duplicating the DLL to the old name (e.g. create a hard link or just plain copy it).

I'm using an older version of MinGW, I did not realize they broke compatibility in the latest versions. I will keep this in mind when telling others.
Alright this is getting really weird...
Nuwen's MinGW 4.8.0 doesn't even have the "libpthread-2.dll"
Here's my story:

I downloaded a few unofficial builds from the MinGW SourceForge project and I couldn't find the exact .dll either, the only thing I found was "libwinpthread-1.dll" - seemed like the right one.
I copied this dll into C:\Clang\bin...
It gave me another error, saying that "libgcc_s_dw2-1.dll" is missing. I found "libgcc_s_sljl-1.dll", luckily I already know about SLJL and Dwarf2 binding, so I understood that I have to copy the dll from the Dwarf2 build.

From the MinGW 4.8.0 Dwarf2 build I copied both "libwinpthread-1.dll" and "libgcc_s_dw2-1.dll" into C:\Clang\bin (because the sizes of "libwinpthread-1.dll" were different, and, of course, I renamed it to "pthreadGC2.dll")

Then, this is kind of funny, but it asked me for "libwinpthread-1.dll" !
So I copied the one from the SLJL build just for some variation...

And of course there was another error, this time it was asking for "libstdc++6.dll"...

Here I kind of lost my temper and deleted the Nuwen MinGW build, temporarily replacing it with "x32-4.8.0-release-win32-dwarf-rev2"
Again it asked for "pthreadGC2.dll", this time I knew that I shouldn't rename the other .dll so I went to do more research on the internet.
Finally I found the pthreadGC2.dll here: ftp://sourceware.org/pub/pthreads-win32/dll-latest/dll/x86/

The command prompt didn't accept "clang++ -std=c++11 -o HelloWorld.exe HelloWorld.cpp" , you have to write it without the quotes...

The end is near and by now, you probably know what happened.
clang++ crashes...

Now I know what people mean by "Welcome to the world of dlls"

LB so you didn't tell me what version of MinGW you use. I want try it out!
Vidminas wrote:
The command prompt didn't accept "clang++ -std=c++11 -o HelloWorld.exe HelloWorld.cpp" , you have to write it without the quotes...
I thought that you knew that, sorry.

Vidminas wrote:
LB so you didn't tell me what version of MinGW you use. I want try it out!
I use the one from the official downloads page here:
http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/mingw-get-inst-20120426/
In the installer I choose to install the pre-packaged stuff, not the latest packages.
The version of GCC from the pre-packaged snapshot seems to be 4.6.2-1. Fairly outdated...
Somebody should ask Clang and LLVM developers to make Clang compatible with the latest stable release of MinGW (4.7.1, right?)

Anyway I set up MinGW 4.6.2 and Clang passed the "Hello World!" test.
I even managed to set it up with Code::Blocks!

It doesn't work with the threading program though (yes, it worked with Boost, but LB said it would work with Clang too)
Here's the code again:
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
30
31
32
33
34
35
#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include <functional>
#include <atomic>

void f1(int n)
{
    for (int i = 0; i < 5; ++i) {
        std::cout << "Thread " << n << " executing\n";
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}

void f2(int& n)
{
    for (int i = 0; i < 5; ++i) {
        std::cout << "Thread 2 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}

int main()
{
    int n = 0;
    std::thread t1; // t1 is not a thread
    std::thread t2(f1, n + 1); // pass by value
    std::thread t3(f2, std::ref(n)); // pass by reference
    std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
    t2.join();
    t4.join();
    std::cout << "Final value of n is " << n << '\n';
}

First you have to pass -std=c++0x or -std=c++11 for the code to compile.

Then I get 9 errors which are mostly:
error: no member named 'this_thread' in namespace 'std'
error: no member named 'thread' in namespace 'std'
error: no type named 'thread' in namespace 'std'


It also gives an error in C:\MinGW\gcc\mingw32\include\c++\ext\concurrence.h
error: no matching function for call to '_S_destroy'


I get the exactly same errors when trying to compile in console and in Code::Blocks (which means that I probably set it up correctly!)
Last edited on
Hm, at this point I don't know, it works for me. I don;t use MinGW to compile because as you say it is outdated, but Clang needs it on Windows.

I'm sorry, I just don't know how to resolve those errors - it works on all my machines.
Last edited on
I do not know what parts of MinGW Clang uses, but it might be related to fact that thread support was added in MinGW 4.7.
Pages: 123