Run two things at the same time? thread/parallel/flow

Hello! I am new to programming and have some quetion:

if I want two simple counters counting from 0 to 100 and back from 100 to 0 continously at two different speed while there is something else running at the same time, do I have to use thread to do that? What is the best way to have more than one thing running at the same time?

I did some search about the thread, some people say it will harm the performance if you have many(depends on your cpu core) threads, but I think it is very normal for a software running more than one stuff at the same time, and most of them won't affect performance at all, so I guess there must be a better way to do that.


Thanks very much!

> if I want two simple counters counting from 0 to 100 and back from 100 to 0 continously at two different speed
> while there is something else running at the same time, do I have to use thread to do that?

The template function async runs the function f asynchronously (potentially in a separate thread which may be part of a thread pool) and returns a std::future that will eventually hold the result of that function call. http://en.cppreference.com/w/cpp/thread/async


> some people say it will harm the performance if you have many(depends on your cpu core) threads

If you have many threads (more than the number of available processors) which are in a ready state (ready to run, competing with each other for processor time). Kernel (scheduler) support for thread-pools addresses this particular problem; a C++ implementation could use this facility. For instance, the Microsoft library implementation of std::async() exploits kernel thread pools (to the extent possible under strict conformance with C++11.)

A trivialised example of using asynchronous counters:

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
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <future>
#include <mutex>
#include <thread>
#include <chrono>

std::mutex cout_lock ; // http://en.cppreference.com/w/cpp/thread/mutex

void counter( int from, int to, int interval_in_ms, char id )
{
    // http://en.cppreference.com/w/cpp/chrono/duration
    const auto interval = std::chrono::milliseconds(interval_in_ms) ;

    for( int i = from ; i < to ; ++i )
    {
        // http://en.cppreference.com/w/cpp/thread/sleep_for
        std::this_thread::sleep_for(interval) ;

        // http://en.cppreference.com/w/cpp/thread/lock_guard
        std::lock_guard<std::mutex> guard(cout_lock) ;

        std::cout << '+' << id << std::flush ;
    }

    for( int i = to ; i > from ; --i )
    {
        std::this_thread::sleep_for(interval) ;
        std::lock_guard<std::mutex> guard(cout_lock) ;
        std::cout << '-' << id << std::flush ;
    }
}

int main()
{
    // http://en.cppreference.com/w/cpp/thread/async
    // http://en.cppreference.com/w/cpp/thread/future
    auto fa = std::async( std::launch::async, counter, 0, 40, 100, 'a' ) ;
    auto fb = std::async( std::launch::async, counter, 0, 80, 50, 'b' ) ;

    for( int i = 0 ; i < 10 ; ++i )
    {
        std::this_thread::sleep_for( std::chrono::milliseconds(800) ) ;
        std::lock_guard<std::mutex> guard(cout_lock) ;
        std::cout << "(main)\n" << std::flush ;
    }
    // wait for destructors of fb, fa
}

http://coliru.stacked-crooked.com/a/5a342759d045b6d6
if I want two simple counters counting from 0 to 100 and back from 100 to 0 continously at two different speed while there is something else running at the same time

Why have the counters at all? If you know when they started, the current time, and the rate at which they should count, then you can compute the value. This method would probably be cheaper and easier than actually incrementing a counter.
thank you JLBorges for the explanation and the code, I think async is what I am looking for.
Hi, dhayden, I need the counters because I want to build a music software which needs modulation source, by using the counter, I can modulate a variable to change from 0 to 127 and 127 to 0 at a certain frequency in real time.
Topic archived. No new replies allowed.