UNIX网络编程之线程浅析

Unix/Linux 编程:网络编程之 线程池 一,线程池的基本组成 1. 工作线程队列 2. 任务队列 3. 线程管理器(封装了线程池的一些基本方法,创建,销毁,加入新任务等) 比如一个常用的网络服务,通常主循环用于处理接收与基本的recv/send操作。可是如果某些业务操作需要长时间的处理数据。 比如等待数据库查询结果,对数据进行编解码等业务逻辑事。为了不让主线程进行长时间的阻塞,从而引入了线程池。让线程池的工作队列来处理具体的业务。 主线程则继续处理网络连接。 应用:线程池是生产者与消费者模型的一个最典型的应用。最常见的ngin. 阅读详情

什么是线程:线程是一个进程内部的控制系列;线程是CPU调度的基本单位

线程的优点:

(1) 通过为每种事件类型的处理分配单独的线程,能够简化处理异步时间的代码。
(2) 多个线程可以自动共享相同的存储地址空间和文件描述符。
(3) 有些问题可以通过将其分解从而改善整个程序的吞吐量。
(4) 交互的程序可以通过使用多线程实现相应时间的改善,多线程可以把程序中处理用户输入输出的部分与其它部分分开。

线程的缺点:

在多线程程序里,因为时间分配的细微偏差或者因为共享了不该共享的变量可能会造成很大的不良影响。因此调试一个多线程程序比调试一个单线程的程序困难很多。

线程的结构:

线程包含了表示进程内执行环境必须的信息,其包括进程中标示线程的线程ID,调度优先级,线程的私有数据,一组寄存器值, 栈,策略,信号屏蔽子及errno变量。调用进程的所有信息对该线程都是共享的,其中包括可执行程序文本,程序的全局内存和堆内存,栈以及文件描述符。

线程的标识:

每个进程都有ID,所以每个线程也有ID。 但是进程ID在整个系统中是唯一的,线程ID不是唯一的,因为线程ID只在其所属的进程环境中有效。

线程的相关函数:
pthread_self 得到线程的id号
函数原型:`

   pthread_t pthread_self(void);

`

只要是想得到某个线程的线程ID号,就可使用该函数,例如:

#include <iostream>
#include <pthread.h>
#include <stdio.h>

using namespace std;

int main()
{
    printf("this pthread's id = %u\n", pthread_self());
    return 0;
}



//执行结果如下:
[root@localhost mine]# g++ pthread.cpp -o pthread -lpthread
[root@localhost mine]# ls
pthread  pthread.cpp  test  test.cpp
[root@localhost mine]# ./pthread 
this pthread's id = 3220752192
[root@localhost mine]# 

pthread_create 线程创建函数
函数原型如下:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

参数thread是线程id;
attr是线程属性;attr== NULL时表示默认属性,即非分离,非绑定,缺省1M的堆栈,与父进程同样级别的优先级;
第三个参数是一个参数为void*类型,返回值为void*类型的函数指针;
第四个参数arg是第三个参数的参数;
注意:若是第三个参数函数指针的参数个数不止一个,那就要将那些参数写入一个结构体中,然后将结构体的地址传给arg就可以啦^_^

该函数的使用示例如下:

#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

using namespace std;

void *thread_func(void *arg)
{
    for(int i = 0; i < 10; ++i){
        printf("this is child pthread.\n");
    }
}

int main()
{
    pthread_t tid;   //线程id

    sleep(1);
    pthread_create(&tid, NULL, thread_func, NULL);
    for(int i = 0; i < 10; ++i)
    {
        printf("this is Main pthread.\n");
    }
    return 0;
}


//未加sleep之前的输出结果:
[root@localhost mine]# g++ pthread.cpp -o pthread -lpthread
[root@localhost mine]# ./pthread 
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
[root@localhost mine]# ./pthread 
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
[root@localhost mine]# 




//加了sleep之后的输出结果:
[root@localhost mine]# ./pthread 
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
[root@localhost mine]# ./pthread 
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
[root@localhost mine]# 

pthread_exit 线程终止函数
函数原型如下:

void pthread_exit(void *retval)

该函数的作用就是终止调用线程,参数retval是线程退出时退出码所保存的地方,这个退出码用于pthread_join的retval参数
使用如下:

#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

using namespace std;

void *thread_func(void *arg)
{
    for(int i = 0; i < 10; ++i){
        printf("this is child pthread.\n");

        if(i == 4){  //子线程执行5次就结束
            const char* retval = "child normal quit."; //每次和pthread_exit函数一起出现
            pthread_exit((void *)retval);
        }
    }

}

