Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

c - C中的usleep()是否实现为忙等待?(Is usleep() in C implemented as busy wait?)

I'm building a multithreaded application with pthreads and need a thread to periodically check some stuff.

(我正在使用pthreads构建多线程应用程序,并且需要一个线程来定期检查一些东西。)

During the time in between this thread shouldn't use any CPU.

(在此线程之间的时间内,不应使用任何CPU。)

Is this possible with usleep() ?

(usleep()有可能吗?)

Is usleep() not busy waiting?

(usleep()不忙于等待?)

Or is there a better solution?

(还是有更好的解决方案?)

  ask by zeebonk translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The function usleep has been removed from SUSv4.

(usleep功能已从SUSv4中删除。)

You should probably use nanosleep instead or timers ( setitimer , etc).

(您可能应该使用nanosleep或计时器( setitimer等)。)

As R.. notes in the comments, should the sleep be implemented as a busy wait:

(正如R ..在评论中指出的那样,应将睡眠实施为繁忙等待:)

  • The thread would continue to use the CPU

    (线程将继续使用CPU)

  • Other (lower-priority) threads wouldn't get a chance to run

    (其他(低优先级)线程将无法运行)

Thus:

(从而:)

  • Some might use signals (I think SUSv3 mentioned SIGALARM?)

    (有些人可能会使用信号(我认为SUSv3提到了SIGALARM吗?))

  • Some might use fancy timers

    (有些人可能会使用花式计时器)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...