当前位置:网站首页>[Muduo] thread package
[Muduo] thread package
2022-07-25 23:31:00 【CID( ͡ _ ͡ °)】
Open thread sem_t Semaphore
If say started This thread runs fast , It's time to start this thread , The main thread goes directly to sem_wait(&sem), Since the sub thread is being created, it has not yet reached sem_post(&sem), Signal resources have not been added 1, therefore sem_wait Semaphore resources are simply unavailable , The main thread will be blocked in sem_wait. As long as the sub thread is successfully created, it will execute until sem_post, Semaphore resources will increase 1, This is the time tid And then there is
#include <semaphore.h>
void Thread::start()
{
started_ = true;
sem_t sem;
sem_init(&sem,false,0);
// Open thread
thread_ = std::shared_ptr<std::thread>(new std::thread(
[&](){
...
sem_post(&sem); // Thread signal
...
}
));
// Here you have to wait to get the thread newly created above tid value
sem_wait(&sem); // Will block the main thread , Until the thread is created successfully
}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() // One Thread object , What is recorded is the details of a new thread
{
started_ = true;
sem_t sem;
sem_init(&sem,false,0);
// Open thread
thread_ = std::shared_ptr<std::thread>(new std::thread(
[&](){
// Get thread ID value
tid_ = CurrenThread::tid();
sem_post(&sem); // Thread signal
// Start a new thread , Specifically execute the thread function
func_();
}
));
// Here you have to wait to get the thread newly created above tid value
sem_wait(&sem); // Will block the main thread , Until the thread is created successfully
}
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;
}
}
边栏推荐
- Promise asynchronous callback function
- About priority queues
- Taobao Search case
- Overview of MES system equipment management (Part 2)
- [test technology performance test LoadRunner] detailed explanation of common functions of LoadRunner
- Mongodb update operator (modifier)
- 多模态——Deep Multi-Modal Sets
- Learning exploration-3d rotation card
- Es5 new method
- 数组中重复的数字
猜你喜欢

MVVM model

行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
![[Database Foundation] summary of MySQL Foundation](/img/89/e22c232b0183eaae35a9f45a40ff36.jpg)
[Database Foundation] summary of MySQL Foundation

类和对象(3)

Take away applet with main version of traffic / repair to add main access function of traffic

Taobao Search case

【MUDUO】EventLoop事件循环

图的遍历-DFS,BFS(代码详解)

Docker installation redis-5.0.12 (remote access)

utility实用组件学习之swap,move,forward,exchange
随机推荐
Practical skills of easyexcel
Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download
[QNX Hypervisor 2.2用户手册]9.8 load
Npm+ module loading mechanism
The new UI people help task help PHP source code with a value of 1500 / reward task Tiktok Kwai headline like source code / with three-level distribution can be packaged applet
学习探索-3d轮播卡片
【微信小程序】页面导航
Serialize addition, deletion, modification and query
Canada EE channel
Es5 new method
推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
POI special effects Market Research
PHP wechat scan code, follow official account and authorize login source code
谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
Dynamic memory management
Serialize data type
XxE & XML external entity injection utilization and bypass
npm+模块加载机制
多模态——Deep Multi-Modal Sets
Enterprise level inventory management system of code audit