进程的进一步分化产生了线程,线程是更细粒度和更轻量级的进程
引入线程是为了更为精细粒度的分配CPU时间片,节省系统公共资源,更为充分和有效的配置有限运算能力
Tip: 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元。一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成。另外,线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的全部资源。一个线程可以创建和撤消另一个线程,同一进程中的多个线程之间可以并发执行。由于线程之间的相互制约,致使线程在运行中呈现出间断性。线程也有就绪、阻塞和运行三种基本状态。就绪状态是指线程具备运行的所有条件,逻辑上可以运行,在等待处理机;运行状态是指线程占有处理机正在运行;阻塞状态是指线程在等待一个事件(如某个信号量),逻辑上不可执行。每一个程序都至少有一个线程,若程序只有一个线程,那就是程序本身。线程是程序中一个单一的顺序控制流程。进程内一个相对独立的、可调度的执行单元,是系统独立调度和分派CPU的基本单位指运行中的程序的调度单位。在单个程序中同时运行多个线程完成不同的工作,称为多线程
有一个很形象的比喻:
多线程的问题是多个人同时吃一道菜的时候容易发生争抢,例如两个人同时夹一个菜,一个人刚伸出筷子,结果伸到的时候菜已经被夹走了。此时就必须等一个人夹一口之后,再让给另外一个人夹菜,也就是说资源共享就会发生冲突和争抢
Tip: 引自 《多线程有什么用》
这里分享一下我在学习线程过程中的笔记和心得
编写单进程多线程程序,用信号量实现一个线程A从标准终端输入一个0-99的整数,另外一个线程B将此数平方后打印输出,交替出现
thread.c
#include <stdio.h>
#include <semaphore.h> //sem_wait,sem_t,sem_post,sem_init 相关定义和声明都包含在内
#include <pthread.h> //pthread_create,pthread_join 相关声明都包含在内
#include <unistd.h> //getpid,getppid
sem_t alock,block; //定义两个信号量类型
int num=0;
void thread_a(void) //定义线程A的操作内容
{
do
{
sem_wait(&alock); //消费A锁
printf("please input a number(0-99):\n");
scanf("%d",&num); //获取输入数值
if(num < 0 || num > 99) //进行输入校验,如果超出范围,则进行提醒,并且将数值置零
{
printf("the number is out of range [0-99]:%d\nwe will set back to 0 as default\n",num);
num=0;
}
sem_post(&block); //释放B锁
}while(1);
}
void thread_b(void) //定义线程B的操作内容
{
do
{
sem_wait(&block); //消费B锁
printf("the sqr of %d is %d\n",num,num*num); //将数值和数值的平方进行打印
sem_post(&alock); //释放A锁
}while(1);
}
int main()
{
pthread_t ida=0,idb=0;
int ret=0,res=-1;
if( sem_init(&alock,0,1) || sem_init(&block,0,0)) //初始化两个信号量
{
perror("sem_init");
return res;
}
ret=pthread_create(&ida,NULL,(void *)thread_a,NULL); //创建线程A
ret+=pthread_create(&idb,NULL,(void *)thread_b,NULL); //创建线程B
if(ret != 0) //如果创建出错,则提醒并返回
{
perror("pthread_create");
return res;
}
else printf("two threads have been created\nthreada: %lu\nthreadb: %lu\npid:%d\nppid:%d\n",ida,idb,getpid(),getppid());
pthread_join(ida,NULL); //等待线程A退出
pthread_join(idb,NULL); //等待线程B退出
printf("return to main\n");
res=0;
return res;
}
emacs@ubuntu:~/c$ alias gtc
alias gtc='gcc -Wall -g -o'
emacs@ubuntu:~/c$ gtc thread.x thread.c -lpthread
emacs@ubuntu:~/c$ ./thread.x
two threads have been created
threada: 3078675312
threadb: 3070282608
pid:31391
ppid:30925
please input a number(0-99):
1
the sqr of 1 is 1
please input a number(0-99):
2
the sqr of 2 is 4
please input a number(0-99):
3
the sqr of 3 is 9
please input a number(0-99):
-1
the number is out of range [0-99]:-1
we will set back to 0 as default
the sqr of 0 is 0
please input a number(0-99):
100
the number is out of range [0-99]:100
we will set back to 0 as default
the sqr of 0 is 0
please input a number(0-99):
99
the sqr of 99 is 9801
please input a number(0-99):
^C
emacs@ubuntu:~/c$
编译执行过程中没有报错,从结果来看,符合预期
Note: 必须加上
-lpthread
参数,否则会因缺少库文件而报错
emacs@ubuntu:~/c$ gtc thread.x thread.c
/tmp/ccAdj40G.o: In function `thread_a':
/home/emacs/c/thread.c:17: undefined reference to `sem_wait'
/home/emacs/c/thread.c:25: undefined reference to `sem_post'
/tmp/ccAdj40G.o: In function `thread_b':
/home/emacs/c/thread.c:34: undefined reference to `sem_wait'
/home/emacs/c/thread.c:36: undefined reference to `sem_post'
/tmp/ccAdj40G.o: In function `main':
/home/emacs/c/thread.c:46: undefined reference to `sem_init'
/home/emacs/c/thread.c:46: undefined reference to `sem_init'
/home/emacs/c/thread.c:52: undefined reference to `pthread_create'
/home/emacs/c/thread.c:53: undefined reference to `pthread_create'
/home/emacs/c/thread.c:60: undefined reference to `pthread_join'
/home/emacs/c/thread.c:61: undefined reference to `pthread_join'
collect2: ld returned 1 exit status
emacs@ubuntu:~/c$ gtc thread.x thread.c -lpthread
emacs@ubuntu:~/c$
bits/pthreadtypes.h
中有关于 pthread_t 的定义
/* Thread identifiers. The structure of the attribute type is not
exposed on purpose. */
typedef unsigned long int pthread_t;
可知 pthread_t
其实是 unsigned long int
的别名
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。