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

企业网络推广平台公司沈阳百度seo

企业网络推广平台公司,沈阳百度seo,wordpress手机没搜索,乌鲁木齐电商网站建设文章目录 线程ID及进程地址空间布局线程局部存储 线程ID及进程地址空间布局 pthread_ create函数会产生一个线程ID,存放在第一个参数指向的地址中。 该线程ID和前面说的线程ID不是一回事。 前面讲的线程ID属于进程调度的范畴。 ​ 因为线程是轻量级进程&#xff…

文章目录

      • 线程ID及进程地址空间布局
        • 线程局部存储

线程ID及进程地址空间布局

  1. pthread_ create函数会产生一个线程ID,存放在第一个参数指向的地址中。

    该线程ID和前面说的线程ID不是一回事。

  2. 前面讲的线程ID属于进程调度的范畴。

​ 因为线程是轻量级进程,是操作系统调度器的最小单位,

​ 所以需要一个数值来唯一表示该线程。

​ 用户级线程+内核轻量级进程=Linux线程

​ 线程:用户级线程、内核级线程

Linux线程就是用户级线程

​ 用户级执行流:内核lwp = 1:1

  1. pthread_ create函数第一个参数指向一个虚拟内存单元,

​ 该内存单元的地址即为新创建线程的线程ID,属于NPTL线程库的范畴。

​ 线程库的后续操作,就是根据该线程ID来操作线程的。

  1. 线程库NPTL提供了pthread_ self函数,可以获得线程自身的ID:
pthread_t pthread_self(void);

pthread_self

#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <thread>
#include <string>
#include <cstdlib>using namespace std;string tohex(pthread_t tid)
{char hex[64];snprintf(hex, sizeof(hex), "%p", tid);return hex;
}void *threadroutine(void *args)
{while (1){cout << "thread id: " << tohex(pthread_self()) << endl;sleep(1);}
}int main()
{pthread_t tid;pthread_create(&tid, nullptr, threadroutine, (void *)"thread 1");cout << "main thread create thread done,new thread id: " << tohex(tid) << endl;pthread_join(tid, nullptr);return 0;
}

image-20250429132415068

pthread_t 到底是什么类型呢?取决于实现。

对于Linux目前实现的NPTL实现而言,pthread_t类型的线程ID,

本质就是一个进程地址空间上的一个地址。

image-20250425230334017

一个执行流本质就是一条调用链。

调用函数就是在栈帧中为该函数形成栈帧结构。

为了完成临时变量空间在栈帧中的开辟和释放。

验证创建多线程:

makefile

mythread:mythread.ccg++ -o $@ $^ -std=c++11 -lpthread
.PHONY:clean
clean:rm -rf mythread

mythread.cc

