The meaning of parallel is not that of parallel processing. I simply mean that if algorithms A and B are represented by the two sequence of operations (a_1, a_2, ...) and (b_1, b_2, ...), then the executed sequence of operations is (a_1, b_1, a_2, b_2, ...). It is not compulsory to be strictly 1 operation from A followed by 1 operation of B. This number may vary. Nonetheless, this should work on systems with only one thread of execution (without built-in parallelism).
The "merged" algorithm that is derived should terminate its execution when either A or B terminates.

The two functions A and B share all their input data, but they wont perform any write operation upon them.

How could I implement it in C++?

Dani AI

Generated

For : this is basically cooperative multitasking (user-level context switching) rather than real parallel threads. already pointed at the same idea; touched on picking which step to run first. A clean, portable way in C++ is to break A and B into tiny "step" units and run a simple scheduler that calls one step at a time (or a small budget of steps) from each algorithm and stops as soon as either reports it has finished.

Example pattern (works in any modern C++):

// task interface: do a tiny unit of work and return true when done
struct Task {
    virtual bool step() = 0;    // perform one micro-step; return true if finished
    virtual ~Task() = default;
};

// scheduler: round-robin, stop when either finishes
void run_until_any_done(Task &A, Task &B) {
    while (true) {
        if (A.step()) break;  // A finished -> stop merged algorithm
        if (B.step()) break;  // B finished -> stop merged algorithm
    }
}

// variant: give each task a small budget of steps before switching
void run_with_budgets(Task &A, Task &B, int aBudget = 1, int bBudget = 1) {
    while (true) {
        for (int i = 0; i < aBudget; ++i) if (A.step()) return;
        for (int i = 0; i < bBudget; ++i) if (B.step()) return;
    }
}

Notes and troubleshooting

  • Make each step() short and deterministic so one task cannot hog the loop. If a natural micro-step doesn’t exist, refactor the algorithm into an explicit state machine (index/counters + switch).
  • Since inputs are read-only you don’t need locks; if you later write shared data, protect it or serialize writes inside the scheduler.
  • If you need nicer syntax, use stackless coroutines (C++20 co_yield) or Boost.Coroutine / platform fibers for stackful switching — they make code cleaner but add complexity/portability concerns.
  • If a task never yields (infinite loop), add timeouts or watchdog checks in step() to avoid starvation.

This pattern gives deterministic interleaving and guarantees the merged run stops as soon as either A or B finishes.

Recommended Answers

All 3 Replies

I implemented something like that (I think) many years ago with MS-DOS 6.X. It was a series of functions that performed context stack switching so that there could be any number of "threads". Unlike modern-day MS-Windows or *nix, the program would switch contexts only when requested by the currently running thread. So instead of running threads like you described -- A_1 B_1 A-2 B_2 etc. it might run something like A-1 A-2 A-3 A-4 B-1 B-2 A-5 A-6 B-3 B-4 B-5 etc.

Well are the operations in your code. represented by a_1 , a_2, Different functions alltogether and is there anyway that we could represent a particular sub-process with a time constraint. If So you could always, Try a Shortest Time Notation which compares 2 stacks and looks to see which operation could be performed as the fastest.

However it might be, its quite hard for me as a beginer in the language to understand the Paralleled Dynamism as I mostly go through Sequential Programming.

What I need is not that complex, but I don't know the way to implement it. In other words, I would like to emulate the parallel execution with two threads in one. How to do it?

Note: The solution of executing the function A, and then function B doesn't work. I want them to run concurrently in one thread.
I hope it doesn't sound absurd. The reason for such a need is that such a "parallelism" will ensure the termination of combined algorithm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.