将 pthread 作为输入并将其挂起的函数 [英] Function which takes a pthread as input and suspends it

查看:56
本文介绍了将 pthread 作为输入并将其挂起的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 POSIX 中的 ExpressLogic 移植实时 Thread_Metric,以便为我的论文对 Linux、Xenomai 和 RTAI 的 PREEMPT_RT 补丁进行基准测试.它们提供了一个包含以下函数的 C 源文件,您必须实现这些函数才能使基准测试正常工作:

I'm trying to port the real time Thread_Metric from ExpressLogic in POSIX, in order to benchmark PREEMPT_RT patches for Linux, Xenomai and RTAI for my thesis. They provide a C source file with the following functions, which you have to implement in order for the benchmark to work:

void   tm_initialize(void (*test_initialization_function)(void));
int    tm_thread_create(int thread_id, int priority, void (*entry_function)(void));
int    tm_thread_resume(int thread_id);
int    tm_thread_suspend(int thread_id);
void   tm_thread_relinquish(void);
void   tm_thread_sleep(int seconds);
int    tm_queue_create(int queue_id);
int    tm_queue_send(int queue_id, unsigned long *message_ptr);
int    tm_queue_receive(int queue_id, unsigned long *message_ptr);
int    tm_semaphore_create(int semaphore_id);
int    tm_semaphore_get(int semaphore_id);
int    tm_semaphore_put(int semaphore_id);
int    tm_memory_pool_create(int pool_id);
int    tm_memory_pool_allocate(int pool_id, unsigned char **memory_ptr);
int    tm_memory_pool_deallocate(int pool_id, unsigned char *memory_ptr);

现在我正在尝试实现 tm_thread_suspend 和 tm_thread_resume 函数,它们以 pthread 作为输入.我知道您可以使用 pthread_mutex_lock 和 pthread_cond_wait 例程挂起 pthread,但是您必须从线程 start_function 中调用它们.我是这种东西的新手,而且我已经不知所措了.任何帮助表示赞赏.

Right now I'm trying to implement the tm_thread_suspend and tm_thread_resume functions, which take a pthread as input. I know that you can suspend a pthread with pthread_mutex_lock and pthread_cond_wait routines, but you have to call these from the thread start_function. I'm new at this kind of stuff and I'm way in over my head. Any help is appreciated.

推荐答案

pthread_suspend 似乎真的是可行的方法.这个解决方案可能比它的价值更脏.为每个线程保留一个 semaphore.让每个线程监听信号.在信号处理程序中,只需对关联的信号量执行 down.

pthread_suspend really seems the way to go if it's available. This solution might be more dirty than it's worth. For each thread keep a semaphore. Make each thread listen for signals. In the signal handler simply do a down on the associated semaphore.

因此,当您想要停止一个线程时,只需向它发送一个信号(也许最好使用实时信号),它就会在处理程序中停止.默认情况下,处理程序会屏蔽自身(即,如果在完成之前接收到相同的信号,则不会再次调用它).当您想重新启动线程时,只需在信号量上执行 up.

So when you want to stop a thread, simply send it a signal (perhaps using a real-time signal would be best) and it will stop in the handler. The handler masks itself by default (that is, it won't be called again if the same signal is received before it is done). When you want to restart the thread, simply do an up on the semaphore.

警告:

  • 使用@bmargulies 的建议,如果可以的话,它会更好(更安全、更清洁、更高效)
  • 您也可以使用互斥锁代替信号量
  • 处理信号是一项令人讨厌的工作.在开始之前,请确保您(重新)阅读
    • 异步信号安全
    • 线程和信号
    • 中断和重新启动系统调用 (SA_RESTART)

    不幸的是,我找不到任何关于此的真正出色的免费文档.不管怎样,还是有很多免费文档.

    Unfortunately I can't find any really stellar free documentation about this. Still, stellar or not, there is a lot of free documentation.

    编辑

    正如@R 所建议的那样,有 async-signal-safe 函数可以阻止您使用(而不是不安全的 sem_wait).这是您可以安全使用的功能列表.

    As suggested by @R.. there are async-signal-safe functions that block that you could use (instead of the unsafe sem_wait). Here's a list of functions you may safely use.

    这篇关于将 pthread 作为输入并将其挂起的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