• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

pthread_yield example in c

by admin

One advantage to using threads is that they can execute for a very long time without preventing the execution of your main thread/application. The downside is that threads that execute without an end can end up consuming too much CPU.

In some cases, however, the user might need the thread to perform an action and then release its access to the CPU to allow another thread to perform its task. To accomplish this, we use the pthread_yield() API, which takes the following form:

Syntax:

int pthread_yield(void);

Returns 0 on success and error value on error.

Example Code

Consider the example shown below:

#include <stdio.h>
#include <pthread.h>

pthread_t tid[2];

void *thread_fn1(void *arg)
{
 int err = pthread_yield();
 perror("pthread_yield");
 printf("Thread 1 in execution\n");
 pthread_exit(NULL);
}

void *thread_fn2(void *arg)
{
 printf("Thread 2 in execution\n");
 pthread_exit(NULL);
}

int main()
{
 int ret;
 ret = pthread_create(&tid[0], NULL, thread_fn1, NULL);
 if (!ret)
  printf("Thread[%d] created Successfully\n",1);
 else
  printf("Thread[%d] not created\n",1);
 ret = pthread_create(&tid[1], NULL, thread_fn2, NULL);
 if (!ret)
  printf("Thread[%d] created Successfully\n",2);
 else
  printf("Thread[%d] not created\n",2);

 pthread_exit(NULL);

}

Note: Although this function is provided, it should be noted that the operating system is excellent at handling threads that must perform a lot of work, and pthread_yield() should only be used when the user explicitly understands how it might provide optimization in their specific use case (since overuse of the pthread_yield() function can actually result in performance degradation).

It should also be noted that pthread_yield() is not available on all Unix systems.

Filed Under: C, C Library

Some more articles you might also be interested in …

  1. Beginners Guide to C Structures: Definition, creation and manipulation
  2. C while, do-while and for Loops
  3. Beginners Guide to C functions
  4. C Pointers
  5. C Branching Statements with Examples
  6. Linux Device Driver example for dump_stack() to print the stack trace of module loading
  7. C Operators
  8. Yocto recipetool tutorial
  9. C Programming Basics – Interview Questions
  10. File Handling in C Programming

You May Also Like

Primary Sidebar

Recent Posts

  • nixos-rebuild Command Examples in Linux
  • nixos-option: Command Examples in Linux
  • nixos-container : Command Examples in Linux
  • nitrogen Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright