libuv

浏览器与Node的事件循环(Event Loop)有何区别?

99封情书 提交于 2020-02-29 15:49:45
前言 本文我们将会介绍 JS 实现异步的原理,并且了解了在浏览器和 Node 中 Event Loop 其实是不相同的。 一、线程与进程 1. 概念 我们经常说 JS 是单线程执行的,指的是一个进程里只有一个主线程,那到底什么是线程?什么是进程? 官方的说法是: 进程是 CPU 资源分配的最小单位;线程是 CPU 调度的最小单位 。这两句话并不好理解,我们先来看张图: 进程好比图中的工厂,有单独的专属自己的工厂资源。 线程好比图中的工人,多个工人在一个工厂中协作工作,工厂与工人是 1:n 的关系。也就是说 一个进程由一个或多个线程组成,线程是一个进程中代码的不同执行路线 ; 工厂的空间是工人们共享的,这象征 一个进程的内存空间是共享的,每个线程都可用这些共享内存 。 多个工厂之间独立存在。 2. 多进程与多线程 多进程:在同一个时间里,同一个计算机系统中如果允许两个或两个以上的进程处于运行状态。多进程带来的好处是明显的,比如你可以听歌的同时,打开编辑器敲代码,编辑器和听歌软件的进程之间丝毫不会相互干扰。 多线程:程序中包含多个执行流,即在一个程序中可以同时运行多个不同的线程来执行不同的任务,也就是说允许单个程序创建多个并行执行的线程来完成各自的任务。 以 Chrome 浏览器中为例,当你打开一个 Tab 页时,其实就是创建了一个进程,一个进程中可以有多个线程(下文会详细介绍)

libuv httpparser写的简单http server

為{幸葍}努か 提交于 2020-02-05 07:58:45
libuv文档地址:http://docs.libuv.org/en/v1.x/代码地址:https://github.com/libuv/libuvhttp-parser https://github.com/nodejs/http-parser #include <stdio.h> #include <stdlib.h> #include <assert.h> #include "uv.h" #include "http_parser.h" struct header { char field[1024]; char value[1024]; }; typedef enum { NONE=0, FIELD, VALUE } head_type; struct message { int header_num; char url[1024]; header headers[15]; head_type last_header_element; }; int on_message_begin(http_parser* parser); int on_headers_complete(http_parser* parser); int on_message_complete(http_parser* parser); int on_url(http_parser* parser,

(How) am I supposed to destroy a uv_async_t?

丶灬走出姿态 提交于 2020-01-15 03:35:50
问题 After I'm done with a uv_async_t , I'm supposed to destroy it to avoid any leaks, right? From glancing at the docs, it appears I'm supposed to use uv_close() for this, but it takes a uv_handle_t* , not a uv_async_t* . Furthermore, it looks like casting it (as in uv_close((uv_handle_t *)async, NULL) ) would cause a strict aliasing violation. Is that what I'm supposed to do anyway? 回答1: Yes, you have to cast the uv_async_t* to uv_handle_t* . That's how libuv internally works. All handles share

详解JavaScript中的事件循环机制(Event Loop)

Deadly 提交于 2020-01-09 17:34:24
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 前言 我们都知道,javascript从诞生之日起就是一门单线程的非阻塞的脚本语言。这是由其最初的用途来决定的:与浏览器交互。 单线程意味着,javascript代码在执行的任何时候,都只有一个主线程来处理所有的任务。 而非阻塞则是当代码需要进行一项异步任务(无法立刻返回结果,需要花一定时间才能返回的任务,如I/O事件)的时候,主线程会挂起(pending)这个任务,然后在异步任务返回结果的时候再根据一定规则去执行相应的回调。 单线程是必要的,也是javascript这门语言的基石,原因之一在其最初也是最主要的执行环境——浏览器中,我们需要进行各种各样的dom操作。试想一下 如果javascript是多线程的,那么当两个线程同时对dom进行一项操作,例如一个向其添加事件,而另一个删除了这个dom,此时该如何处理呢?因此,为了保证不会 发生类似于这个例子中的情景,javascript选择只用一个主线程来执行代码,这样就保证了程序执行的一致性。 当然,现如今人们也意识到,单线程在保证了执行顺序的同时也限制了javascript的效率,因此开发出了web worker技术。这项技术号称让javascript成为一门多线程语言。 然而,使用web worker技术开的多线程有着诸多限制,例如

