C++ What can I use instead sleep() function?

时间:2022-09-05 13:46:11

I'm building scrobbler, and I want my program to wait 10 seconds after song change, before scrobbling. I have been using sleep but I realized that if song change during these 10 seconds, program submit old song and get new one. I want If I change song, code start all over again.

我正在制作scrobbler,我希望我的节目在歌曲改变之后等待10秒,然后才会出现问题。我一直在使用睡眠,但我意识到,如果在这10秒内歌曲发生变化,程序会提交旧歌并获得新歌。我想如果我改变歌曲,代码重新开始。

I'm using Music Player Daemon (MPD) and libmpd to get songs' tags.

我正在使用音乐播放器守护进程(MPD)和libmpd来获取歌曲的标签。

Note: program is under Unix.

注意:程序在Unix下。

2 个解决方案

#1


2  

It depens a lot on how your program works, but in principle, the easiest way would be to keep using sleep and check whether the user changed the song before sending out that data (after sleep has returned). So, instead of "try to sleep better", the goal would be "check that the data you send is really valid before sending".

它取决于你的程序如何工作,但原则上,最简单的方法是继续使用睡眠并检查用户是否在发送数据之前更改了歌曲(睡眠状态返回后)。因此,不是“尝试更好地睡觉”,而是“在发送之前检查您发送的数据是否真正有效”。

A different possibility would be to wait on an epoll using either the timeout for sleeping or better yet on a timerfd, and notify song change via an eventfd. This has the advantage that it is "free" if you need reliable inter-thread communication and readiness notification anyway, which you most probably do (obviously you must have at least one additional GUI thread, or the user would not be able to change songs while you're blocking).

另一种可能性是使用暂停超时或者更好地使用timerfd等待epoll,并通过eventfd通知歌曲更改。如果你需要可靠的线程间通信和准备就绪通知,这很有可能是“免费的”,你最有可能这样做(显然你必须至少有一个额外的GUI线程,否则用户将无法更改歌曲而你在阻止)。

#2


1  

Damon's suggestion is a good one and may be a better overall design. If you're looking for something quick though, you could consider simply sending a signal to your application when the song changes. That will interrupt the sleep() system call and cause it to return early. Your application would then just need to handle the early return as appropriate. Depending on your implementation, this may not be appropriate but it might give you a quick fix.

达蒙的建议很好,可能是一个更好的整体设计。如果您正在寻找快速的东西,您可以考虑在歌曲发生变化时向您的应用程序发送信号。这将中断sleep()系统调用并使其提前返回。您的应用程序只需要在适当的时候处理早期返回。根据您的实施情况,这可能不合适,但它可能会为您提供快速解决方案。

#1


2  

It depens a lot on how your program works, but in principle, the easiest way would be to keep using sleep and check whether the user changed the song before sending out that data (after sleep has returned). So, instead of "try to sleep better", the goal would be "check that the data you send is really valid before sending".

它取决于你的程序如何工作,但原则上,最简单的方法是继续使用睡眠并检查用户是否在发送数据之前更改了歌曲(睡眠状态返回后)。因此,不是“尝试更好地睡觉”,而是“在发送之前检查您发送的数据是否真正有效”。

A different possibility would be to wait on an epoll using either the timeout for sleeping or better yet on a timerfd, and notify song change via an eventfd. This has the advantage that it is "free" if you need reliable inter-thread communication and readiness notification anyway, which you most probably do (obviously you must have at least one additional GUI thread, or the user would not be able to change songs while you're blocking).

另一种可能性是使用暂停超时或者更好地使用timerfd等待epoll,并通过eventfd通知歌曲更改。如果你需要可靠的线程间通信和准备就绪通知,这很有可能是“免费的”,你最有可能这样做(显然你必须至少有一个额外的GUI线程,否则用户将无法更改歌曲而你在阻止)。

#2


1  

Damon's suggestion is a good one and may be a better overall design. If you're looking for something quick though, you could consider simply sending a signal to your application when the song changes. That will interrupt the sleep() system call and cause it to return early. Your application would then just need to handle the early return as appropriate. Depending on your implementation, this may not be appropriate but it might give you a quick fix.

达蒙的建议很好,可能是一个更好的整体设计。如果您正在寻找快速的东西,您可以考虑在歌曲发生变化时向您的应用程序发送信号。这将中断sleep()系统调用并使其提前返回。您的应用程序只需要在适当的时候处理早期返回。根据您的实施情况,这可能不合适,但它可能会为您提供快速解决方案。