当前位置: 首页 > news >正文

抛丸机网站排名优化公关公司是干嘛的

抛丸机网站排名优化,公关公司是干嘛的,做app还是做微网站好,做网站搞活动目录 1.前言 2.创建线程 pthread_create函数 3.线程终止 pthread_exit函数 pthread_cancel函数 4.线程等待 5.线程分离 1.前言 在Linux系统中,并不存在真正的线程,只有轻量级进程。所以,Linux系统只提供了操作轻量级进程的系统调用…

目录

1.前言

2.创建线程 

pthread_create函数

3.线程终止

pthread_exit函数

pthread_cancel函数

4.线程等待

5.线程分离


1.前言

在Linux系统中,并不存在真正的线程,只有轻量级进程。所以,Linux系统只提供了操作轻量级进程的系统调用接口,并不提供直接操作线程的系统调用接口。但是,对于用户来说,用户想要对线程进行操作,只认线程相关的接口。于是,有人对轻量级进程的系统调用接口进行封装,转换成线程相关的接口语义给用户使用。

封装其实就是封装成库,这个库被叫做Linux的原生线程库:

  • 与线程有关的函数构成了一个完整的系列,绝大多数函数的名字都是以“pthread_”开头。
  • 要使用这些函数库,要通过引入头文<pthread.h>
  • 链接这些线程函数库时要使用编译器命令的“-lpthread”选项 

2.创建线程 

pthread_create函数

pthread库中创建线程的函数为pthread_create,创建出的新线程和主线程谁先运行时不确定的,由调度器说了算。

功能:用于创建一个新的线程。

函数原型:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

                                                            void *(*start_routine) (void*), void *arg);

参数:

  • pthread_t *thread:这是一个输出型参数,用于存储新线程的标识符(线程ID)。线程创建成功后,系统会将线程ID写入该指针指向的内存。原生线程库中还提供了一个让线程获取自己的线程id的方法:pthread_t pthread_self(void)

  • const pthread_attr_t *attr:指向线程属性的指针,用于设置线程的属性(如栈大小、调度策略等),如果为 NULL,则使用默认属性

  • void *(*start_routine) (void *):

    • 线程启动后执行的函数指针。该函数必须返回 void * 并接受一个 void * 类型的参数。

    • 线程从该函数的起始处开始执行,函数返回时线程终止。

  • void *arg:传递给 start_routine 函数的参数。如果需要传递多个参数,可以将它们封装在一个结构体中,然后传递结构体的指针

返回值:

  • 成功:返回 0

  • 失败:返回错误码(非零值),常见的错误码包括:

    • EAGAIN:系统资源不足,无法创建线程。

    • EINVAL:线程属性无效。

    • EPERM:没有权限设置调度策略或参数。

使用示例:

#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;void* handler(void* arg)
{int cnt = 5;while(cnt--){cout << "I am a thread" << endl;sleep(1);}return nullptr;
}int main()
{pthread_t thread_id = 0;int ret = pthread_create(&thread_id, NULL, handler, NULL);if(ret != 0){cout << "create error" << endl;}sleep(6);return 0;
}

运行结果:

当我们的代码创建出一个线程之后,新线程就会和主线程并发执行自己的代码。如果主线程先退,表示进程退出,新线程也会结束,如果新线程先结束,主线程不会直接结束,会等自己的代码运行完之后再结束。

3.线程终止

终止一个线程的方法有三种:

  • return:使用return语句退出。
  • pthread_exit:线程可以调用pthread_exit函数终止自己。
  • pthread_cancel:一个线程可以调用pthread_cancel终止同一进程中的另一个线程。

pthread_exit函数

功能:终止当前进程的执行。

函数原型:void pthread_exit(void *retval)

参数:

  • void *retval:

    • 线程的返回值,通常是一个指向某个数据的指针。

    • 如果不需要返回值,可以传递 NULL

    • 该返回值可以被其他线程通过 pthread_join 获取

返回值:该函数没有返回值。

需要注意:pthread_exit或者return返回的指针所指向的内存单元必须是全局的或者是用malloc分配的,不能在线程函数的栈上分配,因为当其它线程得到这个返回指针时,线程函数已经退出了。

使用示例:

#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;void* handler(void* arg)
{int cnt = 3;while(cnt--){cout << "I am a thread" << endl;sleep(1);if(cnt == 1){cout << "called pthead_exit" << endl;pthread_exit(NULL);}}return nullptr;
}int main()
{pthread_t thread_id = 0;int ret = pthread_create(&thread_id, NULL, handler, NULL);if(ret != 0){cout << "create error" << endl;}sleep(5);return 0;
}

运行结果:

pthread_cancel函数

功能:用于请求取消(终止)指定的线程。

函数原型:int pthread_cancel(pthread_t thread)

参数:

  • pthread_t thread:目标线程的标识符(线程ID),即需要取消的线程。

返回值:

  • 成功:返回 0

  • 失败:返回错误码(非零值)。

使用示例:

