thread1.cpp 657 Bytes
Newer Older
lishen's avatar
lishen committed
1
2
3
4
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
5
#include <thread>
lishen's avatar
lishen committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

void* thread_function(void* arg) {
    // 线程开始执行的函数
    printf("Thread is running with argument: %s\n", (char*)arg);
    return NULL;
}

int main() {
    pthread_t thread_id;
    const char* message = "Hello, World!";
    int result;

    // 创建线程
    result = pthread_create(&thread_id, NULL, thread_function, (void*)message);
    if(result != 0) {
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }

    printf("Thread created successfully\n");
    pthread_exit(NULL); // 等待线程结束
    return 0;
}