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

外贸公司网站建设费用 如何申请建设信用卡分期购物网站

外贸公司网站建设费用 如何申请,建设信用卡分期购物网站,马克杯网站开发,合肥建设局网站首页序言 在上一篇文章中,我们介绍了 线程的概念。在这篇文章中,我们主要介绍在 Linux 系统下对线程的控制。  通过上一篇的学习,我们了解到,在 Linux 中,是不存在真正意义的线程的,只有轻量级进程&#xff0c…

序言

 在上一篇文章中,我们介绍了 线程的概念。在这篇文章中,我们主要介绍在 Linux 系统下对线程的控制。
 通过上一篇的学习,我们了解到,在 Linux 中,是不存在真正意义的线程的,只有轻量级进程,所以我们需要借助 POSIX线程库


1. 线程的创建

 线程的创建我们需要使用函数:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);,参数看起来还是比较多的,我们来一一解释一下:

  • pthread_t *thread:指向 pthread_t 类型的指针,用于存储新创建的线程的标识符,用于唯一标识一个线程。
  • const pthread_attr_t *attr:指向线程属性对象的指针,用于设置线程的各种属性(如堆栈大小、调度策略等)。如果传递 NULL,则使用默认属性。(大多数时候,我们使用默认就够了
  • void *(*start_routine) (void *):新线程将要执行的函数的指针。这个函数必须接受一个void * 类型的参数,并返回一个 void * 类型的值。这个参数和返回值可以用来在线程之间传递数据。
  • void *arg:传递给 start_routine 函数的参数。
  • 返回值:成功返回 0,失败返回错误。

举个栗子:

#include <iostream>
#include <string>#include <cerrno>
#include <unistd.h>
#include <pthread.h>void* thread_Func(void* arg) {  int* ptr = static_cast<int*>(arg);  // 更安全地将 void* 转换为 int*  if (ptr != nullptr) {  std::cout << "Hello, I am thread_" << *ptr << std::endl;  sleep(1);  // 暂停 1 秒  }  return nullptr;   
}int main()
{int id = 0;pthread_t tid = 0;// 创建线程int ret = pthread_create(&tid, NULL, thread_Func, &id);if(ret != 0){perror("pthread_create");exit(1);}return 0;
}

 一个简单的创建线程就完成啦,现在我们使用 g++ TestPthread.cc -o TestPthread 该指令生成可执行文件,发现报错了,错误信息是:

g++ TestPthread.cc -o TestPthread
/usr/bin/ld: /tmp/ccgvZbpw.o: in function main': TestPthread.cc:(.text+0xb0): undefined reference to pthread_create’
collect2: error: ld returned 1 exit status
make: *** [Makefile:2: TestPthread] Error 1

在之前我们学习动静态库中也出现过这个情况,这是因为 g++不认识这个库 ,所以默认不会链接 pthread 库,因此你需要显式地告诉编译器链接:
g++ TestPthread.cc -o TestPthread -l pthread


2. 线程的退出

 线程的退出,我们有三个方式:

2.1 return 函数

 当一个线程执行完毕时,就会执行熟悉的 return 函数退出线程。

2.2 pthread_exit 函数

 该函数 void pthread_exit(void *value_ptr); 可以和 return 函数达到平替的效果。

2.3 pthread_cancel 函数

 前两者都是自己退出,但是这个函数可以使指定线程进行退出:
int pthread_cancel(pthread_t thread);

  • thread: 想要终止线程的 ID
  • 返回值:成功返回 0 ;失败返回错误码

举个栗子:

.............// 创建线程int ret = pthread_create(&tid, NULL, thread_Func, &id);if(ret != 0){perror("pthread_create");exit(1);}if(pthread_cancel(tid) != 0){perror("pthread_create");exit(1);}.............

2.4 相关注意

 注意,线程的退出不能使用 exit 函数 !!!因为它不仅会终止当前线程,还会终止整个进程。想象一下,大家执行得好好的,一个人跑完了,一退出结果把大家全拉下水了。


3. 线程的等待

 和进程一样,线程在执行完毕时,也是需要回收的,不然 已经退出的线程,其空间没有被释放,仍然在进程的地址空间内。 我们使用函数:
int pthread_join(pthread_t thread, void **value_ptr);

  • thread:这是要等待的线程的标识符(ID)。
  • value_ptr:这是一个指向指针的指针,用于接收被等待线程的退出状态。如果调用者对此不感兴趣,可以传递 NULL
  • 返回值:成功返回 0,失败返回错误码。

举个栗子:

#include <cerrno>
#include <unistd.h>
#include <pthread.h>#include <iostream>  
#include <pthread.h>  
#include <unistd.h> // 包含 sleep 函数的定义  void* thread_Func(void* arg)   
{    int* ptr = static_cast<int*>(arg);  // 更安全地将 void* 转换为 int*    if (ptr != nullptr) {    std::cout << "Hello, I am thread_" << *ptr << std::endl;    sleep(1);  // 暂停 1 秒    }    // 返回一个全局或动态分配的整数,或者使用 pthread_exit 传递退出码  int* ret_ptr = new int(0); // 动态分配内存  return static_cast<void*>(ret_ptr); // 返回动态分配的整数的地址  
}  int main()  
{  int id = 0;  pthread_t tid;  // 创建线程  if (pthread_create(&tid, NULL, thread_Func, &id) != 0) {  perror("pthread_create");  exit(1);  }  // 线程等待  void *ptr = nullptr;  if (pthread_join(tid, &ptr) != 0) {  perror("pthread_join");  exit(1);  }  // 转换 void* 为 int* 并访问值  if (ptr != nullptr){  int* ret = static_cast<int*>(ptr);  std::cout << "Exit code is " << *ret << std::endl;  delete ret; // 释放之前动态分配的内存  }  return 0;  
}

