一、Pre
1、安装VS Code?
①VS Code是开源免费软件,因此直接在VS Code官网下载对应版本的安装包就好
②目前VS Code支持Linux、Windows和MacOS,兼容性还是很好的
③在Linux下安装建议下载deb安装包后,在本地安装
④安装完成后,根据要开发的语言类型,安装对应的组件就可以
二、今天的内容
1、线程的属性
①pthread_attr:线程属性
线程属性设置,代码示例pthread_attr_setjoin.c
#include "func.h"
void* threadFunc(void *p)
{
printf("I am child thread\n");
pthread_exit(NULL);
}
int main()
{
pthread_t pthid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
pthread_create(&pthid,&attr,threadFunc,NULL);
int ret=pthread_join(pthid,NULL);
printf("I am main thread ret=%d\n",ret);
return 0;
}
②代码示例pthread_tworuning
#include "func.h"
void* threadFunc(void *p)
{
printf("I am child thread\n");
while(1);
pthread_exit(NULL);
}
int main()
{
pthread_t pthid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
pthread_create(&pthid,&attr,threadFunc,NULL);
while(1);
int ret=pthread_join(pthid,NULL);
printf("I am main thread ret=%d\n",ret);
return 0;
}
2、一些小技巧:
①在程序设计中锁应该尽可能的少
②如何查看线程?使用top命令后,按H就可以查看线程
③查看内存,使用free命令
3、5种编程模式
同步阻塞编程
多路复用编程
同步非阻塞编程
信号驱动编程
异步编程
三、本周总结