Skip to content

Commit

Permalink
add rust concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
greathongtu committed Mar 30, 2024
1 parent 44abdea commit ebdea61
Show file tree
Hide file tree
Showing 31 changed files with 93 additions and 44 deletions.
8 changes: 4 additions & 4 deletions content/posts/linux-io.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tags = ['linux', 'io']
* 解决方法二:非阻塞忙轮询
```cpp
while(true) {
for i in [] {
for i in stream[] {
if(i has data) {
read/ other process
}
Expand All @@ -24,8 +24,8 @@ while(true) {
select:
```cpp
while(true) {
select([]); // 阻塞 max 1024个
for i in [] {
select(stream[]); // 阻塞 max 1024个
for i in stream[] {
if(i has data) {
read/ other process
}
Expand Down Expand Up @@ -169,4 +169,4 @@ main thread 只用来监听ListenFd 并 Accept,线程池负责ConnFds
过于理想化,需要很多核


模型五是最合适的
模型五是最合适的
2 changes: 1 addition & 1 deletion public/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down
15 changes: 14 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down Expand Up @@ -223,6 +223,19 @@





<section class="relative my-10 first-of-type:mt-0 last-of-type:mb-0">

<h2 class="!my-0 pb-1 font-bold !leading-none">Rust Concurrency</h2>
<time class="text-sm antialiased opacity-60"
>Mar 31, 2024</time
>
<a class="absolute inset-0 text-[0]" href="http://localhost:1313/posts/rust-concurrency/">Rust Concurrency</a>
</section>





<section class="relative my-10 first-of-type:mt-0 last-of-type:mb-0">
Expand Down
11 changes: 9 additions & 2 deletions public/index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
<description>Recent content on greathongtu 的 Blog</description>
<generator>Hugo -- gohugo.io</generator>
<language>en-us</language>
<lastBuildDate>Thu, 14 Mar 2024 19:42:12 +0800</lastBuildDate>
<lastBuildDate>Sun, 31 Mar 2024 01:39:55 +0800</lastBuildDate>
<atom:link href="http://localhost:1313/index.xml" rel="self" type="application/rss+xml" />
<item>
<title>Rust Concurrency</title>
<link>http://localhost:1313/posts/rust-concurrency/</link>
<pubDate>Sun, 31 Mar 2024 01:39:55 +0800</pubDate>
<guid>http://localhost:1313/posts/rust-concurrency/</guid>
<description>use std::thread; fn main() { // pass a closure let numbers = vec![1, 2, 3]; thread::spawn(move || { for n in numbers { println!(&amp;#34;{n}&amp;#34;); } }).join().unwrap(); let t1 = thread::spawn(f); println!(&amp;#34;Hello from the main thread.&amp;#34;); t1.join().unwrap(); } fn f(){ println!(&amp;#34;Hello from another thread!&amp;#34;); let id = thread::current().id(); println!(&amp;#34;This is my thread id: {id:?}&amp;#34;); } // scoped thread use std::thread; fn main() { let numbers = vec![1, 2, 3]; // argument s of the closure represents the scope.</description>
</item>
<item>
<title>React</title>
<link>http://localhost:1313/posts/react/</link>
Expand Down Expand Up @@ -48,7 +55,7 @@
<link>http://localhost:1313/posts/linux-io/</link>
<pubDate>Fri, 17 Nov 2023 01:04:58 +0800</pubDate>
<guid>http://localhost:1313/posts/linux-io/</guid>
<description>阻塞非阻塞 阻塞占用资源少,但是一个线程通过阻塞IO,只能处理一个请求。 非阻塞需要一直轮询,占用CPU资源大。&#xA;解决方法一:阻塞+多线程/多进程。 浪费资源 解决方法二:非阻塞忙轮询 while(true) { for i in [] { if(i has data) { read/ other process } } } 方法三:IO多路复用:既能阻塞等待,不浪费CPU资源,也能同一时刻监听多个IO请求的状态 select: while(true) { select([]); // 阻塞 max 1024个 for i in [] { if(i has data) { read/ other process } } } epoll:&#xA;while(true) { 可处理的流 = epoll_wait(epoll_fd);// 阻塞 max `cat /proc/sys/fs/file-max` 个 for i in 可处理的流[] { read/ other process } } select/ poll select 实现多路复用的方式是,将已连接的 Socket 都放到一个文件描述符集合,然后调用 select 函数将文件描述符集合拷贝到内核里,让内核来检查是否有网络事件产生,检查的方式很粗暴,就是通过遍历文件描述符集合的方式,当检查到有事件产生后,将此 Socket 标记为可读或可写, 接着再把整个文件描述符集合拷贝回用户态里,然后用户态还需要再通过遍历的方法找到可读或可写的 Socket,然后再对其处理。</description>
<description>阻塞非阻塞 阻塞占用资源少,但是一个线程通过阻塞IO,只能处理一个请求。 非阻塞需要一直轮询,占用CPU资源大。&#xA;解决方法一:阻塞+多线程/多进程。 浪费资源 解决方法二:非阻塞忙轮询 while(true) { for i in stream[] { if(i has data) { read/ other process } } } 方法三:IO多路复用:既能阻塞等待,不浪费CPU资源,也能同一时刻监听多个IO请求的状态 select: while(true) { select(stream[]); // 阻塞 max 1024个 for i in stream[] { if(i has data) { read/ other process } } } epoll:&#xA;while(true) { 可处理的流 = epoll_wait(epoll_fd);// 阻塞 max `cat /proc/sys/fs/file-max` 个 for i in 可处理的流[] { read/ other process } } select/ poll select 实现多路复用的方式是,将已连接的 Socket 都放到一个文件描述符集合,然后调用 select 函数将文件描述符集合拷贝到内核里,让内核来检查是否有网络事件产生,检查的方式很粗暴,就是通过遍历文件描述符集合的方式,当检查到有事件产生后,将此 Socket 标记为可读或可写, 接着再把整个文件描述符集合拷贝回用户态里,然后用户态还需要再通过遍历的方法找到可读或可写的 Socket,然后再对其处理。</description>
</item>
<item>
<title>wsl 代理问题</title>
Expand Down
2 changes: 1 addition & 1 deletion public/posts/cpp-concurrency/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down
2 changes: 1 addition & 1 deletion public/posts/go-design/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down
15 changes: 14 additions & 1 deletion public/posts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down Expand Up @@ -218,6 +218,19 @@



<section class="relative my-10 first-of-type:mt-0 last-of-type:mb-0">

<h2 class="!my-0 pb-1 font-bold !leading-none">Rust Concurrency</h2>
<time class="text-sm antialiased opacity-60"
>Mar 31, 2024</time
>
<a class="absolute inset-0 text-[0]" href="http://localhost:1313/posts/rust-concurrency/">Rust Concurrency</a>
</section>





<section class="relative my-10 first-of-type:mt-0 last-of-type:mb-0">

<h2 class="!my-0 pb-1 font-bold !leading-none">React</h2>
Expand Down
13 changes: 10 additions & 3 deletions public/posts/index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
<description>Recent content in Posts on greathongtu 的 Blog</description>
<generator>Hugo -- gohugo.io</generator>
<language>en-us</language>
<lastBuildDate>Thu, 14 Mar 2024 19:42:12 +0800</lastBuildDate>
<lastBuildDate>Sun, 31 Mar 2024 01:39:55 +0800</lastBuildDate>
<atom:link href="http://localhost:1313/posts/index.xml" rel="self" type="application/rss+xml" />
<item>
<title>Rust Concurrency</title>
<link>http://localhost:1313/posts/rust-concurrency/</link>
<pubDate>Sun, 31 Mar 2024 01:39:55 +0800</pubDate>
<guid>http://localhost:1313/posts/rust-concurrency/</guid>
<description>use std::thread; fn main() { // pass a closure let numbers = vec![1, 2, 3]; thread::spawn(move || { for n in numbers { println!(&amp;#34;{n}&amp;#34;); } }).join().unwrap(); let t1 = thread::spawn(f); println!(&amp;#34;Hello from the main thread.&amp;#34;); t1.join().unwrap(); } fn f(){ println!(&amp;#34;Hello from another thread!&amp;#34;); let id = thread::current().id(); println!(&amp;#34;This is my thread id: {id:?}&amp;#34;); } // scoped thread use std::thread; fn main() { let numbers = vec![1, 2, 3]; // argument s of the closure represents the scope.</description>
</item>
<item>
<title>React</title>
<link>http://localhost:1313/posts/react/</link>
<pubDate>Thu, 14 Mar 2024 19:42:12 +0800</pubDate>
<guid>http://localhost:1313/posts/react/</guid>
<description> jsx:component返回的jsx,看起来像html,实际是javascript,通过babel将jsx编译成javascript,这些js最后通过dom操作生产html。jsx融合了html,css,js三个。 props:是属性,是上级component向下级component传递的只读的信息 state:使用useState()返回一个初始值v和一个function setV。useState是一个react Hook。根据当前state更新新的state需要用lambda function UI = 很多components = f(state) Npx create-react-app@5 travel-list 运行:npm start </description>
<description> jsx:component返回的jsx,看起来像html,实际是javascript,通过babel将jsx编译成javascript,这些js最后通过dom操作生产html。jsx融合了html,css,js三个。 props:是属性,是上级component向下级component传递的只读的信息 state:使用useState()返回一个初始值v和一个function setV。useState是一个react Hook。根据当前state更新新的state需要用lambda function UI = 很多components = f(state) Npx create-react-app@5 travel-list 运行:npm install; npm start </description>
</item>
<item>
<title>Cpp Concurrency</title>
Expand Down Expand Up @@ -48,7 +55,7 @@
<link>http://localhost:1313/posts/linux-io/</link>
<pubDate>Fri, 17 Nov 2023 01:04:58 +0800</pubDate>
<guid>http://localhost:1313/posts/linux-io/</guid>
<description>阻塞非阻塞 阻塞占用资源少,但是一个线程通过阻塞IO,只能处理一个请求。 非阻塞需要一直轮询,占用CPU资源大。&#xA;解决方法一:阻塞+多线程/多进程。 浪费资源 解决方法二:非阻塞忙轮询 while(true) { for i in [] { if(i has data) { read/ other process } } } 方法三:IO多路复用:既能阻塞等待,不浪费CPU资源,也能同一时刻监听多个IO请求的状态 select: while(true) { select([]); // 阻塞 max 1024个 for i in [] { if(i has data) { read/ other process } } } epoll:&#xA;while(true) { 可处理的流 = epoll_wait(epoll_fd);// 阻塞 max `cat /proc/sys/fs/file-max` 个 for i in 可处理的流[] { read/ other process } } select/ poll select 实现多路复用的方式是,将已连接的 Socket 都放到一个文件描述符集合,然后调用 select 函数将文件描述符集合拷贝到内核里,让内核来检查是否有网络事件产生,检查的方式很粗暴,就是通过遍历文件描述符集合的方式,当检查到有事件产生后,将此 Socket 标记为可读或可写, 接着再把整个文件描述符集合拷贝回用户态里,然后用户态还需要再通过遍历的方法找到可读或可写的 Socket,然后再对其处理。</description>
<description>阻塞非阻塞 阻塞占用资源少,但是一个线程通过阻塞IO,只能处理一个请求。 非阻塞需要一直轮询,占用CPU资源大。&#xA;解决方法一:阻塞+多线程/多进程。 浪费资源 解决方法二:非阻塞忙轮询 while(true) { for i in stream[] { if(i has data) { read/ other process } } } 方法三:IO多路复用:既能阻塞等待,不浪费CPU资源,也能同一时刻监听多个IO请求的状态 select: while(true) { select(stream[]); // 阻塞 max 1024个 for i in stream[] { if(i has data) { read/ other process } } } epoll:&#xA;while(true) { 可处理的流 = epoll_wait(epoll_fd);// 阻塞 max `cat /proc/sys/fs/file-max` 个 for i in 可处理的流[] { read/ other process } } select/ poll select 实现多路复用的方式是,将已连接的 Socket 都放到一个文件描述符集合,然后调用 select 函数将文件描述符集合拷贝到内核里,让内核来检查是否有网络事件产生,检查的方式很粗暴,就是通过遍历文件描述符集合的方式,当检查到有事件产生后,将此 Socket 标记为可读或可写, 接着再把整个文件描述符集合拷贝回用户态里,然后用户态还需要再通过遍历的方法找到可读或可写的 Socket,然后再对其处理。</description>
</item>
<item>
<title>wsl 代理问题</title>
Expand Down
10 changes: 5 additions & 5 deletions public/posts/linux-io/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@


<meta name="description" content="阻塞非阻塞 阻塞占用资源少,但是一个线程通过阻塞IO,只能处理一个请求。 非阻塞需要一直轮询,占用CPU资源大。
解决方法一:阻塞&#43;多线程/多进程。 浪费资源 解决方法二:非阻塞忙轮询 while(true) { for i in [] { if(i has data) { read/ other process } } } 方法三:IO多路复用:既能阻塞等待,不浪费CPU资源,也能同一时刻监听多个IO请求的状态 select: while(true) { select([]); // 阻塞 max 1024个 for i in [] { if(i has data) { read/ other process } } } epoll:
解决方法一:阻塞&#43;多线程/多进程。 浪费资源 解决方法二:非阻塞忙轮询 while(true) { for i in stream[] { if(i has data) { read/ other process } } } 方法三:IO多路复用:既能阻塞等待,不浪费CPU资源,也能同一时刻监听多个IO请求的状态 select: while(true) { select(stream[]); // 阻塞 max 1024个 for i in stream[] { if(i has data) { read/ other process } } } epoll:
while(true) { 可处理的流 = epoll_wait(epoll_fd);// 阻塞 max `cat /proc/sys/fs/file-max` 个 for i in 可处理的流[] { read/ other process } } select/ poll select 实现多路复用的方式是,将已连接的 Socket 都放到一个文件描述符集合,然后调用 select 函数将文件描述符集合拷贝到内核里,让内核来检查是否有网络事件产生,检查的方式很粗暴,就是通过遍历文件描述符集合的方式,当检查到有事件产生后,将此 Socket 标记为可读或可写, 接着再把整个文件描述符集合拷贝回用户态里,然后用户态还需要再通过遍历的方法找到可读或可写的 Socket,然后再对其处理。" />
<meta name="author" content="greathongtu 的 Blog" />

Expand Down Expand Up @@ -104,7 +104,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down Expand Up @@ -233,7 +233,7 @@ <h1 class="!my-0 pb-2.5">Linux 网络I/O复用并发模型</h1>
<li>解决方法二:非阻塞忙轮询</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#66d9ef">while</span>(true) {
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">for</span> i in <span style="color:#960050;background-color:#1e0010"></span>[] {
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">for</span> i in stream[] {
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">if</span>(i has data) {
</span></span><span style="display:flex;"><span> read<span style="color:#f92672">/</span> other process
</span></span><span style="display:flex;"><span> }
Expand All @@ -244,8 +244,8 @@ <h1 class="!my-0 pb-2.5">Linux 网络I/O复用并发模型</h1>
select:</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#66d9ef">while</span>(true) {
</span></span><span style="display:flex;"><span> select(<span style="color:#960050;background-color:#1e0010"></span>[]); <span style="color:#75715e">// 阻塞 max 1024个
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span> <span style="color:#66d9ef">for</span> i in <span style="color:#960050;background-color:#1e0010"></span>[] {
</span></span><span style="display:flex;"><span> select(stream[]); <span style="color:#75715e">// 阻塞 max 1024个
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span> <span style="color:#66d9ef">for</span> i in stream[] {
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">if</span>(i has data) {
</span></span><span style="display:flex;"><span> read<span style="color:#f92672">/</span> other process
</span></span><span style="display:flex;"><span> }
Expand Down
2 changes: 1 addition & 1 deletion public/posts/map-reduce/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down
8 changes: 7 additions & 1 deletion public/posts/react/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down Expand Up @@ -257,6 +257,12 @@ <h1 class="!my-0 pb-2.5">React</h1>

<nav class="mt-24 flex rounded-lg bg-black/[3%] text-lg dark:bg-white/[8%]">

<a
class="flex w-1/2 items-center rounded-l-md p-6 pr-3 font-semibold no-underline hover:bg-black/[2%] dark:hover:bg-white/[3%]"
href="http://localhost:1313/posts/rust-concurrency/"
><span class="mr-1.5"></span><span>Rust Concurrency</span></a
>


<a
class="ml-auto flex w-1/2 items-center justify-end rounded-r-md p-6 pl-3 font-semibold no-underline hover:bg-black/[2%] dark:hover:bg-white/[3%]"
Expand Down
2 changes: 1 addition & 1 deletion public/posts/sql-summary/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down
2 changes: 1 addition & 1 deletion public/posts/vim/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down
2 changes: 1 addition & 1 deletion public/posts/wsl-network/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down
11 changes: 7 additions & 4 deletions public/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>http://localhost:1313/tags/frontend/</loc>
<lastmod>2024-03-14T19:42:12+08:00</lastmod>
</url><url>
<loc>http://localhost:1313/</loc>
<lastmod>2024-03-14T19:42:12+08:00</lastmod>
<lastmod>2024-03-31T01:39:55+08:00</lastmod>
</url><url>
<loc>http://localhost:1313/posts/</loc>
<lastmod>2024-03-31T01:39:55+08:00</lastmod>
</url><url>
<loc>http://localhost:1313/posts/rust-concurrency/</loc>
<lastmod>2024-03-31T01:39:55+08:00</lastmod>
</url><url>
<loc>http://localhost:1313/tags/frontend/</loc>
<lastmod>2024-03-14T19:42:12+08:00</lastmod>
</url><url>
<loc>http://localhost:1313/tags/react/</loc>
Expand Down
2 changes: 1 addition & 1 deletion public/tags/distributed-system/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down
2 changes: 1 addition & 1 deletion public/tags/editor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down
2 changes: 1 addition & 1 deletion public/tags/frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png" />


<meta name="generator" content="Hugo 0.123.8">
<meta name="generator" content="Hugo 0.124.1">



Expand Down
Loading

0 comments on commit ebdea61

Please sign in to comment.