linux中的线程是轻量级的线程,linux中的线程调度是由内核调度程序完成的,每个线程有自己的ID号。与进程相比,它们消耗的系统资源少、创建较快、相互间的通信也较容易。linux线程分为两类:一是核心级支持线程,二是用户级的线程。一般都为用户级的线程。
一、多线程的几个常见函数
要创建多线程必须加载pthread.h文件,库文件pthread。线程的标识符pthread_t在头文件/usr/include/bits/pthreadtypes.h中定义:typedef unsigned long int pthread_t
1.创建线程:
int
pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
参数:
thread输出线程id
attr 线程属性, 默认NULL
start_routine线程执行函数
arg线程执行参数
note:函数成功返回0 否则返回错误码
2.等待指定线程结束:
int
pthread_join(pthread_t thread,vo