int main()
{
    pthread_t tid;   //线程id

    pthread_create(&tid, NULL, thread_func, NULL);

    for(int i = 0; i < 10; ++i)
    {
        printf("this is Main pthread.\n");
    }

    char *retval;
    pthread_join(tid, (void **)&retval);
    printf("child thread exit code:> %s\n", retval);
    return 0;
}


//执行结果如下:
[root@localhost mine]# ./pthread 
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
child thread exit code:> child normal quit.
[root@localhost mine]# ./pthread 
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is child pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
this is Main pthread.
child thread exit code:> child normal quit.
[root@localhost mine]# 

pthread_join 线程等待函数
函数原型如下:

int pthread_join(pthread_t thread, void **retval)

第一个参数是线程id,第二个参数是函数pthread_exit的参数
示例如下:

#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

using namespace std;

void *thread_func(void *arg)
{
    for(int i = 0; i < 10; ++i){
        printf("this is child pthread.\n");

        if(i == 4){  //子线程执行5次就结束
            const char* retval = "child normal quit.";
            pthread_exit((void *)retval);
        }
    }

}

int main()
{
    pthread_t tid;   //线程id

    pthread_create(&tid, NULL, thread_func, NULL);

    for(int i = 0; i < 10; ++i)
    {
        printf("this is Main pthread.\n");
    }

    char *retval;
    pthread_join(tid, (void **)&retval);  //retval不为NULL时表示接收pthread_exit中的字符串,用以判断子线程是否正常终止,若retval为NULL则表示不接收pthread_exit中的retval字符串
    printf("child thread exit code:> %s\n", retval);
    return 0;
}

以上只是简单的函数解说,关于线程的属性,其比起这些函数来说比较复杂,会在下一篇博客中介绍^^ ^^ ^_^

UNIX网络编程第3版:实践指南与源代码解析 TCP/IP 协议族是一种分层的网络通讯协议体系,它被设计为允许不同种类的计算机通过网络交换信息。该体系结构通常被划分为四个层次,每一层都有特定的功能和协议来支持数据从源到目的地的传输。链路层:负责在相邻节点间的线路上无差错地传送以帧为单位的数据,包括了物理层与数据链路层。网络层:负责数据包从源到目的地的传输和路由选择,主要协议是互联网协议(IP)。传输层。 阅读详情

相关推荐

UNIX网络编程实战详解:从套接字到高并发服务开发

在UNIX系统中,网络编程以套接字(Socket)为核心,依托丰富的系统调用和底层支持,构建高效、可靠的通信应用。本章将介绍UNIX网络编程的基本运行环境,包括进程模型、文件描述符机制以及TCP/IP协议栈的集成方式。UNIX通过“一切皆文件”的设计哲学,使网络通信如同操作文件般统一,极大简化了开发复杂度。同时,POSIX标准确保了跨平台可移植性,为后续深入学习套接字编程奠定基础。套接字(Socket)是网络编程中最核心的抽象机制,它为应用程序提供了访问底层传输协议的统一接口。

weixin_30299319的博客 1102

UNIX 网络编程之线程

概述:    实现并发服务器一般都是父进程accept一个连接,然后fork一个子进程,该子进程处理与该连接对端的客户之间的通信。但是fork是昂贵,耗资源和时间。而线程是轻量级线程,它的创建比进程的创建块10-100倍。在同一进程内除了共享全局变量外还共享:  大多数数据;进程指令; 打开的文件; 信号处理函数信号处置; 当前工作目录;用户ID和组ID 不过每个线程有各自的资源:‘  线

千里之行,始于足下 1182

在 CentOS Stream 10 上搭建 Kubernetes 集群并支持开发调试与日志查看

搭建 Kubernetes 集群的每个步骤详细的文档。

灵犀物润 1650

Unix网络编程学习笔记

unix网络编程,C语言,多线程,多进程,epoll,select

weixin_45045804的博客 2638

GEO生信数据挖掘实战:从临床信息提取到多维分组可视化(聚类与PCA)

本文以GSE1297数据集为例,详细讲解了GEO生信数据挖掘中临床信息提取与分组构建的实战流程。通过R语言解析样本元数据,利用字符串处理技术从文本中精准构建疾病与对照分组,并强调使用层次聚类图和PCA图进行可视化验证,确保分组合理性与分析可靠性,为后续差异表达等高级分析奠定坚实基础。

cheese的博客 368

AutoCAD 2024 Mac版安装激活教程:从下载到破解一站式解决方案

本文提供AutoCAD 2024 Mac版的详细安装与优化指南,包括系统要求检查、下载安装流程、常见问题解决方案及性能优化设置。特别介绍了如何应用最新补丁和Update保持软件性能,帮助用户顺利完成从安装到破解的全过程。

weixin_29239661的博客 891

unix网络编程

略

google20的博客 675

HFSS入门 - 传输线线路损耗和串扰仿真

