2013-07-27 93 views
0

在讀取systemc中的線程時,據說while(true)循環必須在函數內部使用。爲什麼這樣?線程和系統C中的時鐘線程

能否請你看下面給出的示例代碼,並解釋爲什麼while循環用於線程和等待()命令與循環一起使用:

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 

回答

1

SC_THREAD或SC_CTHREAD應該有無限循環保持線程不被終止。如果不在函數體中放入for(;;)while(true),則當執行到達函數作用域的末尾時,該線程被終止。在這種情況下,你的線程永遠不會被敏感列表喚醒以處理某些事情。或者你可以轉換成等效的SC_METHOD,那麼你可以沒有無限循環。 SystemC正在使用非搶先式線程,因此如果您不使用wait()來等待靜態或動態敏感列表中發生的事情,它會導致線程中的無限執行。你的進程的CPU執行不會超出函數範圍。這樣SystemC內核將無法繼續處理其他方法/線程和事件以繼續進行仿真。而在基於事件的模擬中,線程只應在指定條件(敏感列表中的事件)發生時運行。所以wait()函數可以監視下一個事件發生並將CPU執行交給其他線程/方法和SystemC內核。下一次事件發生時,例如時鐘的上升沿,則wait()語句將返回並繼續您的過程。 在你的例子中,你的程序正在等待時鐘的正向邊沿觸發,使計數器的值增加1.因此,如果你沒有在你的代碼中放入wait(),那麼無論時鐘的狀態如何,incr_count都會無限增加計數器的值,它不會停止。通過使用wait(),您的線程將被阻塞,並等待下一個時鐘正邊沿觸發。

0

,而(真) - >線程使無限,以便它可以繼續執行時,它得到的上下文
等待() - >強制線程失去上下文並允許別的東西要執行