技术标签: c语言
在C语言中可以使用pthread.h的API来创建线程,pthread.h符合POSIX标准,意味可以在Unix和Linux下运行,Windows NT也提供了相应的支持
#include<stdio.h>
#include<pthread.h>
void *thread_func(void *args)
{
printf("This is thread_func");
return 0;
}
int main()
{
pthread_t thread;
if(pthread_create(&thread,NULL,thread_func,NULL))
{
printf("creat thread fail\n");
}
else
{
printf("creat thread successfull\n");
}
pthread_exit(NULL);
}
关于pthread_create函数,成功的时候返回的为0,失败的时候返回的是1
第一个参数为指向线程标识符的指针
第二个参数为线程属性
第三个参数为函数的起始地址
第四个参数为运行函数的参数,利用第四个参数可以在创建线程的时候向函数中传入参数
#include<stdio.h>
#include<pthread.h>
void *thread_func(void *parameter)
{
printf("This is thread_func,parameter:%s",parameter);
return 0;
}
int main()
{
pthread_t thread;
char* parameter="ok";
if(pthread_create(&thread,NULL,thread_func,(void *)parameter))
{
printf("creat thread fail\n");
}
else
{
printf("creat thread successfull\n");
}
pthread_exit(NULL);
}
#include<stdio.h>
#include<pthread.h>
#define THREAD_NUM 5
void *thread_func(void *parameter)
{
printf("This is thread_func,parameter:%d\n",parameter);
return 0;
}
int main()
{
pthread_t threads[THREAD_NUM];
for(int i=0;i<THREAD_NUM;i++)
{
if(pthread_create(&threads[i],NULL,thread_func,(void*)i))
{
printf("creat thread fail\n");
}
else
{
printf("creat thread successfull\n");
}
}
pthread_exit(NULL);
}
如果不使用上面的两个关键字,主线程结束的时候也会结束分线程,但是主线程一直不结束的时候,分线程分配的内存也不会不回收
使用pthread_join()关键字,是主线程和分线程连接上,等线程执行完成以后,就会回收分线程内存,继续主线程
其有两个参数,第一个参数为线程,第二个参数为线程的返回值
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
void *thread_function(void *arg) {
int i;
for ( i=0; i<8; i++) {
printf("Thread working...! %d \n",i);
sleep(1);
}
return NULL;
}
int main(void) {
pthread_t mythread;
if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {
printf("error creating thread.");
abort();
}
pthread_join ( mythread, NULL ) ;
printf("thread done! \n");
}
使用pthread_detach()关键字,将线程分离出去,主线程和分线程各跑各个的,互不相干,分线程结束后会自动回收内存
其有一个参数,参数为线程
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
void *thread_function(void *arg) {
int i;
for ( i=0; i<8; i++) {
printf("Thread working...! %d \n",i);
sleep(1);
}
return NULL;
}
int main(void) {
pthread_t mythread;
if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {
printf("error creating thread.");
abort();
}
pthread_detach ( mythread) ;
printf("thread done! \n");
}
pthread_exit (status)
文章浏览阅读94次。JenkinsJenkins is a self-contained, open source automation server which can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software.主要介绍使用Jenkins..._jenkins构建javaweb应用指定war包名
文章浏览阅读779次。ERNIESage是用在NLP的一个模型。UniMP是标签传播的一个模型https://aistudio.baidu.com/aistudio/education/lessonvideo/937035ERNIESageERNIE结构其实就是多层的transformer。_erniesage
文章浏览阅读6.7k次,点赞24次,收藏92次。小程序入门保姆级教程_微信小程序入门
文章浏览阅读559次。 这两年开始毕业设计和毕业答辩的要求和难度不断提升,传统的毕设题目缺少创新和亮点,往往达不到毕业答辩的要求,这两年不断有学弟学妹告诉学长自己做的项目系统达不到老师的要求。为了大家能够顺利以及最少的精力通过毕设,学长分享优质毕业设计项目,今天要分享的是 **基于深度学习猫狗分类 **学长这里给一个题目综合评分(每项满分5分)难度系数:3分工作量:3分创新点:3分。_毕业设计可以用猫狗大战吗
文章浏览阅读4.4k次,点赞2次,收藏16次。写这篇文章的由来是因为后边要用这个工具,但是由于某些原因有部分小伙伴和童鞋们可能不会安装此工具,为了方便小伙伴们和童鞋们的后续学习和不打击他们的积极性,因为80%的人都是死在工具的安装这第一道门槛上,这门槛说高也不高说低也不是太低。所以就抽时间水了这一篇文章。_eclipse安装教程
文章浏览阅读4.1w次,点赞12次,收藏193次。小编为大家收集了11个web前端开发,大企业实战项目案例+5W行源码!拿走玩去吧!1)小米官网项目描述:首先选择小米官网为第一个实战案例,是因为刚开始入门,有个参考点,另外站点比较偏向目前的卡片式设计,实现常见效果。目的为学者练习编写小米官网,熟悉div+css布局。学习资料的话可以加下web前端开发学习裙:600加上610再加上151自己去群里下载下。项目技术:HTML+CSS+Div布局2)迅雷官网项目描述:此站点特效较多,所以通过练习编写次站点,学生可以更多练习CSS3的新特性过渡与动画的实_前端项目实战案例
文章浏览阅读6.9k次,点赞4次,收藏7次。一、触发碰撞和刚体碰撞共性:碰撞双方都需要加碰撞器组件,至少有一个添加了刚体组件rigidbody;异同:触发碰撞需要在 碰撞器勾选is trigger选项,刚体碰撞需要使用物理动力模式(isKinematic 选项不能勾选)二、OnTriggerEnter事件执行特点;OnTriggerEnter事件不是每帧调用的,它是基于RigidBody物理碰撞后调用的,不管事件所在的脚本是..._ontriggerenter
文章浏览阅读5.6k次,点赞8次,收藏62次。已经有了腾讯云服务器如何搭建网站?腾讯云服务器网以腾讯云服务器,借助宝塔面板搭建Web环境,然后使用WordPress博客程序搭建网站,大致分为三步,首先购买腾讯云服务器,然后在腾讯云服务器上部署宝塔面板,最后在宝塔面板上安装WordPress程序。_腾讯云建站
文章浏览阅读956次。WebSphere MQ6.0 for redhat4.6 setup分类: WebSphere 2010-04-12 14:45650人阅读 评论(0)收藏举报websphereredhatmanageribm消息中间件statisticsWebsphere MQ是IBM的商业消息中间件(Commercial Messaging Middlewar_websphere mq6.0下载
文章浏览阅读1.1w次,点赞11次,收藏49次。目录一、Bean 的生命周期二、代码演示三、主要步骤简述一、Bean 的生命周期 对于普通的 Java 对象,new 的时候会去创建对象,而当它没有任何引用的时候则被垃圾回收机制回收。相较于前者,由Spring IoC 容器托管的对象,它们的生命周期完全由容器控制。Spring 中每个 Bean 的生命周期如下:对于 ApplicationContext 容器,当容器启动结束后,实例化所有的 Bean。设置对象属性,即依赖注入,动态将依赖关系注入到对象中。紧接着,Spring 会检测该对象_spring bean的生命周期
文章浏览阅读94次。为什么logical==0?_matlab fat
文章浏览阅读302次。一. HTML标签分类1.根据标签个数分类。 单标签:只有一个标签。 <br>, <hr>,<img>,<meta>, 实现一个特定的功能。 双标签:既有开始标签,也有结束标签。 Html,head,Body,title,h1~h6,p,a,ul,li,ol,strong,em。2.根据标签特性分类(网页效果)。 2.1行属性..._ol是单标记还是双标记