下图中两相近传输线存在端口间的串扰,而同一根传输线存在线路损耗。 我们可用S21描述端口1到端口2的线路损耗(插损?),用S13描述近端串扰,S24描述远端串扰,串扰通常要求-20dB以下。 实验目的:传输线线路损耗和串扰仿真 具体步骤: 1.草图: 其中板厚Subh为1mm。其余参数具体值如下: 2.由草图作图: 其中介质板表面设为GND。 3.设置激励:先在第一根传输线相连的XZ平面上做一...

qq_37274411的博客 9178

【UNIX网络编程】第26章 线程总结

前言: 这两天又发生了不少的事情,真是诸事不顺,不过这些伤心的事由它去吧,自己的生活还要继续,自己定下来的目标也要去实现,所以没有时间再去伤心了。 1.为什么使用线程 1.使用多线程的必要性 传统的服务器使用为每一个客户调用一次fork,产生一个子进程的方式来处理客户连接,但是fork缺有不少的缺点: a.fork调用昂贵,子进程需要复制父进程的内存,并且要复制所有的描述符。

yw8355507的专栏 372

Megatron-LM深度解析:万亿参数大模型的3D并行训练之道

训练框架,采用创新的"层内模型并行"设计,通过将Transformer层的计算拆分到多个GPU上并行执行,显著提升训练效率。该系统支持3D并行策略(数据并行、张量并行、流水线并行)和序列并行等新技术,成功应用于训练5300亿参数的MT-NLG模型。相比DeepSpeed、TPU GSPMD等框架,Megatron-LM在NVIDIA GPU上展现出卓越性能,并与TensorRT等工具深度集成,形成从训练到部署的完整解决方案。该项目持续演进,已成为大模型训练的重要基准。

数据与算法架构提升之路专栏 2123

AgentVerse实战:构建多智能体协作系统的关键技术与应用场景

本文深入探讨了AgentVerse框架在构建多智能体协作系统中的关键技术及应用场景。通过分析智能体通信协议、环境交互机制和任务分解算法,展示了如何利用大型语言模型(LLM)实现智能体间的专业化分工与动态协作。文章还提供了智能软件开发团队和智能客服系统等典型应用案例,以及部署优化实践,为开发者构建高效的多智能体系统提供实用指导。

weixin_29254029的博客 368

UNIX环境高级编程与UNIX网络编程卷1 PDF版资源

UNIX环境高级编程与UNIX网络编程卷1 PDF版资源 【下载地址】UNIX环境高级编程与UNIX网络编程卷1PDF版资源 UNIX环境高级编程与UNIX网络编程卷1 PDF版资源本仓库提供了《UNIX环境高级编程》高清PDF版和《UNIX网络编程卷1》高清PDF版的下载资源 ...

gitblog_09738的博客 497

【YOLO系列】YOLOv11详解:模型结构、损失函数、训练方法及代码实现

YOLOv11在目标检测领域实现重大突破,通过动态骨干网络(Dyna-Backbone)和FPN++架构,在保持高帧率(≥60FPS)的同时提升小目标检测精度(AP_S提升5.6%)。创新采用CIoU++损失函数和类别平衡Focal Loss,结合Mosaic-Plus数据增强,参数量减少18%至6.5M。实验显示,该模型在COCO数据集上实现更高mAP,特别适合移动端部署,Jetson Xavier平台能耗比优化至3.2W·ms⁻¹,支持目标检测、实例分割等多任务场景。关键创新包括动态深度调整和双向特征金

一碗白开水一 3447

UNIX网络编程

UNIX网络编程 去发现同类优质开源项目:https://gitcode.com/ 简介 《UNIX网络编程》是一本面向网络应用程序开发者,尤其是对那些致力于网游服务器开发人员的宝贵指南。本书深入浅出地讲解了在UNIX环境下的网络编程核心知识,其重点放在SOCKET套接字的基本API及其使用的详尽指导上。通过这本书,读者能够掌握实现高效网络通信的关键技术,这对于构建稳定、可靠的网络游戏服务器至关重...

gitblog_09744的博客 495

vue接入海康4G版摄像头通过萤石云平台推送视频流教程

vue-video-player接入海康视频,videojs-contrib-hls,vue播放视频

renkai721的专栏 7297

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

UNIX网络编程----线程(二十六)

UNIX网络编程----线程 一、概述 一个服务器的父进程accept一个连接,fork()一个子进程,该子进程处理与该连接对端的客户之间的通信。但是fork调用却存在一些问题。 1)  fork是昂贵的。当今的实现使用称为写实复制的技术,用以避免在子进程切实需要自己的副本之前把父进程的数据空间按复制到子进程。然而即便有这样的优化措施,fork还是昂贵的。 2)  fork返回之后斧子进程

鱼思故渊的专栏 1449

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Unix网络编程-同步

