"Pthread"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-== 참고 자료 == +==참고==))
잔글 (봇: 쓰레드을(를) 스레드(으)로 분류 대체함)
60번째 줄: 60번째 줄:
*http://en.wikipedia.org/wiki/POSIX_Threads
*http://en.wikipedia.org/wiki/POSIX_Threads


[[분류: 쓰레드]]
[[분류:스레드]]

2021년 10월 16일 (토) 15:37 판

1 개요

POSIX Thread; Pthread
POSIX 쓰레드, POSIX 스레드; P쓰레드
  • 쓰레드에 관한 POSIX 표준
  • 쓰레드 작성을 위해 제공되는 POSIX 표준 API

2 예시

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
 
#define NUM_THREADS     5
 
void *task_code(void *argument)
{
   int tid;
 
   tid = *((int *) argument);
   printf("Hello World! It's me, thread %d!\n", tid);
 
   /* optionally: insert more useful stuff here */
 
   return NULL;
}
 
int main(void)
{
   pthread_t threads[NUM_THREADS];
   int thread_args[NUM_THREADS];
   int rc, i;
 
   // create all threads one by one
   for (i=0; i<NUM_THREADS; ++i) {
      thread_args[i] = i;
      printf("In main: creating thread %d\n", i);
      rc = pthread_create(&threads[i], NULL, task_code, (void *) &thread_args[i]);
      assert(0 == rc);
   }
 
   // wait for each thread to complete
   for (i=0; i<NUM_THREADS; ++i) {
      // block until thread i completes
      rc = pthread_join(threads[i], NULL);
      printf("In main: thread %d is complete\n", i);
      assert(0 == rc);
   }
 
   printf("In main: All threads completed successfully\n");
   exit(EXIT_SUCCESS);
}

3 같이 보기

4 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}