Delaying a function

Pages: 123
I want to create a program that is running a few functions that do different things at once and I want to delay only one function without affecting the whole program like Sleep() does. I mean if I use Sleep(100) in one function, all functions in the program will pause for 0.1 seconds, when this function is called.

I think you have to create a multi-threaded program and then pause one of the threads, but if there's a better way to do it, please tell me.
Also, I don't know how multi-threaded programs work :D
This seems like just what I need, but it doesn't work!
I modified the example from that website slightly (the original wouldn't compile either) :
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 <thread>
#include <chrono>

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

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';
}

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));
    }
}


This gives me 12 errors:

main.cpp: In function 'int main()':
main.cpp:11:5: error: 'thread' is not a member of 'std'
main.cpp:11:17: error: expected ';' before 't1'
main.cpp:12:5: error: 'thread' is not a member of 'std'
main.cpp:12:17: error: expected ';' before 't2'
main.cpp:13:5: error: 'thread' is not a member of 'std'
main.cpp:13:17: error: expected ';' before 't3'
main.cpp:14:5: error: 'thread' is not a member of 'std'
main.cpp:14:17: error: expected ';' before 't4'
main.cpp:15:5: error: 't2' was not declared in this scope
main.cpp:16:5: error: 't4' was not declared in this scope
main.cpp: In function 'void f1(int)':
main.cpp:24:14: error: 'std::this_thread' has not been declared
main.cpp: In function 'void f2(int&)':
main.cpp:33:14: error: 'std::this_thread' has not been declared
Are you compiling with a compiler that supports C++11? If you are, have you tried specifying "-std=c++11" as a command line option?
Yes, I enabled the option to make g++ follow the C++ 2011 standard in Code::Blocks, I'm using TDM GCC MinGW 4.7.1 (I'm pretty sure that supports c++11 ...)
Ah, apparently MinGW doesn't support threading yet:
http://stackoverflow.com/questions/5930826/how-to-enable-experimental-c11-concurrency-features-in-mingw

You can use clang++ on windows with MinGW and it has support.
Last edited on
I suggest updating to latest version of MinGW. It supports threading now.
MiiniPaa wrote:
I suggest updating to latest version of MinGW. It supports threading now.
Where can I find the latest version? I checked at SourceForge, but the versioning seems to be messed up as it says v1.0, is that the right one? How should I set it up?

LB wrote:
You can use clang++ on windows with MinGW and it has support.
I can't figure out how to install Clang...
http://sourceforge.net/projects/mingw/files/ //official
http://sourceforge.net/projects/mingwbuilds/files/ //unofficial
You can try this. I propose automatic installer, because in that case auto-detect feature for finding compiler should work.

Alternatively you can do manual install. All you should do is replace old MingW files (i would rename MinGW directory to something like "MinGW.backup" in case you mess up, and place new one after that.) and then update names of compilers in Code::Block preferences (in Settings->Compiler and Debugger->Global Compiler settings->Toolchain executables)
Vidminas wrote:
I can't figure out how to install Clang...
Download the "experimental" version for MinGW, put it in C:/clang/ and add C:/clang/bin to your system %PATH%
Thanks MiiNiPaa I was able to download and set up MinGW 4.8.0 from the unofficial builds, haven't tested it yet, though....

As for Clang, I retrieved the source code from SVN (that's what it said to do if you want the experimental version), do I have to configure it with CMake and then build it? What should I use to build it? MinGW?

I have a feeling I got this all wrong and somewhere there's a download that I missed, in this case, can I have a link, please?
I want to try both Clang and MinGW...
http://llvm.org/releases/download.html#3.2
"Experimental Clang Binaries for Mingw32/x86 "
Oh right! :D
I thought it was a stable release and by "experimental" you meant I should get the source code and build it myself.
Seems like I should be fine now...

What are .sig files? I assume it's the signature of the compiler or something? Do I need one? If yes, what should I do with it?
It is not important to you, it is like a hash/verification/checksum.
Last edited on
Haha, I'm back into the world of confusion...
MinGW 4.8.0 seems to work, but it gives me very similar errors to 4.7.1
'thread' is not a member of 'std'
Did I do something wrong? Has anyone tried compiling the above example with MinGW 4.8.0?

How do you set up the toolchain executable paths for Clang?
With GNU GCC compilers it was very easy, because they all have almost identical names, but Code::Blocks doesn't seem to like LLVM compilers much when it comes to C++, there is no option for Clang, which option should I use and what are the executables called which I should add the locations of?
Don't compile with MinGW, you just need MinGW as a backing for Clang on Windows.

With Clang, just plop it in e.g.
C:/clang/
(any path with no spaces) and then you have to manually add C:/clang/bin to your system %PATH% environment variable (right click on My Computer -> Properties -> Advanced -> Environment Variables... -> scroll down in the second box and double click PATH -> Add a semicolon and then add the path to the bin folder, remember no spaces)

Then from the command line you should be able to use

clang++ -std=c++11 -o MyProgram.exe MyFile.cpp
Thinking... still thinking... ooooh!!!
So what you're saying is that Code::Blocks doesn't really support Clang, so I can't just set the paths in the toolchain executables, it doesn't work that way...
Alright, so I have to add the paths to:
1-> The Clang directory itself
2-> The "bin" folder that is within Clang
Then I have to compile programs using the command prompt to call the compiler... But...

Actually I downloaded Code::Blocks, because of:
1-> The debugger interface
2-> Simple one click builds and linking in projects

Is there really no way to integrate Clang into Code::Blocks?
(Actually it doesn't have to be Code::Blocks, I have nothing against using another IDE as long as it's cross-platform)

Second question:
What is LLVM? I think it's an equivalent o GCC, is that right? What are the differences and is one of them better than the other?
Vidminas wrote:
Alright, so I have to add the paths to:
1-> The Clang directory itself
2-> The "bin" folder that is within Clang
Just #2 only

And you should be able to use clang with Code::Blocks, it's just that clang formats error messages with colors and other pretty stuff best viewed in the console.

@Second question: it hasn't concerned me so I don't know (just finding the simplest way to get clang on windows took me days the first time)
When executing the command "clang++" in cmd, I instantly got this error:
clang++.exe - Unable To Locate Component

This application has failed to start because pthreadGC2.dll was not found.
Re-installing the application may fix this problem.

I took the hint and deleted Clang, then downloaded Clang 3.2 again and extracted it into C:\Clang-3.2
And there we go, the same error appears again!
I'm using Linux as my host OS and running Windows XP in VirtualBox as a guest OS.

// Another topic, that will only be important once I can get clang working in command line:
How do I set up Clang with Code::Blocks?
I did some research and found nothing, but people saying that it doesn't work yet. But also, these websites are slightly outdated, so maybe it's possible already?

So I was wondering, do I have to "use" MinGW, but explicitly specify the "clang++" option?
I mean - I pretty much have no idea how to do it. Well at least I know that you start by opening Code::Blocks (Captain Obvious is back!), then go to Settings -> Compiler -> Toolchain Executables.
Or did I get that wrong already?

If you're using Linux, why not just download clang for Linux?
Pages: 123