Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

建议将event的现有实现稍微增强一下 #248

Closed
duyanning opened this issue Nov 5, 2023 · 4 comments
Closed

建议将event的现有实现稍微增强一下 #248

duyanning opened this issue Nov 5, 2023 · 4 comments

Comments

@duyanning
Copy link
Contributor

你在什么场景下需要该功能?

event的典型用法是这样的:
若干线程在某个event上等待,
一个线程设置这个event,
所有等待的线程都被唤醒。

哪怕是过一会又有线程来等待这个event,也会顺利通过。

目前的event其实就是个Semaphore。post唤醒一个等待的线程后,其他线程依然在等待。
这导致无法满足上述典型用法。

描述可能的解决方案

新增类型 tb_event_t
其中含有一个成员 tb_bool_t state

tb_event_init() 将state初始化为 tb_false

函数 tb_event_post 改名为 tb_event_set(), 在执行 tb_semaphore_post() 前,将state设置为 tb_true 。

修改函数 tb_event_wait() ,在 tb_semaphore_wait 之后,检查 state 的值,如果为tb_true,立即自动执行 tb_semaphore_post(),使得底层的信号量始终值为1

新增函数 tb_event_reset() 将state设置为 tb_false

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Title: It is recommended to slightly enhance the existing implementation of event

@waruqi
Copy link
Member

waruqi commented Nov 5, 2023

你这个就是 win event 的手动重置,目前的自动重置 还是 手动重置,都是常规用法,适合不同的用户需求,不可能为了改成手动重置,我再去改接口的内部行为,还去改接口,这会 break 一堆现有用户,然后再来一堆其他用户反馈 应该改成自动重置模式。

另外,你现在的需求,直接用 semaphore 就完事了,也不会多几行代码,没必要非得改 event 。而且我也懒得改,你可以自己基于 semaphore 稍微封装下。

@waruqi waruqi closed this as completed Nov 5, 2023
@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


What you are doing is the manual reset of win event. The current automatic reset or manual reset are both common usage and suitable for different user needs. It is impossible to change the internal behavior of the interface to manual reset. To change the interface, this will break a bunch of existing users.

In addition, for your current needs, you can just use semaphore, and it won't cost more than a few lines of code. There is no need to change event. And I'm too lazy to change it. You can encapsulate it based on semaphore.

@duyanning
Copy link
Contributor Author

master...duyanning:tbox:master

Please take a look at it. It may be useful one day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants