Order of printing numbers using thread synchronization

王林
Release: 2023-09-22 21:41:10
forward
988 people have browsed it

Order of printing numbers using thread synchronization

Here we will see how to print numbers in the correct order using different threads. Here we will create n threads and then synchronize them. The idea is that the first thread will print 1, then the second thread will print 2, and so on. When one thread tries to print, it locks the resource so other threads cannot use that part.

Example

#include  #include  #include  #include  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t* cond = NULL; int threads; volatile int count = 0; void* sync_thread(void* num) { //this function is used to synchronize the threads int thread_number = *(int*)num; while (1) { pthread_mutex_lock(&mutex); //lock the section if (thread_number != count) { //if the thread number is not same as count, put all thread except one into waiting state pthread_cond_wait(&cond[thread_number], &mutex); } printf("%d ", thread_number + 1); //print the thread number count = (count+1)%(threads); // notify the next thread pthread_cond_signal(&cond[count]); pthread_mutex_unlock(&mutex); } return NULL; } int main() { pthread_t* thread_id; volatile int i; int* thread_arr; printf("

Enter number of threads: "); scanf("%d", &threads); // allocate memory to cond (conditional variable) thread id's and array of size threads cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t) * threads); thread_id = (pthread_t*)malloc(sizeof(pthread_t) * threads); thread_arr = (int*)malloc(sizeof(int) * threads); for (i = 0; i < threads; i++) { //create threads thread_arr[i] = i; pthread_create(&thread_id[i], NULL, sync_thread, (void*)&thread_arr[i]); } // waiting for thread for (i = 0; i < threads; i++) { pthread_join(thread_id[i], NULL); } return 0; }

Copy after login

Output

$ g++ test.cpp -lpthread $ ./a.out Enter number of threads: 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 ... ... ...
Copy after login

The above is the detailed content of Order of printing numbers using thread synchronization. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!