同一个线程,多个线程和多个进程

tequila_wx 2007-09-27 07:13:44
Class A generates random integers between -100 and 100 at a random frequency between 0 and 2 seconds. Class B decrements a counter when class A generated a negative integer and increments when class A generated a positive integer. Class B's counter needs to be updated in real time as class A generates numbers.
a) Show pseudo code in 3 cases: 1) A and B are in the same thread, 2) different threads, c) different processes.
b) Bonus: Show different ways to handling each case.
麻烦高手帮解答
...全文
123 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jim_King_2000 2007-09-27
  • 打赏
  • 举报
回复
c) different processes.
把B::handle写成COM/ActiveX
Jim_King_2000 2007-09-27
  • 打赏
  • 举报
回复
2) different threads
在B::handle里面加critical segment.
Jim_King_2000 2007-09-27
  • 打赏
  • 举报
回复
1) A and B are in the same thread
class A
{
public:
A(B &b) : b_(b) {}

private:
void OnRandomGen()
{
...
b.handle(random());
...
}

private:
B &b_;
}

class B
{
public:
void handle(int n);
}

main()
{
B b;
A a(b);
}

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