Running multiple functions at the same time?

16,031

Solution 1

You don't need to run parallel tasks in order to measure the elapsed time. An example in C++11:

#include <chrono>
#include <string>
#include <iostream>

int main()
{
    auto t1 = std::chrono::system_clock::now();

    std::string s;
    std::cin >> s;
    // Or whatever you want to do...

    auto t2 = std::chrono::system_clock::now();
    auto elapsedMS =
        (std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1)).count()

    std::cout << elapsedMS;
}

EDIT:

Since it seems you are interested in a way to launch several tasks in parallel, here is a hint (again, using C++11):

#include <ctime>
#include <future>
#include <thread>
#include <iostream>

int long_computation(int x, int y)
{
    std::this_thread::sleep_for(std::chrono::seconds(5));

    return (x + y);
}

int main()
{
    auto f = std::async(std::launch::async, long_computation, 42, 1729);

    // Do things in the meanwhile...
    std::string s;
    std::cin >> s;
    // And we could continue...

    std::cout << f.get(); // Here we join with the asynchronous operation
}

The example above starts a long computation that will take at least 5 seconds, and in the meanwhile does other stuff. Then, eventually, it calls get() on the future object to join with the asynchronous computation and retrieve its result (waiting until it is finished if it hasn't finished yet).

Solution 2

If you really want to use threads, not just counting time you can use boost.

Example:

include <boost/thread.hpp>

void task1() { 
    // do something
}

void task2() { 
    // do something
}

void main () {
    using namespace boost; 
    thread thread1 = thread(task1);
    thread thread2 = thread(task2);
    thread2.join();
    thread1.join();
}

Solution 3

I'm not trying to do this timing thing in particular, it's just the easiest example I could come up with to ask about running functions independently..

Then you may want to look into multithreading. In C++11, you can do this:

#include <thread>
#include <iostream>

void func1() {
    std::cout << "func1" << std::endl;
}

void func2() {
    std::cout << "func2" << std::endl;
}

int main() {
    std::thread td1(func1);
    std::thread td2(func2);
    std::cout << "Started 2 threads. Waiting for them to finish..." << std::endl;
    td1.join();
    td2.join();
    std::cout << "Threads finished." << std::endl;
    return 0;
}

If you're not using C++11, you still have options. You can look into:

Share:
16,031
Adam
Author by

Adam

(my about me is currently blank)

Updated on July 09, 2022

Comments

  • Adam
    Adam almost 2 years

    This must be a question asked a lot, and yet I couldn't quite find what I'm looking for.

    Imagine this:

    • A program starts up "Hello, what's your name?"
    • You enter a number and it goes "Your name can't be a number!"

    You keep entering a number and keep getting that error, while in the background it just keeps track of how long the program has been running, by doing n++ every second, no matter what goes on in the text/input part. Eventually you could enter something like "time" and then it shows how long you've been there, in seconds...

    SO my question is: Just how the hell would you go about doing that? To have them run independently?

    Thanks in advance!

    Edit: I'm not trying to do this timing thing in particular, it's just the easiest example I could come up with to ask about running functions independently..

  • Koushik Shetty
    Koushik Shetty almost 11 years
    andy you could show the code inside the "error loop" where he could check if "time" has been entered and then display current elapsed time accordingly.
  • JBentley
    JBentley almost 11 years
    @Koushik See the OP's edit, he's not specifically interested in the example. He's asking about multithreading.
  • Koushik Shetty
    Koushik Shetty almost 11 years
    @JBentley oops missed it.sorry
  • Andy Prowl
    Andy Prowl almost 11 years
    @JBentley: Indeed, I realized that only after the OP's edit. I edited the answer.
  • Adam
    Adam almost 11 years
    Thank you for the answer - I now just have one question; What does the .join() function do?
  • Adam
    Adam almost 11 years
    Well, I hate to say this, but I only started learning C++ about a month ago. I was learning Java before that, and, well... C++ is pretty complicated compared to Java.. Maybe I should just go back
  • Andy Prowl
    Andy Prowl almost 11 years
    @Adam: Well, the above examples take less than 10 lines of code each... I am not a Java expert, so I can't make a comparison, but the above does not seem too bad to me
  • user123
    user123 almost 11 years
    It waits for a thread to finish and returns to the caller.
  • dtech
    dtech almost 11 years
    @Adam - what is so complicated about the examples above? C++ is undoubtedly the "harder" but also the more capable, performing and efficient language. You could be a perfectly proficient C++ programmer with 1/5 of the language, and still ask for help if you need something more advanced.
  • NathanielSantley
    NathanielSantley about 7 years
    @Adam, even though C++ is definitely harder than other programming languages, it is definitely still worth learning! Take the time to study it and you will definitely find that it is a lot more capable of high-end graphics, incredible efficiency, and uncompromising speed than many other programming languages. I'm pretty new to C++ too, but I can definitely say that it was one of the best decisions I've ever made to start learning it. Give it a chance and you will be rewarded!