主线程中的nanosleep()是否会影响其他线程?

时间:2021-09-12 20:40:32

My question is about the function nanosleep in c unix. If I put a nanosleep in the main function, will the other threads be affected by that?

我的问题是关于c unix中的nanosleep功能。如果我在主函数中放入nanosleep,那么其他线程会受到影响吗?

1 个解决方案

#1


3  

No, only the "current" thread is suspended.

不,只有“当前”线程被暂停。

The main() function runs in the main thread of the program, suspending it with nanosleep() will only suspend the main thread, all other threads will continue to execute normally.

main()函数在程序的主线程中运行,使用nanosleep()挂起它只会挂起主线程,所有其他线程将继续正常执行。

From the nanosleep(2) manual page

来自nanosleep(2)手册页

nanosleep() suspends the execution of the calling thread until either at least the time specified in *req has elapsed, or the delivery of a signal that triggers the invocation of a handler in the calling thread or that terminates the process.

nanosleep()暂停调用线程的执行,直到至少在* req中指定的时间已经过去,或者传递一个信号,该信号触发调用线程中的处理程序的调用或终止进程。

In POSIX (which is more general than unix) you can make the main thread block until another threads exits by using pthread_join().

在POSIX(比unix更通用)中,您可以使用pthread_join()创建主线程块,直到另一个线程退出。

#1


3  

No, only the "current" thread is suspended.

不,只有“当前”线程被暂停。

The main() function runs in the main thread of the program, suspending it with nanosleep() will only suspend the main thread, all other threads will continue to execute normally.

main()函数在程序的主线程中运行,使用nanosleep()挂起它只会挂起主线程,所有其他线程将继续正常执行。

From the nanosleep(2) manual page

来自nanosleep(2)手册页

nanosleep() suspends the execution of the calling thread until either at least the time specified in *req has elapsed, or the delivery of a signal that triggers the invocation of a handler in the calling thread or that terminates the process.

nanosleep()暂停调用线程的执行,直到至少在* req中指定的时间已经过去,或者传递一个信号,该信号触发调用线程中的处理程序的调用或终止进程。

In POSIX (which is more general than unix) you can make the main thread block until another threads exits by using pthread_join().

在POSIX(比unix更通用)中,您可以使用pthread_join()创建主线程块,直到另一个线程退出。