Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create Thread in nodejs

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to create threads in nodejs, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

When we call new Worker, we finally call C++ 's StartThread function (node_worker.cc) to create a thread.

CHECK_EQ (uv_thread_create_ex (& w-> tid_, & thread_options, [] (void* arg) {

/ /...

}, static_cast (w)), 0)

Let's look at the logic of uv_thread_create_ex.

Int uv_thread_create_ex (uv_thread_t* tid

Const uv_thread_options_t* params

Void (* entry) (void * arg)

Void * arg) {

/ / ignore part of the code

Err = pthread_create (tid, attr, f.out, arg)

Return UV__ERR (err)

}

Then let's take a look at the return value definition of pthread_create

On success, pthread_create () returns 0; on error, it returns an error

Number, and the contents of * thread are undefined.

So, if uv_thread_create_ex returns non-0, pthread_create returns non-0. Indicates that an error has been reported. Let's look back at the processing of C++ when the return is non-zero. We are interested in the CHECK_EQ (uv_thread_create_ex (…) of C++ layer , 0) for macro expansion.

# define CHECK_EQ (a, b) CHECK ((a) = = (b))

# define CHECK (expr)\

Do {\

If (UNLIKELY (! (expr) {\

ERROR_AND_ABORT (expr);\

}\

} while (0)

# define UNLIKELY (expr) expr

Expand through some columns, and finally become

Do {

If (! (return value = = 0) {

ERROR_AND_ABORT (expr)

}

} while (0)

Because non-zero is returned when the thread is created, this is true. Let's keep watching ERROR_AND_ABORT.

# define ERROR_AND_ABORT (expr)\

Do {

Static const node::AssertionInfo args = {\

_ _ FILE__ ":" STRINGIFY (_ _ LINE__), # expr, PRETTY_FUNCTION_NAME\

};\

Node::Assert (args);\

} while (0)

Splice the error message, and then execute node::Assert (args)

[[noreturn]] void Assert (const AssertionInfo& info) {

Char name [1024]

GetHumanReadableProcessName & name)

Fprintf (stderr

"% slug% s:% s% s Assertion `% s' failed.\ n"

Name

Info.file_line

Info.function

* info.function? ":"

Info.message)

Fflush (stderr)

Abort ()

}

The point is Abort.

[[noreturn]] void Abort () {

DumpBacktrace (stderr)

Fflush (stderr)

ABORT_NO_BACKTRACE ()

}

Keep watching ABORT_NO_BACKTRACE.

# ifdef _ WIN32

# define ABORT_NO_BACKTRACE () _ exit

# else

# define ABORT_NO_BACKTRACE () abort ()

# endif

So the final call is _ exit or abort to exit or terminate the process. Let's discuss the situation under linux. Let's look at the description of the abort function.

The abort () function first unblocks the SIGABRT signal, and then

Raises that signal for the calling process (as though raise (3) was)

Called). This results in the abnormal termination of the process

Unless the SIGABRT signal is caught and the signal handler does not

Return (see longjmp (3))

If the SIGABRT signal is ignored, or caught by a handler that

Returns, the abort () function will still terminate the process. It

Does this by restoring the default disposition for SIGABRT and then

Raising the signal for a second time.

The abort function sends a SIGABRT signal to the process, and we can register the function to handle this signal, but we still can't stop the process from exiting, because after he has executed our handler, he will register the handler as the default of the system, and then send the SIGABRT signal again, and the default behavior is to terminate the process. Let's have a test.

Const {Worker, threadId} = require ('worker_threads')

For (let I = 0; I

< 1000; i++) { const worker = new Worker('var a = 1;', { eval: true }); } 我们创建1000个线程。结果

After reading the above, have you mastered how to create threads in nodejs? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report