Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Two functions running at once.

Question posted by: shing (Member) on July 4th, 2008 10:13 AM
Is it possible for a C/C++ program to have 2 functions running at once? For example, the program holds a for() loop while it moves onto another function?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Banfa's Avatar
Banfa
AdministratorVoR
4,943 Posts
July 4th, 2008
10:16 AM
#2

Re: Two functions running at once.
If you mean multi-tasking then yes but this is not a feature of C or C++ but rather the platform/OS on which it is being used.

Reply
JosAH's Avatar
JosAH
Chief Editor
7,526 Posts
July 4th, 2008
10:28 AM
#3

Re: Two functions running at once.
Google for "pthread" (or "POSIX thread"). It's a multi threading library and I bet
a port also exists for MS Windows. It has everything you want.

kind regards,

Jos

Reply
Banfa's Avatar
Banfa
AdministratorVoR
4,943 Posts
July 4th, 2008
10:43 AM
#4

Re: Two functions running at once.
On Windows look up Fibers or threads. Fibers are not a fully fledged threads and have to be manually scheduled and share thread data with other fibers in the same thread. A Thread is a separate execution process automatically scheduled by the system.

Read This

Reply
weaknessforcats's Avatar
weaknessforcats
Moderator
4,656 Posts
July 5th, 2008
07:34 PM
#5

Re: Two functions running at once.
C++ cannot have two functions running at once.

All C++ functions run sequentially, one at a time.

Now that's not to say that your C++ program won't be making operating system calls to spawn additional threads, fibers, or whatever. However, even in this case there is no concurrency UNLESS your machine has multiple processors or, lately, multiple cores.

Where there is one processor, only one thread can be active at a time. The others are blocked. Your code pretends there is concurrency and you have to program as though there were concurrency but there really isn't any.

Reply
Reply
Not the answer you were looking for? Post your question . . .
184,042 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top C / C++ Forum Contributors