C network library to use (a): libuv installation, multi-threaded client tcp, tcp servers using multiple threads

Reference website: https://github.com/libuv/libuv/blob/master/README.md

https://www.cnblogs.com/cnxkey/articles/10058702.html

A, libuv installation

1, the windows installed (herein vs2017 compiled)

  1. Download libuv: https://github.com/libuv/libuv

  2. Download gyp ( https://github.com/turbulenz/gyp ) placed under decompression after libuv / build

  3. gyp installation requires python environment, download Python2.x ( https://www.python.org/ftp/python/2.7.13/python-2.7.13.msi ) installation and configuration environment variable

  4. Installation gyp: cmd enter libuv / build / gpy under execution: setup.py install

  5. Open vs2017 compiler tool to compile 32-bit debug version here (start -> vs2017-> x86 tools, the right to run with administrator privileges)

  6. libuv Installation: Switch to the project directory execute vcbuild vs2017

  7. When finished, under construction Debug / lib will generate .lib library, copy it include engineering and .lib libraries to use

2, mounted under linux

  1. Download libuv: https://github.com/libuv/libuv

  2. Extracting tar -zxvf libuv-v1.32.0.tar.gz

  3. If the lack of libtool library, please install yum install libtool

  4.  sh autogen.sh   

  5. ./configure 

  6. make 

  7. make check (error disregard)

  8. make install

  9. Create a soft link ln -s /usr/local/lib/libuv.so.1 /usr/lib64/libuv.so.1

Two, libuv use

1), using the windows

1. Create a new project vs2017

2, add the directory containing libuv header files, libraries directory added libuv of .lib library

3, conflict Additional Dependencies add the required items lib library library caused ignored LIBCMTD

Additional Dependencies:

libuv.lib
ws2_32.lib
iphlpapi.lib
Userenv.lib
Psapi.lib

Ignore specific default library: LIBCMTD

4, you can compile the project

2) using the following linxu

1, it can perform the g ++ test.cpp -luv

 

Two, libuv multithreaded tcp client and server programming

May refer to the URL code: https: //github.com/wangdamingll/libuv.git

Vs2017 code is written, if the compiler error, please remove the linux platform differences in the file header itself

It should be removed #include "pch.h", which is vs2017 header files used

1, multi-threaded client tcp

See the above URL LibuvClient.cpp file

2, multi-threaded tcp server

See the above URL LibuvServer.cpp file

Three, libuv multithreaded tcp client / server thinking

1, libuv single-threaded example is given official asynchronous I / O, and generally should be enough for normal use, provided herein only to provide reference multithreaded

2, libuv provided uv_queue_work (), if the server accept after the task is pushed to the inside of the thread pool, but because the server is tcp tcp data, the above-described embodiment is the consequence of having continuity data may no longer tcp continuity, resulting in a packet out of order, but also need to add additional logic to adjust for the normal running bales can not be taken personally think

3, I see the official website, function libuv, only uv_async_send () function is thread-safe, but the following code (server on the code above address):

It should not be thread-safe, but I did not test it, hope to understand the big brother advice

4, multi-threaded drawbacks is sending the message how hair problems, single-threaded asynchronous I / O can be used uv_async_send () callback is triggered and then sent, but you need to establish a multi-multi-threaded pipeline between the worker and the libuv multi-threaded, the pipeline read to libuv event registration cycle, by writing a byte of data in the worker thread to the pipe threads or trigger pipeline libuv all the specified thread reading the events send, I have tested the pipeline incident on linux, can be performed. Add your own on the windows. In addition, the use of libuv pipeline ipc mechanism, should be able to achieve, not repeat them here

Published 155 original articles · won praise 15 · views 160 000 +

Guess you like

Origin blog.csdn.net/wangdamingll/article/details/101619464