How to have two separate things simutaniously compute?

I noticed the while statement can infinitely continue, but what if I want two of them going at the same time but separately coded?

If c++ code sequence-ly does 1 task at a time by integers then how can it record every image at the time of retrieval while also doing something else constantly? Basically, how do I make two separate code piece *both at the same time constantly compute their own thing!?
Last edited on
std::async with std::launch::async
See http://en.cppreference.com/w/cpp/thread/async (there is an example at the end)
So yes then?............Proof would be I could make the output screen box show 2 lists of numbers continuously being written forever, like this below: (but one can't stop for the other, it's gotta be like you place a working camera in my liver and another in my brain, both compute at same time is needed)

generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2
generated number ---SPACE--- generated number2

Oh and what if I want 100 of them? Hope 4 memory sticks/board paths doesn't mean it can't gulp!
Last edited on
Not sure what your question is, but, you can make use of std::promise and std::future.
http://www.cplusplus.com/reference/future/promise/

for example one thread gives promise to another thread that it won't continue unless another thread finished current batch of work.

Also you might want to use either std::atomic or std::mutex to lock std::cout or whatever else to ensure 2 threads do not access shared variable in same time.
http://www.cplusplus.com/reference/atomic/atomic/atomic/
http://www.cplusplus.com/reference/mutex/mutex/
What I mean is picture a ant, on your screen, as big as a dot, it follows int main() to the next part that is computed until it gets to the end of the trail, got it so far? What I am asking for (which may even need), is if I can make 100 ants follow their own line and compute their own at the same exact time, no breaks for anyone of them - no break tricks. Can it do this?
> if I can make 100 ants follow their own line and compute their own at the same exact time,
> no breaks for anyone of them - no break tricks. Can it do this?

Yes, if 100 or more processors are available on the machine for your program to use,
and the library implementation can exploit all 100 of them.
(Ideally, std::thread::hardware_concurrency is greater than 99.)
What I am asking for (which may even need), is if I can make 100 ants follow their own line and compute their own at the same exact time, no breaks for anyone of them - no break tricks.


Hi,

I am just wondering why you might need all this concurrency? If one had 100 ants and processed them sequentially, it might only take an entire millisecond to do the co-ordinate type math for all of them, depending on how complex the processing was. It's possible that you may not need any concurrency for that type of scenario.

Your question has changed quite a bit: first two things at once, then imagery from multiple cameras, now 100 ants. So what are you trying to do exactly? I suspect you won't need to do anything as complex as modelling the weather with a supercomputer.

I imagine there might be various scenarios where the work is divided between threads, but not 1 thread per ant. For example some of the ants are calculated in one thread, then another another thread draws them on the screen, meanwhile more ants are calculated in the first thread, and so on.

Also, there are various design patterns that might help with some aspects of a problem.

God Luck !!

Topic archived. No new replies allowed.