1、互斥锁(量)和条件变量默认情况下互斥锁和条件变量用于线程间同步,若将它们放在共享内存区,也能用于进程间同步。1.1 互斥锁1、概述: 互斥锁(Mutex,也称互斥量),防止多个线程对一个公共资源做读写操作的机制,以保证共享数据的完整性。用以保护临界区,以保证任何时候只有一个线程(或进程)在访问共享资源(如代码段)。保护临界区的代码形式:lock_the_mutex(...); 临界区 unlo

爱橙子的OK绷的专栏 9427

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

UNIX网络编程笔记:同步

这篇文章摘要如下: 互斥锁与条件变量是并发控制的核心机制。互斥锁通过二进制锁机制保护共享资源,确保线程安全访问临界区,但需注意死锁风险。条件变量则用于线程间高效协作,结合互斥锁实现"生产者-消费者"等模式,避免CPU轮询浪费。读写锁针对"读多写少"场景优化,允许并发读取而独占写入,提升系统吞吐量。实践建议包括最小化临界区、处理虚假唤醒、合理选择锁类型等。这些同步机制是构建高性能并发程序的基石,需根据具体场景组合使用。

梅见十柒的博客 1257

C++教程网之Linux网络编程视频 Unix网络编程视频

教程非常不错,价值280元,绝对是干货 Linux网络编程(总共41集) 讲解Linux网络编程知识,分以下四个篇章。 Linux网络编程之TCP/IP基础篇 Linux网络编程之socket编程篇 Linux网络编程之进程间通信篇 Linux网络编程之线程篇Linux网络编程之TCP/IP基础篇 01TCPIP基础(一) ISO/OSI参考模型 TCP/IP四层模型 基本概念(

diaohan2030的博客 4437

UNIX网络编程:互斥锁和条件变量

在网络编程中,一般都是多线程的编程,这就出现了一个问题:数据的同步与共享。而互斥锁和条件变量就是为了允许在线程或进程间共享数据、同步的两种最基本的组成部分。它们总能够用来同步一个进程中的多个线程。 再进入互斥锁和条件变量之前,我们先对多线程的一些相关函数进行简单介绍:多线程简单介绍和相关函数: 通常,一个进程中包括多个线程,每个线程都是CPU进行调度的基本单位,多线程可以说是在共享内存空间中并发

Dandelion_gong的博客 3208

《UNIX网络编程》中如何使用线程实现高性能TCP服务器?

书中通过线程池、同步机制和I/O优化,实现高吞吐、低延迟的TCP服务器。其核心在于。

1123

《UNIX 网络编程-卷1》阅读笔记24: 线程

在传统的 UNIX 模型中,服务器处理并发客户端连接的标准做法是 子进程。一个进程中的所有线程共享相同的全局内存,这使得线程很容易共享信息,但这种简易性也带来了同步(synchronization)问题。虽然 模型被广泛使用,但它有两个主要问题: :成功创建后,新线程的 ID 通过此指针返回 :线程属性(优先级、栈大小等),通常传 使用默认值 :新线程执行的函数,必须声明为 :传递给 的通用指针。如需传递多个参数,需打包成结构体 2.3 线程等待与分离 :等待指定线程终止。 非空时存放线程

1473

UNIX网络编程卷1套接字联网API第3版完整书签下载:深入UNIX网络编程的必备利器

UNIX网络编程卷1套接字联网API第3版完整书签下载:深入UNIX网络编程的必备利器 去发现同类优质开源项目:https://gitcode.com/ 在数字时代,网络编程的重要性日益凸显。UNIX作为网络编程的重要平台之一,拥有丰富的API和资源。《UNIX网络编程卷1:套接字联网API(第3版)》完整书签下载项目,为广大编程爱好者提供了一个高效学习的工具。以下是对这个项目的详细介绍。 项目介...

gitblog_06748的博客 734

unix 网络编程总结

unix 网络编程总结 unix 网络编程总结 - 邓维 - 博客园unix 网络编程总结from:http://middleware123.com/tuxedo/intro/303.html1.处理SIGCHLD信号当编写fork子进程处理连接的服务器程序时,...

147

UNIX网络编程卷2 源码编译篇

1 tar -xzfv unpv22e.tar.gz 2 cd unpv22e 3 ./configure 4 cd lib 5 make   make编译失败,因为需要对两个文件修改,unpv22e/config.h和unpv22e/wrapunix.c。 vi config.h /*注释掉这三行*/ // #define uint8_t unsigned char /* <sys/types.h> */ // #define uint16_t unsigned sho..

1 477
上一篇: 线程的状态----joinable和detached
下一篇: 管道(无名管道)和FIFO(有名管道)
lm_123_456
博客等级 码龄11年 35粉丝 86原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址