Home >Backend Development >C++ >Using C program for thread synchronization to print numbers in sequence
Given a thread, the program must print the threads from 0 to 10 according to their priority.
Threads are lightweight processes that run inside the program. A simple program can contain n threads.
Unlike Java, the C/C language standard does not support multi-threading. POSIX threads (Pthreads) are the standard for multi-threading in C/C. The C language does not contain any built-in support for multi-threaded applications, but relies entirely on the operating system to provide this functionality. How does
In order to use thread functions, we use the header file #include. This header file will contain all thread-related functions in our program, such as pthread_create(), etc.
The current task is to use the pthread standard library provided by the gcc compiler to synchronize n threads. The idea is to get the thread count and print 1 in the first thread, 2 in the second thread, 3 in the third thread, until the tenth thread. The output will contain numbers from 1 to 10 based on the thread's priority. The Chinese translation of
Start Step 1 -> Declare global variables as int MAX=10 and count=1 Step 2 -> declare variable thr of pthread_mutex_t and cond of pthread_cond_t Step 3 -> Declare Function void *even(void *arg) Loop While(count < MAX) Call pthread_mutex_lock(&thr) Loop While(count % 2 != 0) Call pthread_cond_wait(&cond, &thr) End Print count++ Call pthread_mutex_unlock(&thr) Call pthread_cond_signal(&cond) End Call pthread_exit(0) Step 4 -> Declare Function void *odd(void *arg) Loop While(count < MAX) Call pthread_mutex_lock(&thr) Loop While(count % 2 != 1) Call pthread_cond_wait(&cond, &thr) End Print count++ Call pthread_mutex_unlock(&thr) Call pthread_cond_signal(&cond) End Set pthread_exit(0) Step 5 -> In main() Create pthread_t thread1 and pthread_t thread2 Call pthread_mutex_init(&thr, 0) Call pthread_cond_init(&cond, 0) Call pthread_create(&thread1, 0, &even, NULL) Call pthread_create(&thread2, 0, &odd, NULL) Call pthread_join(thread1, 0) Call pthread_join(thread2, 0) Call pthread_mutex_destroy(&thr) Call pthread_cond_destroy(&cond) Stop
#include <pthread.h> #include <stdio.h> #include <stdlib.h> int MAX = 10; int count = 1; pthread_mutex_t thr; pthread_cond_t cond; void *even(void *arg){ while(count < MAX) { pthread_mutex_lock(&thr); while(count % 2 != 0) { pthread_cond_wait(&cond, &thr); } printf("%d ", count++); pthread_mutex_unlock(&thr); pthread_cond_signal(&cond); } pthread_exit(0); } void *odd(void *arg){ while(count < MAX) { pthread_mutex_lock(&thr); while(count % 2 != 1) { pthread_cond_wait(&cond, &thr); } printf("%d ", count++); pthread_mutex_unlock(&thr); pthread_cond_signal(&cond); } pthread_exit(0); } int main(){ pthread_t thread1; pthread_t thread2; pthread_mutex_init(&thr, 0); pthread_cond_init(&cond, 0); pthread_create(&thread1, 0, &even, NULL); pthread_create(&thread2, 0, &odd, NULL); pthread_join(thread1, 0); pthread_join(thread2, 0); pthread_mutex_destroy(&thr); pthread_cond_destroy(&cond); return 0; }
If we run the above program, it will generate The following output
1 2 3 4 5 6 7 8 9 10
The above is the detailed content of Using C program for thread synchronization to print numbers in sequence. For more information, please follow other related articles on the PHP Chinese website!