Boost线程学习简记

Boost线程学习简记

头文件

#include <boost/thread.hpp>

创建线程

boost::thread是线程类,创建线程非常简单,只需要创建一个thread对象,并把线程工作函数作为参数传给构造参数。

void output()

{

   std::cout<<"output"<<std::endl;

}

boost::thread t(output);

 

传递参数

如果我们的工作函数需要参数怎么办呢?

void output2(int v)

{

   std::cout<<"output : "<<v<<std::endl;

}

boost::thread t(output2,4);

如果我们的线程函数有多个参数,不论什么类型,都写成如下形式(只要不超过十个)

boost::thread t(fun,arg1,arg2,);

 

合并到主线程

t.join();

等待线程函数执行完毕

 

Sleep函数

boost::this_thread::sleep()

这里的sleep()函数并不是休眠多久的意思,而是睡眠到什么时候。

所以如果我们想要他休眠一秒钟,就要先计算一秒钟之后的时间是多少然后传给sleep

   //计算出sec+0.0000000001*nsec秒后的时间

boost::xtime getNextTime(int sec,int nsec)

   {

      boost::xtime t;

      boost::xtime_get(&t, boost::TIME_UTC);

      t.sec += sec;

      t.nsec += nsec;

      return t;

   }

boost::this_thread::sleep(getNextTime(1,0));

<think>我们正在讨论LWIP中的tcpip_thread函数。根据引用,tcpip_thread是LWIP协议栈中一个核心的线程,负责处理网络消息。引用[1]和[2]都提到了tcpip_init函数会创建tcpip_thread线程,该线程在初始化时会调用一个回调函数(如果设置的话),然后进入一个无限循环,从邮箱(mbox)中获取消息并处理。引用[3]则提到了使用Boost::thread创建线程的方法,但LWIP通常是在嵌入式系统中使用,可能不会直接使用Boost库,而是使用自己的线程机制。用户要求:tcpip_thread函数的用法、示例和说明。根据引用[1]和[2],我们可以总结如下:1.tcpip_thread是LWIP中负责处理TCP/IP协议栈的主线程。2.它由tcpip_init函数创建(通过sys_thread_new创建)。3.该线程首先会调用初始化回调函数(如果tcpip_init_done不为空),然后进入一个循环,等待邮箱中的消息,并对消息进行处理。在LWIP中,应用程序通常不直接调用tcpip_thread,而是由协议栈内部创建并运行。应用程序通过邮箱机制(tcpip_mbox)向该线程发送消息(tcpip_msg类型),然后由tcpip_thread调用相应的处理函数。下面我们根据引用内容,详细说明tcpip_thread函数的用法,并给出一个示例(伪代码或说明性代码)。</think>### tcpip_thread 函数详解 `tcpip_thread` 是 LWIP(轻量级 TCP/IP 协议栈)的核心线程函数,负责处理所有网络协议栈操作。它由 `tcpip_init()` 初始化时创建,通过邮箱机制(mbox)接收并处理网络消息。 #### 函数功能 1. **初始化阶段** 调用注册的回调函数(如果存在): ```c if (tcpip_init_done != NULL) { tcpip_init_done(tcpip_init_done_arg); // 执行初始化回调[^2] } ``` 2. **消息处理循环** 通过邮箱接收消息并处理: ```c while (1) { TCPIP_MBOX_FETCH(&tcpip_mbox, (void **)&msg); // 从邮箱获取消息 tcpip_thread_handle_msg(msg); // 处理消息[^1] } ``` - 邮箱 (`tcpip_mbox`) 是线程间通信机制 - 消息类型包括:数据包接收、定时器事件、API 调用等 #### 使用示例 以下展示如何创建并运行 `tcpip_thread`: ```c // 初始化LWIP协议栈 void net_init() { tcpip_init(NULL, NULL); // 创建tcpip_thread线程[^1] } // 自定义初始化回调(可选) void my_init_callback(void *arg) { printf("TCP/IP stack initialized!\n"); } // 带回调的初始化 void net_init_with_callback() { tcpip_init(my_init_callback, "user_data"); // 传递回调函数[^2] } ``` #### 关键机制 1. **邮箱通信** 其他线程/中断通过 `tcpip_mbox` 向 `tcpip_thread` 发送消息,例如: ```c struct tcpip_msg msg; msg.type = API_CALL_MSG; sys_mbox_post(&tcpip_mbox, &msg); // 投递消息 ``` 2. **线程安全** 通过 `LOCK_TCPIP_CORE()` 确保核心资源独占访问[^1]。 3. **消息处理** `tcpip_thread_handle_msg()` 根据消息类型调用相应处理函数(如 ARP 请求、TCP 状态机等)。 #### 应用场景 - 嵌入式设备网络通信(如 IoT 设备) - 实时操作系统中的 TCP/IP 协议栈实现 - 需要轻量级网络协议栈的场景 --- ### 相关问题 1. LWIP 中 `tcpip_mbox` 的消息传递机制如何实现线程安全? 2. 如何在 `tcpip_thread` 中添加自定义协议处理逻辑? 3. 当 `tcpip_thread` 无法及时处理邮箱消息时,会导致什么后果? [^1]: LwIP源码分析(2):tcpip_init和tcpip_thread函数分析 [^2]: LWIP_简记(7.tcpip_init()之tcpip_thread) [^3]: Boost::thread 用法(网络socket引用)TCPIP编程
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值