我正在Linux中做C程序。我有一个主线程,它连续更新两个变量的值,其他线程每20毫秒将这些变量值写入文件中。我利用睡眠来达到这个时间间隔。示例代码如下。
main()
{
.
.
.
.
.
pthread_create(...write_file..); /* started another thread by passing a function write_file */
while(variable1)
{
updates value of variables
}
return 0;
}
void write_file()
{
假设你有这段代码:
useEffect(() => {
const interval = setInterval(() => {
console.log("This should run at the end of every second");
}, 1000);
return () => clearInterval(interval);
}, []);
这将每秒钟记录一次日志,但我希望它在每秒结束时进行日志记录(假设它以300毫秒的速度运行到当前秒,我希望它第一次运行需要700毫秒,以后每运行1000毫秒)。这是怎么做到的呢?