⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread6.c

📁 linux的程序设计第二版 是学习LINUX下程序设计的好教程哦
💻 C
字号:
#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <pthread.h>void *thread_function(void *arg);char message[] = "Hello World";int thread_finished = 0;int main() {    int res;    pthread_t a_thread;    void *thread_result;    pthread_attr_t thread_attr;    res = pthread_attr_init(&thread_attr);    if (res != 0) {        perror("Attribute creation failed");        exit(EXIT_FAILURE);    }    res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);    if (res != 0) {        perror("Setting detached attribute failed");        exit(EXIT_FAILURE);    }    res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message);    if (res != 0) {        perror("Thread creation failed");        exit(EXIT_FAILURE);    }    (void)pthread_attr_destroy(&thread_attr);    while(!thread_finished) {        printf("Waiting for thread to say it's finished...\n");        sleep(1);    }    printf("Other thread finished, bye!\n");    exit(EXIT_SUCCESS);}void *thread_function(void *arg) {    printf("thread_function is running. Argument was %s\n", (char *)arg);    sleep(4);    printf("Second thread setting finished flag, and exiting now\n");    thread_finished = 1;    pthread_exit(NULL);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -