使用 pthread_yield() 从 Rhealstone 基准测试得出的任务切换时间

发布于 2024-11-08 16:55:50 字数 320 浏览 5 评论 0原文

我正在尝试在 Linux 中实现任务切换 Rhealstone 基准测试。这是原始代码: http://pastebin.com/aYF4Tnvt

这是我到目前为止所写的内容: http://pastebin.com/tX7zK7h7

问题是 for 循环随机运行几次,并且程序退出。任何人都可以指出我正确的方向吗?我是一个菜鸟程序员,我真的对这些东西迷失了。

I'm trying to implement the task switch Rhealstone benchmark in Linux. Here is the original code:
http://pastebin.com/aYF4Tnvt

And here is what I've written so far:
http://pastebin.com/tX7zK7h7

The problem is that the for loops run for a random few times and the program quits. Anyone can point me to the right direction? I'm a noob programmer and I'm really lost with this stuff.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2024-11-15 16:55:50

join() main() 中的线程。您正在退出主程序并退出程序,这会杀死其他线程。将代码更改为类似

clock_gettime(CLOCK_REALTIME, &start_time);

pthread_t threadID[2];

pthread_create(&threadID[0], NULL, task1, NULL);

pthread_create(&threadID[1], NULL, task2, NULL);  

for (int i = 0; i < 2; ++i)
    pthread_join(threadID[i], NULL);

clock_gettime(CLOCK_REALTIME, &end_time);

//... the rest

不确定要将结束计时器放置在何处。通过这种方式,您还可以对 join() 的两次调用进行计时,但它们应该是最少的。如果您将计时器放在连接之前,那么您将在线程实际结束之前结束计时器 - 您将计时的是两个 create_thread 调用完成所需的时间。

join() the threads in main(). You are falling out of main and exiting your program which kills the other threads. Change the code to something like

clock_gettime(CLOCK_REALTIME, &start_time);

pthread_t threadID[2];

pthread_create(&threadID[0], NULL, task1, NULL);

pthread_create(&threadID[1], NULL, task2, NULL);  

for (int i = 0; i < 2; ++i)
    pthread_join(threadID[i], NULL);

clock_gettime(CLOCK_REALTIME, &end_time);

//... the rest

Not sure exactly where you want to put that end timer. In this manner you would also be timing the two calls to join() but they should be minimal. If you put the timer before the join then you will be ending your timer way before your threads actually end - all you will be timing is how long it takes to for two create_thread calls to complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文