线程控制 线程创建 代码示例 线程中的各种id 线程终止 主动退出 被动退出 线程等待 代码示例: 线程分离 代码示例 实际上Linux操作系统并没有给用户提供创建一个线程的系统调用接口,用户无法直接创建线程,但是大佬们封装了一套线程库,通过库函数可以实现线程控制的各种操作。 线程创建 我们创建线程就是在用户态创建线程,可以实现在内核态创建一个轻量级进程实现调度; int pthread_create(pthread_t *tid, pthread_attr_t *attr, void*(*thread_routine)(void *arg), void *arg) tid:输出型参数,用于向用户返回线程id,是后序续线程操作的句柄 attr:用于设置线程属性,通常置NULL thread_routine:线程的入口函数 arg:最终会通过线程入口函数的参数传递给线程的数据 返回值:若成功则返回0,失败返回一个非0值 代码示例 #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> void *thread_start(void *arg) { printf("accept information : %s", (char*)arg); while(1) { printf("this is a normal thread\n"); sleep(1); } return NULL