>  기사  >  백엔드 개발  >  스레드 동기화를 위한 C 프로그램을 사용하여 숫자를 순서대로 인쇄

스레드 동기화를 위한 C 프로그램을 사용하여 숫자를 순서대로 인쇄

王林
王林앞으로
2023-08-26 22:45:061325검색

스레드 동기화를 위한 C 프로그램을 사용하여 숫자를 순서대로 인쇄

스레드가 주어지면 프로그램은 우선순위에 따라 0부터 10까지 스레드를 인쇄해야 합니다.

스레드란 무엇인가요?

스레드는 프로그램 내에서 실행되는 가벼운 프로세스입니다. 간단한 프로그램에는 n개의 스레드가 포함될 수 있습니다.

Java와 달리 C/C++ 언어 표준은 멀티스레딩을 지원하지 않습니다. POSIX 스레드(Pthreads)는 C/C++의 멀티스레딩 표준입니다. C 언어에는 다중 스레드 응용 프로그램에 대한 기본 지원이 포함되어 있지 않지만 운영 체제에 전적으로 의존하여 이 기능을 제공합니다.

우리 프로그램에서는 어떻게 작동하나요?

스레드 기능을 사용하기 위해 헤더 파일 #include를 사용합니다. 이 헤더 파일에는 pthread_create() 등과 같은 프로그램의 모든 스레드 관련 함수가 포함됩니다.

현재 작업은 gcc 컴파일러에서 제공하는 pthread 표준 라이브러리를 사용하여 n 스레드를 동기화하는 것입니다. 아이디어는 스레드 수를 가져와 10번째 스레드까지 첫 번째 스레드에서 1, 두 번째 스레드에서 2, 세 번째 스레드에서 3을 인쇄하는 것입니다. 출력에는 스레드의 우선순위에 따라 1부터 10까지의 숫자가 포함됩니다.

Algorithm

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

Example

의 중국어 번역은 다음과 같습니다:

Example

#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;
}

Output

위 프로그램을 실행하면 다음과 같은 출력이 생성됩니다

1 2 3 4 5 6 7 8 9 10

위 내용은 스레드 동기화를 위한 C 프로그램을 사용하여 숫자를 순서대로 인쇄의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제