I'm building a pipeline cycle with threads. Cycles are Fetch, Decode, Execute, and Write Back. I coded a GUI button which triggers each cycle. The problem I'm having is that the output is all jumbled up when I want it to be in a specific order (F->D->X->W). In addition, I want these threads to run concurrently meaning I can't use locks with monitors. From what I know about monitors and locks, they make it so one thread runs at a time and that won't work for me. Here is a simple outline of my code:
public void nextInstruction()
{
// These all trigger at once
// and run in parallel
fetchEvent.Set();
decodeEvent.Set();
executeEvent.Set();
wbEvent.Set();
}
public void fetch()
{
fetchEvent.waitone();
// do stuff...
}
public void decode()
{
decodEvent.waitone();
// do stuff...
}
public void execute()
{
exeEvent.waitone();
// do stuff...
}
public void writeBack()
{
wbEvent.waitone();
// do stuff...
}
and current output:
F D X W
F lda $50
F sta $2
D lda #$10
F lda #$0
D sta $1
X sta $0
D lda $50
W sta $0
X lda #$10
F sta $0
F lda #$10
D lda $50
X sta $1
W sta $0
F sta $1
W sta $0
X lda $50
D sta $2
D lda #$0
X lda $50
W sta $0
my desired output:
F D X W
F lda $10
F sta $0
D lda #$10
F lda #$11
D sta $0
X lda #$10
F sta $0
D lda $11
X sta $0
W lda #$10
F lda #$10
D sta $0
X lda $11
W sta $0
F sta $1
D lda #$10
X sta $0
W lda $11
D sta $1
X lda #$10
W sta $0
I have my print statements formatted this way. Makes it easier for me to see the ordering. Again each of these print statements--F, D, X, W--are each triggered by a distinct thread. Any input on how to achieve this?
I'm also open to using locks if there is a way to use them I'm unaware of.