当前位置:网站首页>[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;
}
}
边栏推荐
- Classes and objects (2) (6 default member functions)
- XXE&XML-外部实体注入-利用和绕过
- Recommended system - an embedded learning framework for numerical features in CTR prediction
- Dynamic memory management
- How does PHP remove an element from an array based on the key value
- Wrote a little webapi knowledge points from 0 to 1
- Strategy mode_
- Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants
- 关于优先队列
- 行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
猜你喜欢

XxE & XML external entity injection utilization and bypass

Qt风格(QSS)应用之QProgressBar

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

MVVM model

numeric学习之iota,accumulate

Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download

Grain Academy p98 trample pit e.globalexceptionhandler: null

Data broker understanding

Source code of YY music wechat applet imitating Netease cloud music

PHP JSON variable array problem
随机推荐
Classes and objects (3)
行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
Query commodity cases (operate data with array addition method) / key points
chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
numeric学习之iota,accumulate
Data broker understanding
Node Foundation
[nodejs] nodejs create a simple server
Multimodal deep multi modal sets
Why are there many snapshot tables in the BI system?
[testing technology automated testing pytest] basic summary of pytest
npm+模块加载机制
Source code of YY music wechat applet imitating Netease cloud music
JS get the current date and time
Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants
Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
Take away applet with main version of traffic / repair to add main access function of traffic
类和对象(3)
PyTorch的数据输入格式要求及转换
This point inside the function / change this point inside the function