Skip to content

Commit

Permalink
v1.0.9.7
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomeng committed Sep 2, 2024
1 parent ecc1231 commit 9dcccb5
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ package-lock.json

# Misc
assets/js/dist

git.txt
.gitignore
116 changes: 115 additions & 1 deletion _posts/并发编程/2024-08-31-线程管理.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,118 @@ mermaid: true
int num(0);
std::thread t(&X::do_lengthy_work,&my_x, num);
```
# 转移线程所有权
# 转移线程所有权
主要是可以使用`std::move`转移已经创建的线程所有权
```cpp
void some_function();
void some_other_function();
std::thread t1(some_function); //创建一个线程t1
std::thread t2=std::move(t1); //将t1线程的所有权转移给t2
t1=std::thread(some_other_function); //t1重新指向一个新线程
std::thread t3; //创建一个新线程变量
t3=std::move(t2); //将t2所有线程权转移给t3
t1=std::mover(t3); //程序崩溃,t1此时有别的线程,不能赋值
```
函数返回`std::thread`对象
```cpp
std::thread f()
{
void some_function();
return std::thread(some_function);
}
std::thread g()
{
void some_other_function(int);
std::thread t(some_other_function,42);
return t;
}
```
当所有权也可以在函数内部传递,就允许`std::thread`实例可作为参数进行传递,例如:
```cpp
void f(std::thread t);
void g()
{
void some_funciton();
f(std::thread(some_function)); //线程作为参数传递
std::thread t(some_function);
f(std::move(t));
}
```
`std::thread`支持移动的好处是可以创建`thread_guard`类的实例,并且拥有其线程所有权。
可以用这个类来管理线程,例如:
```cpp
class joining_thread
{
std::thread t_;
public:
joining_thread(); noexcept=default;
template<typename Callable, typename ... Args>;
explicit joining_thread(Callable&& func, Args&& ... args):
t(std::forward<Callable>(func), std::forward<Args>(args)...)
{}
explict joining_thread(std::thread t) noexcept:
t_(std::move(t))
{}
joining_thread& operator=(joining_thread&& other) noexcept
{
if(joinable()){
join();
}
t_=std::move(other.t);
return *this;
}
joining_thread& operator=(std::thread other) noexcept
{
if(joinable())
{
join()
}
t_=std::move(other);
return *this;
}
~joining_thread() noexcept
{
if(joinable())
{
join();
}
}
void swap(joining_thread& other) noexcept
{
t_.swap(other.t);
}
std::thread::id get_id() const noexcept
{
return t_.get_id();
}
bool joinable() const noexcept
{
return t_.joinable();
}
void join()
{
t_.join();
}
void detach()
{
t_.detach();
}
std::thread& as_thread() noexcept
{
return t_;
}
const std::thread& as_thread() const noexcept
{
return t_;
}
};
```
# 标识线程
线程标识类型为`std::thread::id`,并可以通过两种方式进行检索。

第一种,可以通过调用`std::thread`对象的成员函数`get_id()`来直接获取。如果
`std::thread`对象没有与任何线程相关联,`get_id()`将返回`std::thread::type`
默认构造值,这个值表示“无线程”

第二种,在当前线程中调用`std::thread::get_id()`也可以获得线程标识

24 changes: 24 additions & 0 deletions _posts/机器人/2024-04-20-hello-world.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: Hello World
description: Examples of text, typography, math equations, diagrams, flowcharts, pictures, videos, and more.
author: cotes
date: 2024-04-20 11:33:00 +0800
categories: [Blogging, Demo]
tags: [typography]
pin: true
math: true
mermaid: true
---
<center>

## Hello,world!

</center>

## Hello,world!

<!-- markdownlint-capture -->
<!-- markdownlint-disable -->
# H1 - heading
{: .mt-4 .mb-0 }

0 comments on commit 9dcccb5

Please sign in to comment.