1// thread_basic.c
2// κΈ°λ³Έ μ€λ λ νλ‘κ·Έλ¨
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <pthread.h>
7#include <unistd.h>
8
9// μ€λ λ ν¨μ
10void* print_message(void* arg) {
11 char* message = (char*)arg;
12
13 for (int i = 0; i < 5; i++) {
14 printf("[μ€λ λ] %s - %d\n", message, i);
15 sleep(1);
16 }
17
18 return NULL;
19}
20
21int main(void) {
22 pthread_t thread;
23 const char* msg = "Hello from thread";
24
25 printf("=== κΈ°λ³Έ μ€λ λ μμ ===\n\n");
26
27 // μ€λ λ μμ±
28 int result = pthread_create(&thread, NULL, print_message, (void*)msg);
29 if (result != 0) {
30 fprintf(stderr, "μ€λ λ μμ± μ€ν¨: %d\n", result);
31 return 1;
32 }
33
34 // λ©μΈ μ€λ λλ μμ
μν
35 for (int i = 0; i < 5; i++) {
36 printf("[λ©μΈ] Main thread - %d\n", i);
37 sleep(1);
38 }
39
40 // μ€λ λ μ’
λ£ λκΈ°
41 pthread_join(thread, NULL);
42
43 printf("\nλͺ¨λ μμ
μλ£\n");
44 return 0;
45}
46
47/*
48 * μ»΄νμΌ λ°©λ²:
49 * gcc thread_basic.c -o thread_basic -pthread
50 *
51 * μ€ν:
52 * ./thread_basic
53 *
54 * λ©μΈ μ€λ λμ μμ±λ μ€λ λκ° λμμ μ€νλ©λλ€.
55 */