Two functions running at once.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shing
    New Member
    • Mar 2007
    • 58

    Two functions running at once.

    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?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      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

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        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

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          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.

          Comment

          Working...