Two Paths

This is a quite nooby question, but is there any way to have the program control go sone two paths at once? For example: if my program has two functions, one that does some simple calculation that requires nothing from outside it to set the variables and another that displays text for the user to read then enter something. Is there any way to have both run at the same time?
1
2
3
4
5
6
7
8
9
10
11
12
simpleFnc()
{int a = 1;
int b = 2 ;
int c = a+b ;
}

complexFnc()
string secrets ;
{cout << "Enter your secrets to get magic powers: " ;
cin >> secrets ;
//blah blah blah
}

There is no connection of simplefnc to anywhere else in the program so is there any way to get it to run at the same time the other function is running?
Yes*, but you are asking the wrong question.

Computers are fast enough that you don't need to actually do more than one thing at once to make the user think you are.


*Well, no, actually. Unless you are using multiple processors. Like supercomputers do. And video games.
Hi jazpen.

Your question isnt nooby at all, trust me!
I believe that you may want to look at multithreading, google will possibly extend your knowledge on that.
Multithreading allows you to run two or more pieces of code at once, its really good for multicore systems ( which are reasonably common these days ) for increases in performance.

Heres some things to get you started:
http://en.wikipedia.org/wiki/Multithreading

I think it also depends on what operating system you are running on, if you are running on windows, you can use some thread stuff from <windows.h>:

http://www.bogotobogo.com/cplusplus/multithreaded2A.php
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682516(v=vs.85).aspx

For linux, I think you have to use POSIX threads

http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
http://www.bogotobogo.com/cplusplus/multithreading_pthread.php


Sorry if im not too much help, ive only worked in pure C and windows as of this point, But i hope this helps!


Last edited on
Seriously, though, unless you are doing some heavy processing, you ought to rethink your algorithm if you think you need multitasking.
Not to mention how amazingly complex things become once you start to modify program state in more than one place at once. Unless you're very careful and disciplined (or you figure out how to write self-locking objects), it's not very difficult to bury yourself in bugs that are extremely hard to reproduce, find, and fix.
Last edited on
Topic archived. No new replies allowed.