典型的 UNIX/Linux 进程可以看成只有一个控制线程:一个进程在同一时刻只做一件事情。有了多个控制线程后,在程序设计时可以把进程设计成在同一时刻做不止一件事,每个线程各自处理独立的任务。
进程是程序执行时的一个实例,是担当分配系统资源(CPU 时间、内存等)的基本单位。在面向线程设计的系统中,进程本身不是基本运行单位,而是线程的容器。程序本身只是指令、数据及其组织形式的描述,进程才是程序(那些指令和数据)的真正运行实例。
线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。线程包含了表示进程内执行环境必须的信息,其中包括进程中表示线程的线程 ID、一组寄存器值、栈、调度优先级和策略、信号屏蔽字、errno 常量以及线程私有数据。进程的所有信息对该进程的所有线程都是共享的,包括可执行的程序文本、程序的全局内存和堆内存、栈以及文件描述符。在 Unix 和类 Unix 操作系统中线程也被称为轻量级进程(lightweight processes),但轻量级进程更多指的是内核线程(kernel thread),而把用户线程(user thread)称为线程。
“进程——资源分配的最小单位,线程——程序执行的最小单位”
从上面我们知道了进程与线程的区别,其实这些区别也就是我们使用线程的理由。总的来说就是:进程有独立的地址空间,线程没有单独的地址空间(同一进程内的线程共享进程的地址空间)。
使用多线程的理由之一是和进程相比,它是一种非常"节俭"的多任务操作方式。我们知道,在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护它的代码段、堆栈段和数据段,这是一种"昂贵"的多任务工作方式。而运行于一个进程中的多个线程,它们彼此之间使用相同的地址空间,共享大部分数据,启动一个线程所花费的空间远远小于启动一个进程所花费的空间,而且,线程间彼此切换所需的时间也远远小于进程间切换所需要的时间。据统计,总的说来,一个进程的开销大约是一个线程开销的30倍左右,当然,在具体的系统上,这个数据可能会有较大的区别。
使用多线程的理由之二是线程间方便的通信机制。对不同进程来说,它们具有独立的数据空间,要进行数据的传递只能通过通信的方式进行(信号量、共享内存、消息队列…),这种方式不仅费时,而且很不方便。线程则不然,由于同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其它线程所用,这不仅快捷,而且方便。当然,数据的共享也带来其他一些问题(条件变量和互斥锁可解决),有的变量不能同时被两个线程所修改,有的子程序中声明为static的数据更有可能给多线程程序带来灾难性的打击,这些正是编写多线程程序时最需要注意的地方。
多线程开发在 Linux 平台上已经有成熟的 pthread 库支持。其涉及的多线程开发的最基本概念主要包含三点:线程,互斥锁,条件。其中,线程操作又分线程的创建,退出,等待 3 种。互斥锁则包括 4 种操作,分别是创建,销毁,加锁和解锁。条件操作有 5 种操作:创建,销毁,触发,广播和等待。其他的一些线程扩展概念,如信号灯等,都可以通过上面的三个基本元素的基本操作封装出来。详细请见下表:
#include
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
// 返回:若成功返回0,否则返回错误编号
#include
int pthread_exit(void *rval_ptr);
#include
int pthread_join(pthread_t thread, void **rval_ptr);
// 返回:若成功返回0,否则返回错误编号
#include
int pthread_detach(pthread_t thread);
// 返回:若成功返回0,否则返回错误编号
pthread_detach(pthread_self());
#include
pthread_t pthread_self(void);
// 返回:调用线程的ID
#include
int pthread_equal(pthread_t tid1, pthread_t tid2);
// 返回:若相等则返回非0值,否则返回0
#include
#include
//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
void *func1(void *arg)
{//子线程
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
ret = pthread_create(&t1, NULL, func1,(void *)¶m);
if(ret == 0){
printf("main:create t1 success\n");//主线程
}
printf("main:%ld\n",(unsigned long)pthread_self());//获取线程的ID
pthread_join(t1,NULL);//如果没有while(1)或join函数,子线程运行可能会丢失
return 0;
}
#include
#include
//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
void *func1(void *arg)
{//子线程
static int ret = 10;//必须使用静态变量 其在每次函数调用之间保留其值,可在多个函数调用之间共享状态
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
pthread_exit((void *)&ret);//退出
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
int *pret = NULL;
ret = pthread_create(&t1, NULL, func1,(void *)¶m);
if(ret == 0){
printf("main:create t1 success\n");//主线程
}
printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1,(void **)&pret);//等待,线程将一直阻塞直到退出(ret)
printf("main: t1 quit: %d\n",*pret);//取内容
return 0;
}
#include
#include
//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
void *func1(void *arg)
{
static char *p = "t1 is run out";
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
pthread_exit((void *)p);
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
char *pret;
ret = pthread_create(&t1, NULL, func1,(void *)¶m);
if(ret == 0){
printf("main:create t1 success\n");
}
printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1,(void **)&pret);
printf("main: t1 quit: %s\n",pret);
return 0;
}
#include
#include
#include
//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
int g_data = 0;
void *func1(void *arg)
{//子线程1
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
while(1){
printf("t1: %d\n",g_data++);
sleep(1);
if(g_data == 3){//不一定每次都会到他
pthread_exit(NULL);
}
}
}
void *func2(void *arg)
{//子线程2
printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
printf("t2:param is %d\n",*((int *)arg));
while(1){
printf("t2: %d\n",g_data++);
sleep(1);
}
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
ret = pthread_create(&t1, NULL, func1,(void *)¶m);
if(ret == 0){
printf("main:create t1 success\n");
}
ret = pthread_create(&t2, NULL, func2,(void *)¶m);
if(ret == 0){
printf("main:create t2 success\n");
}
printf("main:%ld\n",(unsigned long)pthread_self());//主线程
while(1){
printf("main: %d\n",g_data++);
sleep(1);
}
pthread_join(t1,NULL);
pthread_join(t2,NULL);
return 0;
}
#include
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
// 返回:若成功返回0,否则返回错误编号
#include
int pthread_mutex_lock(pthread_mutex_t mutex);//加锁
int pthread_mutex_trylock(pthread_mutex_t mutex);
int pthread_mutex_unlock(pthread_mutex_t mutex);//解锁
// 返回:若成功返回0,否则返回错误编号
#include
#include
#include
/*int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int pthread_mutex_lock(pthread_mutex_t mutex);
int pthread_mutex_unlock(pthread_mutex_t mutex);
int pthread_mutex_destroy(pthread_mutex_t mutex);*/
int g_data = 0;
pthread_mutex_t mutex;//创建一把互斥锁mutex
void *func1(void *arg)
{
int i;
pthread_mutex_lock(&mutex);//加锁
for(i=0;i<5;i++){
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
sleep(1);
}
pthread_mutex_unlock(&mutex);//解锁
}
void *func2(void *arg)
{
pthread_mutex_lock(&mutex);
printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
printf("t2:param is %d\n",*((int *)arg));
pthread_mutex_unlock(&mutex);
}
void *func3(void *arg)
{
pthread_mutex_lock(&mutex);
printf("t3:%ld thread is create\n",(unsigned long)pthread_self());
printf("t3:param is %d\n",*((int *)arg));
pthread_mutex_unlock(&mutex);
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
pthread_t t3;//子线程123
pthread_mutex_init(&mutex, NULL);//初始化互斥锁,属性NULL
ret = pthread_create(&t1, NULL, func1,(void *)¶m);
if(ret == 0){
printf("main:create t1 success\n");
}
ret = pthread_create(&t2, NULL, func2,(void *)¶m);
if(ret == 0){
printf("main:create t2 success\n");
}
ret = pthread_create(&t3, NULL, func3,(void *)¶m);//创建子线程123
printf("main:%ld\n",(unsigned long)pthread_self());//打印主线程id
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_join(t3,NULL);//子线程的等待
pthread_mutex_destroy(&mutex);//销毁互斥锁
return 0;
}
vi test.sh
./a.out
./a.out
./a.out
chmod +x test.sh
./test.sh
#include
int main(){
int i = 0;
for(i=0;i<100;i++){
system("./thread");
}
}
#include
#include
#include //sleep
#include //exit
int g_data = 0;
pthread_mutex_t mutex;
void *func1(void *arg)
{
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
pthread_mutex_lock(&mutex);
while(1){
printf("t1: %d\n",g_data++);
sleep(1);
if(g_data == 3){
pthread_mutex_unlock(&mutex);
printf("t1 quit================================\n");
//pthread_exit(NULL);
exit(0);
}
}
}
void *func2(void *arg)
{
printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
printf("t2:param is %d\n",*((int *)arg));
while(1){
printf("t2: %d\n",g_data);
pthread_mutex_lock(&mutex);
g_data++;
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
pthread_mutex_init(&mutex,NULL);
ret = pthread_create(&t1, NULL, func1,(void *)¶m);
if(ret == 0){
printf("main:create t1 success\n");
}
ret = pthread_create(&t2, NULL, func2,(void *)¶m);
if(ret == 0){
printf("main:create t2 success\n");
}
printf("main:%ld\n",(unsigned long)pthread_self());
while(1){
printf("main: %d\n",g_data);
sleep(1);
}
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
#include
#include
#include //sleep
int g_data = 0;
pthread_mutex_t mutex;
pthread_mutex_t mutex2;
void *func1(void *arg)
{
int i;
pthread_mutex_lock(&mutex);
sleep(1);
pthread_mutex_lock(&mutex2);
for(i=0;i<5;i++){
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
sleep(1);
}
pthread_mutex_unlock(&mutex);
}
void *func2(void *arg)
{
pthread_mutex_lock(&mutex2);
sleep(1);
pthread_mutex_lock(&mutex);
printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
printf("t2:param is %d\n",*((int *)arg));
pthread_mutex_unlock(&mutex);
}
void *func3(void *arg)
{
pthread_mutex_lock(&mutex);
printf("t3:%ld thread is create\n",(unsigned long)pthread_self());
printf("t3:param is %d\n",*((int *)arg));
pthread_mutex_unlock(&mutex);
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
pthread_t t3;
pthread_mutex_init(&mutex, NULL);
pthread_mutex_init(&mutex2, NULL);
ret = pthread_create(&t1, NULL, func1,(void *)¶m);
if(ret == 0){
printf("main:create t1 success\n");
}
ret = pthread_create(&t2, NULL, func2,(void *)¶m);
if(ret == 0){
printf("main:create t2 success\n");
}
ret = pthread_create(&t3, NULL, func3,(void *)¶m);
printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_mutex_destroy(&mutex);
pthread_mutex_destroy(&mutex2);
return 0;
}
- 条件变量是线程另一可用的同步机制。条件变量给多个线程提供了一个会合的场所。条件变量与互斥量一起使用时,允许线程以无竞争的方式等待特定的条件发生。
- 条件本身是由互斥量保护的。线程在改变条件状态前必须首先锁住互斥量,其他线程在获得互斥量之前不会察觉到这种改变,因为必须锁定互斥量以后才能计算条件。
- 条件变量使用之前必须首先初始化,pthread_cond_t 数据类型代表的条件变量可以用两种方式进行初始化,可以把常量 PTHREAD_COND_INITIALIZER 赋给静态分配的条件变量,但是如果条件变量是动态分配的,可以使用 pthread_cond_destroy 函数对条件变量进行去除初始化(deinitialize)。
#include
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t cond);
// 返回:若成功返回0,否则返回错误编号
#include
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, cond struct timespec *restrict timeout);
// 返回:若成功返回0,否则返回错误编号
#include
int pthread_cond_signal(pthread_cond_t *cond);//接受者有一条
int pthread_cond_broadcast(pthread_cond_t *cond);//接受者有多条
// 返回:若成功返回0,否则返回错误编号
#include
#include
#include //sleep
#include //exit
int g_data = 0;
pthread_mutex_t mutex;//创建一把互斥锁mutex
pthread_cond_t cond;//创建一个条件变量cond
void *func1(void *arg)
{
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
static int cnt = 1;
while(1){
pthread_cond_wait(&cond,&mutex);//等待(发生在锁之上的)条件的发话
printf("t1 run================================\n");
printf("t1: %d\n",g_data);
g_data = 0;
sleep(1);
if(cnt++ == 10){//子线程运行10次退出
exit(1);
}
}
}
void *func2(void *arg)
{
printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
printf("t2:param is %d\n",*((int *)arg));
while(1){
printf("t2: %d\n",g_data);
pthread_mutex_lock(&mutex);
g_data++;
if(g_data == 3){
pthread_cond_signal(&cond);//到3时运行cond后面的
}
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
pthread_mutex_init(&mutex,NULL);//动态初始化(互斥锁),属性NULL
pthread_cond_init(&cond,NULL);//动态初始化(条件变量),属性NULL
ret = pthread_create(&t1, NULL, func1,(void *)¶m);
if(ret == 0){
// printf("main:create t1 success\n");
}
ret = pthread_create(&t2, NULL, func2,(void *)¶m);
if(ret == 0){
// printf("main:create t2 success\n");
}
// printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1,NULL);
pthread_join(t2,NULL);//子线程的等待
pthread_mutex_destroy(&mutex);//销毁互斥锁
pthread_cond_destroy(&cond);//销毁条件变量
return 0;
}
#include
#include
#include //sleep
#include //exit
int g_data = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;//静态初始化
void *func1(void *arg)
{
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
static int cnt = 0;
while(1){
pthread_cond_wait(&cond,&mutex);
printf("t1 run================================\n");
printf("t1: %d\n",g_data);
g_data = 0;
sleep(1);
if(cnt++ == 10){
exit(1);
}
}
}
void *func2(void *arg)
{
printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
printf("t2:param is %d\n",*((int *)arg));
while(1){
printf("t2: %d\n",g_data);
pthread_mutex_lock(&mutex);
g_data++;
if(g_data == 3){
pthread_cond_signal(&cond);
}
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
// pthread_mutex_init(&mutex,NULL);
// pthread_cond_init(&cond,NULL);
ret = pthread_create(&t1, NULL, func1,(void *)¶m);
if(ret == 0){
// printf("main:create t1 success\n");
}
ret = pthread_create(&t2, NULL, func2,(void *)¶m);
if(ret == 0){
// printf("main:create t2 success\n");
}
// printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}