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.
> 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:
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.
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.