Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pthread - What is the difference between time.h::sleep() and pthread.h::pthread_yield()?

I spent a good long while looking for info on the differences between time.h::sleep() and pthread.h::pthread_yield() but was unable to find any solid reference material and so I am posting this question.

What is the difference between time.h::sleep() and pthread.h::pthread_yield()?

Update:

The reason I ask is because I was using sleep() to sleep() each individual thread... and my application started having issues when there was 8 threads vs 4 threads. When I went online to see if sleep() only affects each thread, I couldn't find any good reference stating whether Sleep() affects the entire process OR sleep() only affects the individual thread.

like image 297
Trevor Boyd Smith Avatar asked Jun 01 '09 21:06

Trevor Boyd Smith


People also ask

What is Pthread_yield?

The pthread_yield() function allows a thread to give up control of a processor so that another thread can have the opportunity to run. The parameter to the function must be NULL, because non-NULL values are reserved.

What is pthread H used for?

The pthread. h header file contains function declarations and mappings for threading interfaces and defines a number of constants used by those functions. The header includes the sched. h header.

Is pthread the same as thread?

POSIX Threads, commonly known as pthreads, is an execution model that exists independently from a language, as well as a parallel execution model. It allows a program to control multiple different flows of work that overlap in time.

What is pthread supported functions and syntax?

The functions defined in the pthreads library include: pthread_create: used to create a new thread. Syntax: int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg);


2 Answers

From pthread_yield:

The pthread_yield subroutine forces the calling thread to relinquish use of its processor, and to wait in the run queue before it is scheduled again. If the run queue is empty when the pthread_yield subroutine is called, the calling thread is immediately rescheduled.

From the sleep manpage:

sleep() makes the calling process sleep until seconds seconds have elapsed or a signal arrives which is not ignored.

If you don't want to have a real time delay in your threads and just want to allow other threads to do their work, then pthread_yield is better suited than sleep.

like image 168
lothar Avatar answered Oct 14 '22 22:10

lothar


sleep() causes your program to stop executing for a certain length of time. No matter what else happens on the system, your thread will not start again until at least the length of time passed to sleep() has elapsed. pthread_yield() notifies the operating system that your thread is done working, and that it can switch execution to another thread. However, if there is no higher-priority thread that needs to do work at that time, your thread may start again immediately.

IOWs, after sleep() your thread is guaranteed to stop running even if no one else needs to run, while pthread_yield() is just a polite way to give other threads a chance to run if they need to.

Update in response to question update: both sleep() and pthread_yield() affect only the calling thread.

like image 28
JSBձոգչ Avatar answered Oct 14 '22 21:10

JSBձոգչ