C optimization: conditional store to avoid dirtying a cache line

好久不见. 提交于 2020-01-01 02:28:09
问题 In the libuv source, I found this code: /* The if statement lets the compiler compile it to a conditional store. * Avoids dirtying a cache line. */ if (loop->stop_flag != 0) loop->stop_flag = 0; Can someone explain this a bit? What exactly is a cache line? Also, I guess a conditional store is some Assembler instruction which checks something and if succeeded, writes some value. Right? When does such construct makes sense? I guess not always, because otherwise the compiler would just always

Linking libuv library in XCode

南楼画角 提交于 2019-12-31 03:01:51
问题 Has anyone had any experience with linking libuv in xcode? I've created a submodule in git to allow easy updating to the library but I can't quite figure out how to correctly add the library to the project to allow me to include uv.h. Any help would me greatly appreciated. 回答1: You can use GYP to generate an xcodeproj for libuv (as explained in libuv's README) and add this xcodeproj to your main Xcode project. It can be automated (for easy updating) with a simple shell script (assumes you put

Does libuv provide any facilities to attach a buffer to a connection and re use it

耗尽温柔 提交于 2019-12-22 08:55:02
问题 I am evaluating libuv as a library for a C/c++ server that I am writing. The protocol is length prefixed so as soon as I can read a 32 bit integer from the stream I should be able to tell what size of buffer I should allocate. The documentation says that the uv_read_start function might be called multiple times. UV_EXTERN int uv_read_start(uv_stream_t*, uv_alloc_cb alloc_cb, uv_read_cb read_cb); Since I am using a length prefixed protocol, once I know the right size of the buffer I would like

Is libuv just a wrapper on libev on POSIX systems?

梦想与她 提交于 2019-12-22 08:36:36
问题 I am really confused between libev and libuv. Is libuv just a wrapper on libev on POSIX systems? If not where does it differ? 回答1: No longer, since libuv-v0.9 Here is the libuv github issue which tracked the removal of libev from libuv. The reasons for removal are, to quote from the above linked issue: In case any project watchers are wondering, libev served us well but: It only supports level-triggered I/O. On Linux, we want to use edge-triggered mode - it cuts down the number of syscalls by

Is libuv just a wrapper on libev on POSIX systems?

佐手、 提交于 2019-12-22 08:36:04
问题 I am really confused between libev and libuv. Is libuv just a wrapper on libev on POSIX systems? If not where does it differ? 回答1: No longer, since libuv-v0.9 Here is the libuv github issue which tracked the removal of libev from libuv. The reasons for removal are, to quote from the above linked issue: In case any project watchers are wondering, libev served us well but: It only supports level-triggered I/O. On Linux, we want to use edge-triggered mode - it cuts down the number of syscalls by

dnx kestrel “System.EntryPointNotFoundException: uv_loop_size”

三世轮回 提交于 2019-12-22 05:30:21
问题 I'm trying to run an unmodified Web Api application created by yo aspnet. I'm getting this error: System.EntryPointNotFoundException: uv_loop_size at (wrapper managed-to-native) Microsoft.AspNet.Server.Kestrel.Networking.Libuv+NativeDarwinMonoMethods:uv_loop_size () at Microsoft.AspNet.Server.Kestrel.Networking.Libuv.loop_size () <0x42615b8 + 0x00014> in <filename unknown>:0 at Microsoft.AspNet.Server.Kestrel.Networking.UvLoopHandle.Init (Microsoft.AspNet.Server.Kestrel.Networking.Libuv uv)