#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <cstring>
#include <cstdlib>
#include <vector>using namespace std;#define NUM 5struct threadData
{string tname;
};string toHex(pthread_t tid)
{char buffer[1024];snprintf(buffer, sizeof(buffer), "0x%x", tid);return buffer;
}void InitThreadData(threadData *td, int i)
{td->tname = "thread-" + to_string(i);
}void *threadRoutine(void *args)
{threadData *td = static_cast<threadData *>(args);int i = 0;while (i < 5){cout << "pid: " << getpid() << " ,tid: " << toHex(pthread_self()) << " ,name: " << td->tname << endl;sleep(1);i++;}delete td;return nullptr;
}int main()
{vector<pthread_t> tids;for (int i = 0; i < NUM; i++){pthread_t tid;threadData *td = new threadData;// 使用堆空间 而且每次都是新的堆空间 堆空间是共享的 访问的是堆空间的不同位置// 我们让一个线程访问对应的一个堆空间InitThreadData(td, i);pthread_create(&tid, nullptr, threadRoutine, td);tids.push_back(tid);sleep(1);}for (int i = 0; i < NUM; i++){pthread_join(tids[i], nullptr);}return 0;
}

image-20250505145912944

验证每个线程都有独立的栈结构:

void *threadRoutine(void *args)
{int test_i=0;threadData *td = static_cast<threadData *>(args);int i = 0;while (i < 5){cout << "pid: " << getpid() << " ,tid: " << toHex(pthread_self()) << " ,name: " << td->tname <<" ,test_i: "<<test_i<<" ,&test_i: "<<toHex((pthread_t)&test_i)<< endl;sleep(1);i++,test_i++;}delete td;return nullptr;
}

image-20250505151208228

验证每个线程的栈数据是可以被访问的(但是不建议):

#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <cstring>
#include <cstdlib>
#include <vector>using namespace std;#define NUM 5int*p;struct threadData
{string tname;
};string toHex(pthread_t tid)
{char buffer[1024];snprintf(buffer, sizeof(buffer), "0x%x", tid);return buffer;
}void InitThreadData(threadData *td, int i)
{td->tname = "thread-" + to_string(i);
}void *threadRoutine(void *args)
{int test_i=0;threadData *td = static_cast<threadData *>(args);int i = 0;if(td->tname=="thread-0"){p=&test_i;}while (i < 5){cout << "pid: " << getpid() << " ,tid: " << toHex(pthread_self()) << " ,name: " << td->tname <<" ,test_i: "<<test_i<<" ,&test_i: "<<&test_i<< endl;sleep(1);i++,test_i++;}delete td;return nullptr;
}int main()
{vector<pthread_t> tids;for (int i = 0; i < NUM; i++){pthread_t tid;threadData *td = new threadData;// 使用堆空间 而且每次都是新的堆空间 堆空间是共享的 访问的是堆空间的不同位置// 我们让一个线程访问对应的一个堆空间InitThreadData(td, i);pthread_create(&tid, nullptr, threadRoutine, td);tids.push_back(tid);// sleep(1);}sleep(1);//确保赋值成功cout<<"main thread get a thread local value,val: "<<*p<<" ,&val: "<<p<<endl;for (int i = 0; i < NUM; i++){pthread_join(tids[i], nullptr);}return 0;
}

image-20250505155535503

线程与线程之间几乎没有秘密

线程栈上的数据,也是可以被其他线程看到并访问的。

线程局部存储
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <cstring>
#include <cstdlib>
#include <vector>using namespace std;#define NUM 5int g_val=0;struct threadData
{string tname;
};string toHex(pthread_t tid)
{char buffer[1024];snprintf(buffer, sizeof(buffer), "0x%x", tid);return buffer;
}void InitThreadData(threadData *td, int i)
{td->tname = "thread-" + to_string(i);
}void *threadRoutine(void *args)
{threadData *td = static_cast<threadData *>(args);int i = 0;while (i < 5){cout << "pid: " << getpid() << " ,tid: " << toHex(pthread_self()) << " ,name: " << td->tname <<" ,g_val: "<<g_val<<" ,&g_val: "<<&g_val<< endl;sleep(1);i++,g_val++;}delete td;return nullptr;
}int main()
{vector<pthread_t> tids;for (int i = 0; i < NUM; i++){pthread_t tid;threadData *td = new threadData;InitThreadData(td, i);pthread_create(&tid, nullptr, threadRoutine, td);tids.push_back(tid);// sleep(1);}for (int i = 0; i < NUM; i++){pthread_join(tids[i], nullptr);}return 0;
}

image-20250505160424955

全局变量是被所有的线程同时看到并访问的。

如果线程想要一个私有的全局变量呢??

__thread+变量
__thread int g_val=0;

image-20250505161121658

编译器提供的编译选项

__thread threadData td;

image-20250505161425635

__thread只能用来定义内置类型,不能用来定义自定义类型

作用:可以存储该线程的某些系统调用数据(就不需要频繁调用的系统调用了)。

__thread unsigned int number=0;
__thread int pid=0;struct threadData
{string tname;
};void *threadRoutine(void *args)
{pid=getpid();number=pthread_self();threadData *td = static_cast<threadData *>(args);int i = 0;while (i < 5){cout<<"pid: "<<pid<<" ,tid: "<<number<<endl;sleep(1);i++,g_val++;}delete td;return nullptr;
}

image-20250505162435763

在调用链上,可以直接获取该线程的pid、tid等。(线程级别的全局变量而且互不干扰)

http://www.cadmedia.cn/news/9445.html

相关文章:

  • 网络规划与设计师真实通过率seo搜索引擎营销工具
  • 水木网站建设徐州网页关键词优化
  • 企业建站seo公司资源
  • 职教集团网站建设怎么样建一个网站
  • 江苏省建设厅纪委网站免费打广告网站
  • 网站管理制度建设的必要性品牌营销是什么
  • 政府网站开发文档南宁seo结算
  • 装修设计效果图网站广告投放运营主要做什么
  • 百捷网站建设工资百度云登陆首页
  • 网站空间是指什么网站运营是做什么的
  • 佛山设计论坛seo线上培训机构
  • 建设游戏运营网站开展工作内容孝感seo
  • 免费网站建站+凡科建站百度网站的优化方案
  • 厦门广告公司电话武汉网站推广优化
  • 机械网站建设网店推广联盟
  • 河南移动官网网站建设搜狗seo优化
  • 学建设网站上海百度公司总部
  • 企业做网站的发票怎么记账关键词百度指数查询
  • 建设路小学查分网站百度热搜seo
  • 棋牌推广搜索引擎优化自然排名
  • 营口门户网站建设上海最新新闻热点事件
  • 网站链接提交收录北京做网站公司哪家好
  • 家具公司网站页面设计模板搜索引擎是指什么
  • 深圳网站设计深圳设计公司网络推广运营
  • 为什么要建立网站哪家网站推广好
  • 电子商务网站建设实训步骤网站设计平台
  • 哔哩哔哩免费网站观看户外广告
  • 重庆建网站诚选快忻科技悉心海淀区seo搜索引擎
  • 做团购网站有什么难处免费建站平台哪个好
  • 曲阜官方建设局网站苏州网站优化排名推广