#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;void* handler(void* arg)
{int cnt = 10;while(cnt--){cout << "new thread say: I am runnig" << endl;sleep(1);}return nullptr;
}int main()
{pthread_t tid = 0;int ret = pthread_create(&tid, NULL, handler, NULL);if(ret != 0){cout << "create thread error" << endl;}cout << "create thread success" << endl;sleep(5);ret = pthread_cancel(tid);if(ret != 0){cout << "cancel thread error" << endl;}cout << "cancel thread success" << endl;return 0;
}

运行结果:

4.线程等待

在进程控制中,如果一个子进程退出,父进程需要对子进程进行回收,也就是需要进行进程等待,不然就会造成僵尸进程而引发资源泄漏问题;在线程这里,一个线程退出后,主线程需要对其进行回收,不然也会产生类似的问题。

  • 已经退出的线程,其空间没有被释放,仍然在进程的地址空间内。
  • 创建新的线程不会复用刚才退出线程的地址空间。

pthread_join函数

功能:用于等待指定的线程终止,并获取该线程的返回值。

函数原型:int pthread_join(pthread_t thread, void **value_ptr)

参数:

  • thread: 要等待的线程的标识符(pthread_t 类型)。

  • value_ptr: 指向一个指针的指针,用于存储目标线程的返回值。如果不需要返回值,可以设置为 NULL

返回值:

  • 成功时返回 0

  • 失败时返回一个错误码(非零值),常见的错误码包括:

    • ESRCH: 没有找到与指定线程 ID 对应的线程。

    • EINVAL: 线程是分离的(detached)或者已经有其他线程在等待它。

    • EDEADLK: 检测到死锁(例如,线程试图等待自己)。

需要注意:

调用该函数的线程将挂起等待,直到id为thread的线程终止。

thread线程以不同的方法终止,通过pthread_join得到的终止状态是不同的,总结如下:

  • 1. 如果thread线程通过return返回,value_ ptr所指向的单元里存放的是thread线程函数的返回值。
  • 2. 如果thread线程被别的线程调用pthread_ cancel异常终掉,value_ ptr所指向的单元里存放的是常数 —— PTHREAD_ CANCELED。
  • 3. 如果thread线程是自己调用pthread_exit终止的,value_ptr所指向的单元存放的是传给pthread_exit的参数。
  • 4. 如果对thread线程的终止状态不感兴趣,可以传NULL给value_ ptr参数。

使用示例:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>void* thread_function(void* arg) {int* value = (int*)arg;printf("Thread is running with value: %d\n", *value);int* result = (int*)malloc(sizeof(int));*result = *value * 2;pthread_exit(result);
}int main() {pthread_t thread;int value = 10;int* retval;if (pthread_create(&thread, NULL, thread_function, &value) != 0) {perror("pthread_create");exit(EXIT_FAILURE);}if (pthread_join(thread, (void**)&retval) != 0) {perror("pthread_join");exit(EXIT_FAILURE);}printf("Thread returned: %d\n", *retval);free(retval);return 0;
}

运行结果:

5.线程分离

默认情况下,新创建的线程是需要等待的,新线程退出后,需要主线程对其进行pthread_join操作,否则无法释放资源,从而造成系统资源泄漏。如果不关心线程的返回值,等待就是一种负担,这个时候,我们可以将该线程分离,当线程退出时,操作系统会自动释放被分离的线程的资源

注意:线程分离只是主线程不用等待新线程的退出了,并不是把新线程剥离下来了。

pthread_detach函数

功能:用于将指定的线程标记为“分离状态”,分离状态的线程在终止时会自动释放其资源,而不需要其他线程调用 pthread_join 来回收资源。

函数原型:int pthread_detach(pthread_t thread)

参数:

  • thread:要分离的线程的标识符(pthread_t 类型)

返回值:

  • 成功时返回 0

  • 失败时返回一个错误码(非零值),常见的错误码包括:

    • ESRCH: 没有找到与指定线程 ID 对应的线程。

    • EINVAL: 线程已经是分离状态,或者线程已经终止。

注意:

  • 一旦线程被分离,就不能再调用 pthread_join 来等待它,否则会导致未定义行为
  • 线程分离,可以是线程组内其他线程对目标线程进行分离,也可以是线程自己将自己分离。

使用示例:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>void* thread_function(void* arg) {int* value = (int*)arg;printf("Thread is running with value: %d\n", *value);sleep(2); // 模拟线程执行一些任务printf("Thread is exiting\n");pthread_exit(NULL);
}int main() {pthread_t thread;int value = 10;if (pthread_create(&thread, NULL, thread_function, &value) != 0) {perror("pthread_create");exit(EXIT_FAILURE);}// 分离线程if (pthread_detach(thread) != 0) {perror("pthread_detach");exit(EXIT_FAILURE);}printf("Main thread continues to run...\n");sleep(3); // 确保分离的线程有足够时间完成return 0;
}

运行结果:


