当前位置:网站首页>[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;
}
}
边栏推荐
- XXE&XML-外部实体注入-利用和绕过
- This point inside the function / change this point inside the function
- [nodejs] nodejs create a simple server
- [QNX hypervisor 2.2 user manual]9.7 generate
- Wrote a little webapi knowledge points from 0 to 1
- Vscode shortcut key: collapse and expand code
- Take away applet with main version of traffic / repair to add main access function of traffic
- Which securities firm is the best and safest for beginners to open an account
- 策略模式_
- TS function
猜你喜欢

Regular expression (user name form verification / verification of landline number / regular replacement)

Docker 安装 Redis-5.0.12(远程访问)
![[code case] blog page design (with complete source code)](/img/9e/0e7cab956515b9cc75a7567eb477d2.png)
[code case] blog page design (with complete source code)

Tencent map API request source is not authorized, this request source domain name

@Import

Duplicate numbers in array

多模态——Deep Multi-Modal Sets

电商RPA,大促轻松上阵的法宝

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

initializer_list工具库学习
随机推荐
ETL tool (data synchronization) II
XxE & XML external entity injection utilization and bypass
ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用
Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download
类和对象(3)
TS function
Deep and shallow copies
JS get the current date and time
MVVM model
@Import
1913. Maximum product difference between two number pairs - no sorting required
Which securities company should a novice choose to open an account? Is it safe?
Overview of MES system equipment management (Part 2)
图的遍历-DFS,BFS(代码详解)
utility实用组件学习之swap,move,forward,exchange
[testing technology automated testing pytest] basic summary of pytest
【JUC】并发需要了解的关键字volatile
Mongodb update operator (modifier)
加拿大EE通道
利用用户脚本优化 Yandere/Konachan 站点浏览体验