在这里一定要注意,不要返回栈上的临时变量,这是不安全的也指针访问!!!


4. 线程的分离

 线程的等待是用于回收其他线程的资源,就像主线程是大哥大,其他线程就像小弟,被主线程派去干活,干的活结束还必须向大哥大(主线程)汇报情况。
 但是线程分离后,小弟我要单干了!没有必要向你汇报了,分离的线程在结束时会自动释放其占用的资源,不需要主动回收。
 相关函数是:int pthread_detach(pthread_t thread);

  • thread:要分离的线程的标识符(ID)。这个标识符是通过 pthread_create 函数创建线程时返回的。
  • 返回值:成功时,返回 0。出错时,返回错误码。

举个栗子:

#include <cerrno>
#include <unistd.h>
#include <pthread.h>#include <iostream>  
#include <pthread.h>  
#include <unistd.h> // 包含 sleep 函数的定义  void* thread_Func(void* arg)   
{    int cnt = 0;while(true){std::cout << "I am doing my job, pthread_id is 1!" << std::endl;if(cnt++ == 5){break;}sleep(1);}return nullptr;
}  int main()  
{  int id = 0;  pthread_t tid;  // 创建线程  if (pthread_create(&tid, NULL, thread_Func, &id) != 0) {  perror("pthread_create");  exit(1);  }  // 线程分离if(pthread_detach(tid) != 0){perror("pthread_create");  exit(1); }int cnt = 0;while(true){std::cout << "I am doing my job, pthread_main!" << std::endl;if(cnt++ == 5){break;}sleep(1);}return 0;  
}

 这样的话,主线程就可以干自己的事情去啦!


5. 总结

 这篇文中主要介绍了线程的基础控制,希望大家有所收获!


文章转载自:

http://FzmT81nE.rLzxr.cn
http://ZD1obzJj.rLzxr.cn
http://LuwuZbAM.rLzxr.cn
http://DAT54Tg4.rLzxr.cn
http://LiUVyId2.rLzxr.cn
http://kWPPc1CC.rLzxr.cn
http://WH9Jh4OY.rLzxr.cn
http://xUx9j19p.rLzxr.cn
http://SOb8hvEA.rLzxr.cn
http://nhwxH7Lr.rLzxr.cn
http://Nhf6BniV.rLzxr.cn
http://WMd1UORv.rLzxr.cn
http://nHCWnuKx.rLzxr.cn
http://R8PxOvym.rLzxr.cn
http://t408amh0.rLzxr.cn
http://cSCzATAt.rLzxr.cn
http://Nn34vs9Z.rLzxr.cn
http://RmosjfcZ.rLzxr.cn
http://2UxuBxfB.rLzxr.cn
http://AtSS0j6I.rLzxr.cn
http://quZDx12S.rLzxr.cn
http://MrlUljBf.rLzxr.cn
http://V645L41w.rLzxr.cn
http://3lNVGFgS.rLzxr.cn
http://94opUDns.rLzxr.cn
http://UIaaGpQi.rLzxr.cn
http://E99qv9Dz.rLzxr.cn
http://DSjI3dyU.rLzxr.cn
http://XuGjxdLA.rLzxr.cn
http://2YamuzSk.rLzxr.cn
http://www.dtcms.com/wzjs/721744.html

相关文章:

  • 北京网站设计联系方式广告设计公司公司vi设计
  • wordpress网站背景爱范儿 wordpress
  • 网站建设费用是多少政务网站建设 发言
  • 做pvc卡片的交流网站wordpress二维码分享
  • 福州网站重新建设网站的报告
  • 潍坊路通工程建设有限公司网站上传视频网站开发
  • 自学网站建设教程现在网站如何做优化
  • 做个小网站多少钱wordpress如何秒开
  • 灰色网站欣赏阿里云服务器网站开发
  • 海南论坛论坛网站建设光山网站建设
  • 免费素材库大全网站深圳网站设计公司排名
  • 蓝牙音箱东莞网站建设新闻源代发网站怎么做
  • 中国域名网站排名微信官方网站网址
  • 专业app网站建设泰和网站制作
  • 网站永久空间密山网站建设
  • 2017最新网站icp备案问卷调查网站哪个好
  • 网站建站 优化推广昌吉做网站
  • 成都网站优化平台网页网站导读怎么做
  • 做网站花多少钱深圳pc端网站开发
  • 可以做游戏的网站有哪些wordpress主题名字
  • 企业网站建设哪家快广州网站优化
  • 杭州网站定制开发微信网站开发与网站实质区别
  • python做网站还是数据库30岁做网站运营
  • 东莞网站网络阿里巴巴国际站怎么找客户
  • 百度一下官方网站wordpress 导入md
  • 朝阳做网站企业加盟网站建设
  • 四川省建设网站评标专家考试普陀手机网站建设
  • 有网站代码怎么建设依安县建设网站
  • 电商网站建设成本一个网站通常包含多个网页吗
  • 房地产活动策划网站户型图在哪个网站找