> 백엔드 개발 > C++ > 본문

C/C++의 스레드 함수

PHPz
풀어 주다: 2023-08-30 10:49:07
앞으로
1037명이 탐색했습니다.

C/C++의 스레드 함수

이 튜토리얼에서는 C/C++의 스레드 기능을 이해하는 프로그램에 대해 설명합니다.

스레딩 기능을 사용하면 사용자가 동시 기능을 동시에 구현할 수 있으며, 이는 실행을 위해 서로 종속되거나 독립적일 수 있습니다.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void* func(void* arg){
   //detaching the current thread
   pthread_detach(pthread_self());
   printf("Inside the thread\n");
   pthread_exit(NULL);
}
void fun(){
   pthread_t ptid;
   //creating a new thread
   pthread_create(&ptid, NULL, &func, NULL);
   printf("This line may be printed before thread terminates\n");
   if(pthread_equal(ptid, pthread_self())
      printf("Threads are equal\n");
   else
      printf("Threads are not equal\n");
   //waiting for the created thread to terminate
   pthread_join(ptid, NULL);
   printf("This line will be printed" " after thread ends\n");
   pthread_exit(NULL);
}
int main(){
   fun();
   return 0;
}
로그인 후 복사

출력

This line may be printed before thread terminates
Threads are not equal
Inside the thread
This line will be printed after thread ends
로그인 후 복사

위 내용은 C/C++의 스레드 함수의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:tutorialspoint.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!