Search anything:

Basic OpenMP functions

Learn Algorithms and become a National Programmer
Indian Technical Authorship Contest starts on 1st July 2023. Stay tuned.

Reading time: 15 minutes

There are 3 basic functions in OpenMP namely omp_get_thread_num, omp_set_num_threads (nthreads) and omp_get_num_threads.


  • omp_get_thread_num() - get the thread rank in a parallel region (0- omp_get_num_threads() -1)
  • omp_set_num_threads(nthreads) - set the number of threads used in a parallel region
  • omp_get_num_threads() - get the number of threads used in a parallel region


#include <omp.h>
int omp_get_thread_num();
# pragma omp parallel
  printf("Thread rank: %d\n", omp_get_thread_num());


Note that the thread rank is not in order:

Thread rank:  2
Thread rank:  0
Thread rank:  3
Thread rank:  1
OpenGenus Tech Review Team

OpenGenus Tech Review Team

The official account of OpenGenus's Technical Review Team. This team review all technical articles and incorporates peer feedback. The team consist of experts in the leading domains of Computing.

Read More

Vote for Author of this article:

Improved & Reviewed by: