Fork join for req-ack in loop

Below is my code for NUMLINES=4. If pkt_send_req comes on any of the requests, pkt_send_ack[i] must be asserted for once cycle within a delay of 1-32 cycles. I am not seeing the expected behavious. Total number of acks are not matching total number of reqs. Can you please correct this?

task foo_driver::drive_ack();
 int req_pos;
 int ack_pos;
 int pos;
 int delay_ack;
 int req_pos_q[$];
 int ack_q[$];
 semaphore mutex = new(1); // Create a semaphore with 1 token


 forever begin 
   @(posedge intf.clock);
   
	  fork
	   begin: requests
		  if(|intf.pkt_send_req ==1) begin
        val = intf.o_val;
        for (int i = 0; i < `NUM_LINES; i++) begin
            if (intf.pkt_send_req[i] == 1) begin
							mutex.get(); // Acquire the mutex
                req_pos_q.push_back(i);
							mutex.put(); // Release the mutex
            end
        end
			end	
		  else begin
				val = 0;
			end
		 end
		 begin: acks
        while (req_pos_q.size() != 0) begin
							mutex.get(); // Acquire the mutex
            		ack_pos = req_pos_q.pop_front();
						    ack_q.push_back(ack_pos);
							mutex.put(); // Release the mutex
            delay_ack = $urandom_range(32, 1);
            repeat (delay_ack) @(posedge intf.clock);
						pos=ack_q.pop_front();
            intf.pkt_send_ack[pos] <= 1'b1;
            @(posedge intf.clock);
            intf.pkt_send_ack[pos] <= 1'b0;
            @(posedge intf.clock);
        end
		 end
		join_none
   
end
endtask

Since you don’t have all the code shown, it’s hard to debug this. We can’t see the results you are getting. The while (req_pos_q.size() != 0) loop is in a race with the for loop in the request process. Also, it would be much simpler to use a mailbox#(int) req_pos_q instead of a queue wrapped around semaphores.

Can you please edit my above code to use mailbox instead?

The below code errors out randomly. It still does not generate equal number of acks for the reqs. Please help me fix this code.

task foo_driver::drive_ack();
 int req_pos;
 int ack_pos;
 int pos;
 int delay_ack;
mailbox req_mb = new();
mailbox ack_mb = new();



 forever begin 
   @(posedge intf.clock);
   
	  fork
	   begin: requests
		  if(|intf.pkt_send_req ==1) begin
                     val = intf.o_val;
                     for (int i = 0; i < `NUM_LINES; i++) begin
			automatic int k = i;
                         if (pkt_send_req[k] == 1) begin
            			  req_pos_mb.put(k);
				$display("req is here with k=%0d",k);
				$display("req size is %0d",req_pos_mb.num());
                        end
		   end	
		else begin
			val = 0;
		end
	  end
       begin:acks
          if(req_pos_mb.num()>0) begin
	        req_pos_mb.get(req_pos); // Get the request position from the mailbox
                ack_mb.put(req_pos); 
			
            delay_ack = $urandom_range(32, 1);
            repeat (delay_ack) @(posedge intf.clock);
		ack_mb.get(req_pos);
		 intf.pkt_send_ack[req_pos] <= 1'b1;
                @(posedge intf.clock);
                     intf.pkt_send_ack[req_pos] <= 1'b0;
                @(posedge intf.clock);
           end
       end
     join_none
   
end
endtask

Still very difficult to debug without more code shown. What outputs and errors are you getting?