Jump to content

How to simulate thread interaction correctly


YYF

Recommended Posts

Hi,

Fellow experts, I'm having a problem with thread interaction. I design a model where thread_2 needs to wait for thread_1 to finish executing logic, and thread_1 needs to wait for thread_2 to finish executing logic before it can start executing.
I use the method of flag(wait_t_1 & wait_t_2) + wait(), but it seems that this method is a little silly, please tell me the correct design method, provided the code is as follows:

 

SC_MODULE(test)
{
  int a,b;
  
  bool wait_t_1 = true;
  bool wait_t_2 = false;
  
  void thread_1();
  void thread_2();

  SC_CTOR(test):
  {
    SC_CTHEAD(thread_1,clk.pos());
    SC_CTHEAD(thread_2,clk.pos());
  }
}

//-----------------------------------

void test::thread_1(){
  while(true){
    while(!wait_t_1) wait();
    
    for (int i = 0; i < 10000; i++) a++;
    
    wait_t_1 = false;
    wait_t_2 = true;
  }
}

void test::thread_2(){
  while(true){
    while(!wait_t_2) wait();
    
    for (int i = 0; i < 10000; i++) b++;
    
    wait_t_2 = false;
    wait_t_1 = true;
  }
}

 

Link to comment
Share on other sites

The prblem is the use of plain C datatypes for communication. Although SystemC simulates parallel exeution everything is executed in a sequential manner. When clk has a rising edge thread_1 as well as thread_2 have to be executed in the evalauation phase an the SystemC kernel decides in which order. Now if thread_1 executes first the value change of wait_t_2 is immediatley seen in thread_1 (and vice versa).

Therefore you need an element which defers the value update until the next evaluation phase. This element is a signal....

So you sould use sc_core::signal<bool> instead of a bool.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...