Is multi-threaded programs just programs with multiple functions

Hi,

Cannot fully wrap my mind around multi-threading in C++. All the examples I see just look like the same programs I've been writing just that main() gives control to multiple functions. So are multi-threaded programs just programs with multiple functions? (for instance, the functions are the threads?)

Kind of like :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include< Libraries that are needed >

using .... std;

void function1 ( parameter )
{
     // Do Something
}

void function2 ( parameter )
{
     // Do Something
}

int main ( [], ** )
{

function1( parameter);
function2( parameter);

}



... Or do I have the wrong idea about this?

Any advice or clarification would be greatly appreciated.
closed account (Dy7SLyTq)
no thats not the right idea at all. if my understanding is correct, there are multiple ways of achieving/emulating it, but this is the one i know of: so lets say for some weird reason your code compiled. it would handle one instruction at a time. ie it would call function1 then function2 and then return an int. that is called single threaded. if it was multi threaded (and assuming you had each function call in a different thread) then it would execute them at the same time. its a bit more complicated than that, but i dont understand it enough to go into the theories behind it
In the code you have there, main would give control to function 1, function 1 would do stuff, return control to main, then main would give control to function 2, function 2 would do stuff, then return control to main.

Now, what I'm about to say is not technically accurate, but it serves as a good illustration. In a multithreaded example with... let's say two additional threads, main might split its control into three, give function 1 and function 2 pieces of control which will fade away when they're done, then (if the programmer knows what s/he is doing) wait until function 1 and function 2 are done executing before continuing.

Multithreaded programming is like a can of worms. It's a whole ton of fun and can lead to awesome things if you know what you're doing, but otherwise will just make a horrible mess of everything that's an utter pain to clean up.

-Albatross
Last edited on
Thanks guys for clarifying that.

Can I use the #include <thread> library to split control?
Yes, std::threads are indeed in <thread>. But, remember what I said about the worms. Threads can be an absolute pain if you're even slightly unsure of what you're doing. >_>

-Albatross
Last edited on
closed account (9wqjE3v7)
You probably already know this, though it is also notable that threads within the same process share the same virtual address space, that is, each thread can access the same global variables.

if it was multi threaded (and assuming you had each function call in a different thread) then it would execute them at the same time.


That is only assuming you have multiple processors. Processor time is usually shared and alternated between threads for single core processors, to an extent where it seems as if execution is concurrent, when in reality it is not.
Topic archived. No new replies allowed.