2

在阅读 SystemC 中的线程时,据说while(true)必须在函数内部使用循环。为什么会这样?

您能否查看下面给出的示例代码并解释为什么 while 循环用于线程并且wait()命令与循环一起使用:

 1 //-----------------------------------------------------
  2 // This is my second Systemc Example
  3 // Design Name : first_counter
  4 // File Name : first_counter.cpp
  5 // Function : This is a 4 bit up-counter with
  6 // Synchronous active high reset and
  7 // with active high enable signal
  8 //-----------------------------------------------------
  9 #include "systemc.h"
 10 
 11 SC_MODULE (first_counter) {
 12   sc_in_clk     clock ;      // Clock input of the design
 13   sc_in<bool>   reset ;      // active high, synchronous Reset input
 14   sc_in<bool>   enable;      // Active high enable signal for counter
 15   sc_out<sc_uint<4> > counter_out; // 4 bit vector output of the counter
 16 
 17   //------------Local Variables Here---------------------
 18   sc_uint<4>    count;
 19 
 20   //------------Code Starts Here-------------------------
 21   // Below function implements actual counter logic
 22   void incr_count () {
 23     // For threads, we need to have while true loop
 24     while (true) { 
 25       // Wait for the event in sensitivity list to occure
 26       // In this example - positive edge of clock
 27       wait();
 28       if (reset.read() == 1) {
 29         count =  0;
 30         counter_out.write(count);
 31       // If enable is active, then we increment the counter
 32       } else if (enable.read() == 1) {
 33         count = count + 1;
 34         counter_out.write(count);
 35       }
 36     }
 37   } // End of function incr_count
 38 
 39   // Below functions prints value of count when ever it changes
 40   void print_count () {
 41     while (true) {
 42       wait();
 43       cout<<"@" << sc_time_stamp() <<
 44         " :: Counter Value "<<counter_out.read()<<endl;
 45     }
 46   }
 47 
 48   // Constructor for the counter
 49   // Since this counter is a positive edge trigged one,
 50   // We trigger the below block with respect to positive
 51   // edge of the clock
 52   SC_CTOR(first_counter) {
 53     // Edge sensitive to clock
 54     SC_THREAD(incr_count);
 55     sensitive << clock.pos();
 56     // Level Sensitive to change in counter output
 57     SC_THREAD(print_count);
 58     sensitive << counter_out;
 59   } // End of Constructor
 60 
 61 }; // End of Module counter
4

2 回答 2

3

SC_THREAD或者SC_CTHREAD应该有无限循环以保持线程不被终止。如果在函数体中不放for(;;)or while(true),当执行到函数作用域结束时,线程终止。在这种情况下,您的线程将永远不会被敏感列表唤醒以进行处理。或者您可以将其转换为等效项SC_METHOD,则您可以没有无限循环。

SystemC 正在使用非抢占式线程,因此如果您不使用wait()等待静态或动态敏感列表中列出的某些事情发生,它会导致您的线程无限执行。并且您的进程的 CPU 执行不会超出函数范围。这样 SystemC 内核将无法继续处理其他方法/线程和事件以继续模拟。而在基于事件的模拟中,线程应该只在指定条件(敏感列表中的事件)发生时才运行。因此该wait()函数可以等到下一个事件发生并将 CPU 执行交给其他线程/方法和 SystemC 内核。下次事件发生时,例如时钟的上升沿,wait()语句将返回并继续您的过程。

在您的示例中,您的程序正在等待时钟的上升沿触发以将计数器的值增加 1。因此,如果您不wait()输入代码,那么incr_count无论时钟的状态如何,计数器的值都会无限增加,并且它赢了不要停下来。通过使用wait(),您的线程将被阻塞并等待下一个时钟上升沿触发。

于 2013-07-29T01:04:57.403 回答
0

while(true) --> 使线程无限,因此它可以在获得上下文时继续执行
wait() --> 强制线程丢失上下文并允许执行其他操作

于 2013-07-30T06:25:37.550 回答