Learn SystemC - Concurrency

本文介绍了SystemC如何通过仿真过程模型化并发操作,尽管在多线程情况下,实际执行时只有一个线程运行。它强调了与Go等语言中真正的并行性不同,SystemC的并发是基于仿真时间的并行。通过实例,展示了两个线程在仿真时间上的交替执行。
摘要由CSDN通过智能技术生成

本文为英文翻译,原文地址:Concurrency

Concurrency

SystemC使用仿真过程建模并发操作,并非真实的并发运行。
当有多个仿真过程进行并发仿真,在某一时刻只有一个仿真过程在运行。
当然,仿真时间在所有的仿真过程完成当前任务前不会变化,也就是可以认为:这些仿真过程在相同的仿真时间是并行运行的。这一点和一些其他编程语言不同,例如Go等,可以真正并行操作。

下面看下示例以理解仿真并行:

// Learn with Examples, 2020, MIT license
#include <systemc>
using namespace sc_core;

SC_MODULE(CONCURRENCY) {
  SC_CTOR(CONCURRENCY) { // constructor
    SC_THREAD(thread1); // register thread1
    SC_THREAD(thread2); // register thread2
  }
  void thread1() {
    while(true) { // infinite loop
      std::cout << sc_time_stamp() << ": thread1" << std::endl;
      wait(2, SC_SEC); // 2秒(仿真时间)后再次触发
    }
  }
  void thread2() {
    while(true) {
      std::cout << "\t" << sc_time_stamp() << ": thread2" << std::endl;
      wait(3, SC_SEC); // 3秒(仿真时间)后再次触发
    }
  }
};

int sc_main(int, char*[]) {
  CONCURRENCY concur("concur"); // define an object
  sc_start(10, SC_SEC); // run simulation for 10 seconds
  return 0;
}
0 s: thread1 # thread1 is running
        0 s: thread2 # thread2 is running
2 s: thread1 # thread1 is running
        3 s: thread2 # thread2 is running
4 s: thread1 # thread1 is running
        6 s: thread2 # thread2 is running
6 s: thread1 # thread1 is running
8 s: thread1 # thread1 is running
        9 s: thread2 # thread2 is running
# simulation ends after 10 simulated seconds
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值