Domanda

I have 6 functions:

  • fetch operation
  • decode
  • fetch operands
  • execute
  • writeback
  • updatepc

Each giving input to another. I want to execute them at the same time i.e., pipelining.

How to do that?

È stato utile?

Soluzione

Create a class for each pipeline component that implements Runnable. Give each component a ConcurrentLinkedQueue to hold the data to be processed; each component will poll this queue in an infinite loop (in its run() method), processing the data as it pulls it off. Each preceding component will add its output to the next component's queue. Now assign each runnable to a thread, start the threads, and start feeding data to the first component's queue.

If a component finds that its queue is empty then you may want to put it to sleep for half a second or so.

You may also want to add a cancel() method to each component that will break out of the infinite loop in its run() method.

public class Decode implements Runnable {
   private boolean cancel = false;
   private ConcurrentLinkedQueue<Data> queue = new ConcurrentLinkedQueue<>();
   private FetchOperands nextComponent;

   public void run() {
       while(!cancel) {
           Data data = queue.poll();
           if(data != null) {
               ...
               nextComponent.enqueue(data);
           } else (Thread.sleep(500);
       }
   }

   public void enqueue(Data data) {
       queue.offer(data);
   }

   public void cancel() {
       cancel = true;
   }

   public void setFetchOperands(FetchOperands nextComponent) {
       this.nextComponent = nextComponent;
   }
}

public class Main implements Runnable {
    public void run() {
        Decode decode = new Decode();
        FetchOperands fetchOperands = new FetchOperands();
        decode.setFetchOperands(fetchOperands);
        Thread t1 = new Thread(decode);
        Thread t2 = new Thread(fetchOperands);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}

Altri suggerimenti

Pipeline Pattern

Pipeline Pattern is helpful in dividing the problem into smaller reusable code components. This is a simple yet powerful structural pattern to organize a complex logic into smaller reusable components, which can be added/removed/modified independently.

To improve upon @Zim-Zam O'Pootertoot's answer, instead of having your component sleep for a little when there is nothing to process, you could have it wait(), and then call notify() whenever you pass it something to process.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top