当前位置:网站首页>【MUDUO】Thread封装
【MUDUO】Thread封装
2022-07-25 23:22:00 【CID( ͡ _ ͡°)】
开启线程 sem_t信号量
假如说started这个线程走的快,要开启这个线程了,主线程直接就到sem_wait(&sem),由于子线程在创建还没走到sem_post(&sem), 还没给信号资源加1,所以sem_wait根本就获取不到信号量资源,主线程就会被阻塞在sem_wait。子线程只要创建成功执行到sem_post,信号量的资源就会加1,这个时候tid就有了
#include <semaphore.h>
void Thread::start()
{
started_ = true;
sem_t sem;
sem_init(&sem,false,0);
//开启线程
thread_ = std::shared_ptr<std::thread>(new std::thread(
[&](){
...
sem_post(&sem); //线程信号
...
}
));
//这里必须要等待获取上面新创建的线程tid值
sem_wait(&sem); //会把主线程阻塞,直到线程创建成功
}Thread.h
#pragma once
#include "noncopyable.h"
#include <functional>
#include <thread>
#include <memory>
#include <unistd.h>
#include <atomic>
class Thread:noncopyable
{
public:
using ThreadFunc = std::function<void()>;
public:
explicit Thread(ThreadFunc, const std::string &name = std::string() );
~Thread();
void start();
void join();
bool started()const { return started_; }
pid_t pid() const {return tid_; }
const std::string &name() const {return name_; }
static int numCreated() { return numCreated_; }
private:
void setDautleName();
bool started_;
bool joined_;
std::shared_ptr<std::thread> thread_;
pid_t tid_;
ThreadFunc func_;
std::string name_;
static std::atomic_int numCreated_;
};
Thread.cc
#include "Thread.h"
#include "CurrenThread.h"
#include <semaphore.h>
std::atomic_int Thread::numCreated_(0);
Thread::Thread(ThreadFunc func, const std::string &name )
:started_(false)
,joined_(false)
,tid_(0)
,func_(std::move(func))
,name_(name)
{
setDautleName();
}
Thread::~Thread()
{
if(started_ && !joined_)
{
thread_->detach();
}
}
void Thread::start() //一个Thread对象,记录的就是一个新线程的详细信息
{
started_ = true;
sem_t sem;
sem_init(&sem,false,0);
//开启线程
thread_ = std::shared_ptr<std::thread>(new std::thread(
[&](){
//获取线程ID值
tid_ = CurrenThread::tid();
sem_post(&sem); //线程信号
//开启一个新的线程,专门执行该线程函数
func_();
}
));
//这里必须要等待获取上面新创建的线程tid值
sem_wait(&sem); //会把主线程阻塞,直到线程创建成功
}
void Thread::join()
{
joined_ = true;
thread_->join();
}
void Thread::setDautleName()
{
int num = ++numCreated_;
if(name_.empty())
{
char buf[32] = {0};
snprintf(buf,sizeof buf, "Thread %d", num);
name_ = buf;
}
}
边栏推荐
- @Import
- POI special effects Market Research
- 多模态——Deep Multi-Modal Sets
- Secure code warrior learning record (IV)
- WordPress removes the website publishing time
- Learning exploration-3d rotation card
- PHP binary array is sorted by a field in it
- Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download
- Classes and objects (3)
- chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
猜你喜欢

chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted

idea设置get、set模板解决boolean类型字段的命名问题

Dynamic memory management

Solve the problem phpstudy failed to import the database

Grain Academy p98 trample pit e.globalexceptionhandler: null
![[interface performance optimization] reasons for index failure and how to optimize SQL](/img/b9/64058c823c4497ac36bfb62a101816.jpg)
[interface performance optimization] reasons for index failure and how to optimize SQL

Idea sets get and set templates to solve the naming problem of boolean type fields

npm+模块加载机制

Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK

Tencent map API request source is not authorized, this request source domain name
随机推荐
【代码案例】博客页面设计(附完整源码)
About priority queues
CTS test method "suggestions collection"
Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download
1913. 两个数对之间的最大乘积差-无需排序法
新手开户选择哪个券商公司好呢?安全吗
Node Foundation
CTS测试方法「建议收藏」
WebMvcConfigurationSupport
ETL tool (data synchronization) II
@Import
js正则表达式匹配ip地址(ip地址正则表达式验证)
Yii2 behavior usage and its calling method
The difference between MySQL clustered index and non clustered index
E-commerce RPA, a magic weapon to promote easy entry
Wamp MySQL empty password
Serialize data type
[QNX hypervisor 2.2 user manual]9.7 generate
Implementation of mesh parameterized least squares conformal maps (3D mesh mapping to 2D plane)
Analysis of the influence of ESM direction finding error on positioning error