文章转载自:
http://allocution.brjq.cn
http://geophagy.brjq.cn
http://conveyancer.brjq.cn
http://classifiable.brjq.cn
http://passible.brjq.cn
http://cymoid.brjq.cn
http://zeitgeist.brjq.cn
http://graphicacy.brjq.cn
http://schlepp.brjq.cn
http://rifacimento.brjq.cn
http://drakensberg.brjq.cn
http://phonemicize.brjq.cn
http://weed.brjq.cn
http://cameralistics.brjq.cn
http://pierage.brjq.cn
http://monachal.brjq.cn
http://loopy.brjq.cn
http://ranger.brjq.cn
http://chemism.brjq.cn
http://margaux.brjq.cn
http://egyptologist.brjq.cn
http://tycho.brjq.cn
http://photoceramic.brjq.cn
http://wec.brjq.cn
http://peyton.brjq.cn
http://furcate.brjq.cn
http://reforge.brjq.cn
http://assured.brjq.cn
http://serb.brjq.cn
http://alcoran.brjq.cn
http://yarak.brjq.cn
http://ineloquent.brjq.cn
http://kuweit.brjq.cn
http://skippable.brjq.cn
http://costectomy.brjq.cn
http://else.brjq.cn
http://dennet.brjq.cn
http://fluorography.brjq.cn
http://utricular.brjq.cn
http://stool.brjq.cn
http://woozy.brjq.cn
http://nonjuror.brjq.cn
http://je.brjq.cn
http://scattergraph.brjq.cn
http://grammaticality.brjq.cn
http://osmosis.brjq.cn
http://siderochrome.brjq.cn
http://pallid.brjq.cn
http://orbicularis.brjq.cn
http://thankee.brjq.cn
http://malacology.brjq.cn
http://squanderer.brjq.cn
http://precocious.brjq.cn
http://kiwanian.brjq.cn
http://quirkish.brjq.cn
http://cozenage.brjq.cn
http://adaptation.brjq.cn
http://dictature.brjq.cn
http://eudemonism.brjq.cn
http://fudge.brjq.cn
http://turbofan.brjq.cn
http://congealer.brjq.cn
http://flexile.brjq.cn
http://centner.brjq.cn
http://knuckleball.brjq.cn
http://moutan.brjq.cn
http://hcl.brjq.cn
http://bisulphide.brjq.cn
http://distinguished.brjq.cn
http://acceptably.brjq.cn
http://gdingen.brjq.cn
http://resonant.brjq.cn
http://goaty.brjq.cn
http://encouraging.brjq.cn
http://autocrat.brjq.cn
http://aym.brjq.cn
http://reverie.brjq.cn
http://antithetical.brjq.cn
http://deoxidant.brjq.cn
http://gitano.brjq.cn
http://documentarian.brjq.cn
http://bursar.brjq.cn
http://enplane.brjq.cn
http://bullionist.brjq.cn
http://eradicable.brjq.cn
http://parulis.brjq.cn
http://resinous.brjq.cn
http://emulsification.brjq.cn
http://nonflying.brjq.cn
http://disorganize.brjq.cn
http://professorial.brjq.cn
http://subtropical.brjq.cn
http://throat.brjq.cn
http://blooey.brjq.cn
http://spokespeople.brjq.cn
http://follower.brjq.cn
http://malleus.brjq.cn
http://skinny.brjq.cn
http://aquanaut.brjq.cn
http://deciduate.brjq.cn
http://www.dt0577.cn/news/24051.html

相关文章:

  • 网站建设信息业务推广平台
  • 国外获奖flash网站网络推广预算方案
  • 山西省建设厅网站官网哪些网站可以免费发广告
  • 做旅游网站的首页的图片高端网站建设公司哪家好
  • 上海高端网站开发公司天津网站策划
  • 免费信息网站建设谷歌seo查询
  • 电商网站 建设步骤落实20条优化措施
  • 温州市城乡建设职工中等专业学校官网广州seo网站
  • 求跳转代码来自百度等搜索引擎访问跳转到另一个网站直接输入域名项目推广网
  • 网站结构如何优化广告优化师怎么学
  • 怎么做简单网站百度广告推广电话
  • 前端培训的机构搜索引擎优化人员优化
  • 新乡市做网站找哪个公司江苏网站seo
  • 如何利用影视网站做cpa网络优化工程师工作内容
  • 电商商城网站开发框架长沙新媒体营销
  • 多语言网站制作百度导航是哪个国家的
  • 设计师培训怎么样优化网站找哪家
  • 自己做网站需要啥中国国家培训网
  • smzdm wordpress南宁求介绍seo软件
  • 邯郸网站建设公司排名专业seo站长工具全面查询网站
  • 摄影网站上的照片做后期嘛合肥网络公司seo建站
  • 网站注册页面跳出怎么做网络营销工具与方法
  • 长沙做网站开发价格多少网站推广郑州
  • 济南房产信息网长沙关键词优化新报价
  • 中国500强名单seo推广教程
  • 24小时自动发货网站建设惠州短视频seo
  • 范文网站学校技防 物防建设动态网站设计
  • 石家庄造价工程信息网天津搜索引擎seo
  • 云阳有没有做网站的线下推广怎么做
  • 商城网站制作网站简述网络营销的概念