Linux应用编程8之什么是线程?
1.再论进程
1.1多进程实现同时读取键盘和鼠标
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void)
{
// 思路就是创建子进程,然后父子进程中分别进行读键盘和鼠标的工作
int ret = -1;
int fd = -1;
char buf[200];
ret = fork();
if (ret == 0)
{
// 子进程
fd = open("/dev/input/mouse0", O_RDONLY);
if (fd < 0)
{
perror("open:");
return -1;
}
while (1)
{
memset(buf, 0, sizeof(buf));
printf("before read.\n");
read(fd, buf, 50);
printf("读出鼠标的内容是:[%s].\n", buf);
}
}
else if (ret > 0)
{
// 父进程
while (1)
{
memset(buf, 0, sizeof(buf));
printf("before read.\n");
read(0, buf, 5);
printf("读出键盘的内容是:[%s].\n", buf);
}
}
else
{
perror("fork:");
return -1;
}
return 0;
}
1.2使用进程技术的优势
1)CPU时分复用,单核心CPU可以实现宏观上的并行
2)实现多任务系统需求(多任务的需求是客观的)
1.3进程技术的劣势
1)进程间切换开销大
2)进程间通信麻烦而且效率低
1.4解决方案就是线程技术
1)线程技术保留了进程技术实现多任务的特性。
2)线程的改进就是在线程间切换和线程间通信上提升了效率。
3)多线程在多核心CPU上面更有优势。
2.线程的引入
2.1使用线程技术同时读取键盘和鼠标
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
char buf[200];
void *func(void *arg)
{
while (1)
{
memset(buf, 0, sizeof(buf));
printf("before read.\n");
read(0, buf, 5);
printf("读出键盘的内容是:[%s].\n", buf);
}
}
int main(void)
{
// 思路就是创建子进程,然后父子进程中分别进行读键盘和鼠标的工作
int ret = -1;
int fd = -1;
pthread_t th = -1;
ret = pthread_create(&th, NULL, func, NULL);
if (ret != 0)
{
printf("pthread_create error.\n");
return -1;
}
// 因为主线程是while(1)死循环,所以可以在这里pthread_detach分离子线程
// 主任务
fd = open("/dev/input/mouse1", O_RDONLY);
if (fd < 0)
{
perror("open:");
return -1;
}
while (1)
{
memset(buf, 0, sizeof(buf));
printf("before read.\n");
read(fd, buf, 50);
printf("读出鼠标的内容是:[%s].\n", buf);
}
return 0;
}
2.2linux中的线程简介
1)一种轻量级进程
2)线程是参与内核调度的最小单元
3)一个进程中可以有多个线程
2.3线程技术的优势
1)像进程一样可被OS调度
2)同一进程的多个线程之间很容易高效率通信
3)在多核心CPU(对称多处理器架构SMP)架构下效率最大化
3.线程常见函数
3.1线程创建与回收
1)pthread_create 主线程用来创造子线程的
2)pthread_join 主线程用来等待(阻塞)回收子线程
q3)pthread_detach 主线程用来分离子线程,分离后主线程不必再去回收子线程
3.2线程取消
1)pthread_cancel 一般都是主线程调用该函数去取消(让它赶紧死)子线程
2)pthread_setcancelstate 子线程设置自己是否允许被取消
3)pthread_setcanceltype
3.3线程函数退出相关
1)pthread_exit与return退出
2)pthread_cleanup_push
3)pthread_cleanup_pop
3.4获取线程id
(1)pthread_self
4.线程同步之信号量
4.1、任务:用户从终端输入任意字符然后统计个数显示,输入end则结束
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
char buf[100];
printf("请输入一个字符,并以回车结束.\n");
while(scanf("%s", buf))
{
//比较用户输入的是不是end,如果是,则退出
if(!strncmp(buf, "end", 3))
{
printf("程序结束!.\n");
_exit(-1);
}
printf("本次输入了%d个字符.\n",strlen(buf));
memset(buf, 0, sizeof(buf));
}
return 0;
}
4.2、使用多线程实现:主线程获取用户输入并判断是否退出,子线程计数
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
char buf[100] = {0};
sem_t sem;
void fun(void)
{
//子线程是统计buf中的字符个数并打印
//子线程首先应该有个循环
//循环中阻塞等待主线程**的时候,去获取buf中的字符
sem_wait(&sem);
while(strncmp(buf, "end", 3) != 0)
{
printf("本次输入了%d个字符.\n",strlen(buf));
memset(buf, 0, sizeof(buf));
sem_wait(&sem);
}
pthread_exit(NULL);
}
int main(void)
{
int ret = -1;
pthread_t th = -1;
sem_init(&sem, 0, 0);
ret = pthread_create(&th, NULL, fun, NULL);
if (ret != 0)
{
perror("pthread_create:");
return -1;
}
printf("请输入一个字符,并以回车结束.\n");
while(scanf("%s", buf))
{
//比较用户输入的是不是end,如果是,则退出
if(!strncmp(buf, "end", 3))
{
printf("程序结束!.\n");
sem_post(&sem);
break;
}
//主线程在确认收到字符串不是end后,发信号**子线程
//子线程被阻塞,主线程可以**,这就是线程的同步
//信号量就可以完成这个操作
sem_post(&sem);
}
//回收子线程
printf("等待回收子线程!.\n");
ret = pthread_join(th, NULL);
if (ret != 0)
{
perror("pthread_join:");
return -1;
}
printf("收子线程回收成功!.\n");
sem_destroy(&sem);
return 0;
}
(1)为什么需要多线程实现
(2)问题和困难点是?
(3)理解什么是线程同步
4.3、信号量的介绍和使用
5.线程同步之互斥锁
5.1、什么是互斥锁
(1)互斥锁又叫互斥量(mutex)
(2)相关函数:pthread_mutex_init pthread_mutex_destroy
pthread_mutex_lock pthread_mutex_unlock
(3)互斥锁和信号量的关系:可以认为互斥锁是一种特殊的信号量
(4)互斥锁主要用来实现关键段保护
5.2、用互斥锁来实现上节的代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
char buf[200] = {0};
pthread_mutex_t mutex;
unsigned int flag = 0;
// 子线程程序,作用是统计buf中的字符个数并打印
void *func(void *arg)
{
// 子线程首先应该有个循环
// 循环中阻塞在等待主线程**的时候,子线程被**后就去获取buf中的字符
// 长度,然后打印;完成后再次被阻塞
//while (strncmp(buf, "end", 3) != 0)
sleep(1);
while (flag == 0)
{
pthread_mutex_lock(&mutex);
printf("本次输入了%d个字符\n", strlen(buf));
memset(buf, 0, sizeof(buf));
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit(NULL);
}
int main(void)
{
int ret = -1;
pthread_t th = -1;
pthread_mutex_init(&mutex, NULL);
ret = pthread_create(&th, NULL, func, NULL);
if (ret != 0)
{
printf("pthread_create error.\n");
exit(-1);
}
printf("输入一个字符串,以回车结束\n");
while (1)
{
pthread_mutex_lock(&mutex);
scanf("%s", buf);
pthread_mutex_unlock(&mutex);
// 去比较用户输入的是不是end,如果是则退出,如果不是则继续
if (!strncmp(buf, "end", 3))
{
printf("程序结束\n");
flag = 1;
//exit(0);
break;
}
sleep(1);
// 主线程在收到用户收入的字符串,并且确认不是end后
// 就去发信号**子线程来计数。
// 子线程被阻塞,主线程可以**,这就是线程的同步问题。
// 信号量就可以用来实现这个线程同步
}
// 回收子线程
printf("等待回收子线程\n");
ret = pthread_join(th, NULL);
if (ret != 0)
{
printf("pthread_join error.\n");
exit(-1);
}
printf("子线程回收成功\n");
pthread_mutex_destroy(&mutex);
return 0;
}
注意:man 3 pthread_mutex_init时提示找不到函数,说明你没有安装pthread相关的man手册。安装方法:1、虚拟机上网;2、sudo apt-get install manpages-posix-dev
6.线程同步之条件变量
6.1、什么是条件变量
6.2、相关函数
pthread_cond_init pthread_cond_destroy
pthread_cond_wait pthread_cond_signal/pthread_cond_broadcast
6.3、使用条件变量来实现上节代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
char buf[200] = {0};
pthread_mutex_t mutex;
pthread_cond_t cond;
unsigned int flag = 0;
// 子线程程序,作用是统计buf中的字符个数并打印
void *func(void *arg)
{
// 子线程首先应该有个循环
// 循环中阻塞在等待主线程**的时候,子线程被**后就去获取buf中的字符
// 长度,然后打印;完成后再次被阻塞
//while (strncmp(buf, "end", 3) != 0)
//sleep(1);
while (flag == 0)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
printf("本次输入了%d个字符\n", strlen(buf));
memset(buf, 0, sizeof(buf));
pthread_mutex_unlock(&mutex);
//sleep(1);
}
pthread_exit(NULL);
}
int main(void)
{
int ret = -1;
pthread_t th = -1;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
ret = pthread_create(&th, NULL, func, NULL);
if (ret != 0)
{
printf("pthread_create error.\n");
exit(-1);
}
printf("输入一个字符串,以回车结束\n");
while (1)
{
//pthread_mutex_lock(&mutex);
scanf("%s", buf);
pthread_cond_signal(&cond);
//pthread_mutex_unlock(&mutex);
// 去比较用户输入的是不是end,如果是则退出,如果不是则继续
if (!strncmp(buf, "end", 3))
{
printf("程序结束\n");
flag = 1;
//exit(0);
break;
}
//sleep(1);
// 主线程在收到用户收入的字符串,并且确认不是end后
// 就去发信号**子线程来计数。
// 子线程被阻塞,主线程可以**,这就是线程的同步问题。
// 信号量就可以用来实现这个线程同步
}
// 回收子线程
printf("等待回收子线程\n");
ret = pthread_join(th, NULL);
if (ret != 0)
{
printf("pthread_join error.\n");
exit(-1);
}
printf("子线程回收成功\n");
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
6.4、线程同步总结