`
aigo
  • 浏览: 2583272 次
  • 性别: Icon_minigender_1
  • 来自: 宜昌
社区版块
存档分类
最新评论

libuv的多线程问题

 
阅读更多

原文:http://stackoverflow.com/questions/13838972/is-libuv-thread-safe?answertab=votes#tab-top

 

I have created a new thread dedicated to a libuv run loop. The thread function looks something like this:

void thread_function()
{
  uv_loop_t *loop = uv_loop_new();
  uv_ref( loop );
  uv_run( loop );
}
 

The ref counter increment keeps the thread alive and in a state to process libuv events. I hope to be able to cause the run loop to end, thus causing the thread to exit, by executing uv_unref on the main thread.

However, on inspecting the uv_ref source code I failed to see any guarantee that access to the reference counter variable would be synchronized during concurrent access. Additionally I did not see any yield calls to relinquish control to the operating system during the run loop, meaning the program will not cooperate well with other processes.

This leads me to believe that I am not using libuv in the right way. If someone could explain what I'm doing wrong, that would be great!

 

 

Answer:

No, libuv is not thread safe in this way. You should use uv_async to signal the loop to exit. uv_async is the only thread-safe facility that libuv has.

It would look something like this:

uv_async_t exit_handle;

void exit_async_cb(uv_async_t* handle, int status) {
  /* After closing the async handle, it will no longer keep the loop alive. */
  uv_close((uv_handle_t*) &exit_handle, NULL);
}

void thread_function() {
  uv_loop_t *loop = uv_loop_new();
  /* The existence of the async handle will keep the loop alive. */
  uv_async_init(loop, &exit_handle, exit_async_cb);
  uv_run(loop);
}

 

Now from the other thread you can signal this loop to exit by calling

uv_async_send(&exit_handle);

 

You need to take care to not call uv_async_send() before the other thread has finished setting up the loop and the uv_async handle. Recent version of libuv include the uv_barrier synchronization primitives that you could use; but the libuv version that ships with Node.js 0.8 doesn't support this yet, so you probably need to use pthread facilities to make this work.

On a side note, you seem to be calling uv_ref and uv_unref with a loop reference as the argument. In recent versions of libuv this has changed, you are now supposed to uv_ref and uv_unref a specific handle. See uv.h for details.

分享到:
评论

相关推荐

    模拟摄像头libuv支持多线程并发

    本文将深入探讨“模拟摄像头libuv支持多线程并发”这一主题,结合“模拟IPC”这一标签,以及文件名称“IpcSimulate”,我们将分析如何利用libuv库在多线程环境下实现模拟摄像头的功能。 首先,让我们了解一下什么是...

    libuv多线程

    服务端代码进行封装,libuv以及pjsip等,支持扩展,便于开发.

    libuv book 中文文档 An introduction to libuv

    4. **threads.html**: 讨论libuv的线程支持,包括线程池的使用和线程间的通信,这对于构建多线程应用至关重要。 5. **networking.html**: 这部分详细介绍了libuv的网络功能,如TCP/UDP套接字、HTTP服务器和客户端、...

    异步通信LIBUV

    总之,libuv作为一个基于事件的I/O库,为开发者提供了一套完整的工具集,无论是简单的文件操作还是复杂的网络编程,甚至是多线程和子进程的管理,libuv都提供了一种高效、统一的方式来处理这些任务。通过理解libuv的...

    libuv 1.22.0动态链接库libuv.dll

    4. **进程和线程管理**:可以创建子进程,实现进程间通信(IPC),以及线程的同步和互斥。 5. **定时器和一次性任务**:支持定时触发回调,以及一次性任务的安排和执行。 总之,libuv 1.22.0动态链接库libuv.dll是...

    libuv-1.39.0.zip

    虽然libuv主要依赖于单线程事件循环,但它允许开发者在需要时利用多线程进行计算密集型任务,提高程序性能。1.39.0版本的线程池优化了任务调度和资源管理,使得多线程协作更为顺畅。 总的来说,libuv-1.39.0是一个...

    编译好的libuv

    3. **线程池**:libuv内置了一个线程池,用于执行阻塞的I/O操作或CPU密集型任务,从而避免了单线程中的阻塞问题,提高了程序的并发性能。 4. **跨平台兼容性**:libuv支持多种操作系统,包括Windows、Linux、macOS...

    VS2019编译好的libuv-v1.33.1库

    5. **进程和线程管理**:支持创建子进程,以及跨平台的线程池管理,简化多进程和多线程编程。 6. **事件循环**:Libuv 的核心是事件循环模型,它负责调度和分发各种异步操作,确保程序的高效运行。 在 VS2019 中...

    libuv 编程指南 2.0

    线程间通信是多线程编程的一个重要方面,libuv提供了必要的工具和接口。 ### 6. 进程 #### 6.1 孵化子进程 libuv能够创建子进程,并管理它们的生命周期。 #### 6.2 更改进程参数 开发者可以在子进程启动前更改...

    libuv源码libuv-1.x

    在 libuv 中,可以创建和管理线程,实现多线程编程。同时,libuv 还提供了进程管理功能,包括创建子进程、向子进程发送信号、读取子进程的标准输出和错误流等。 5. **信号处理** libuv 支持注册信号处理器,可以...

    An Introduction to libuv.pdf

    - **多线程支持**:探讨libuv如何管理线程,包括线程间同步、工作队列(work queue)机制及线程间通信。 - **进程管理**:涵盖子进程的创建、参数设置、信号发送(signal sending)以及子进程输入输出管理等方面。 - **...

    libuv开发说明中文版

    在多线程方面,libuv提供线程池来处理一些耗时的任务,这样不会阻塞主线程,从而保证了事件循环的流畅运行。此外,libuv也支持创建和管理子进程,提供了创建新进程和执行外部程序的接口,这在执行需要隔离或并行处理...

    cpp-libuv跨平台异步IO

    - **单线程与多线程**:libuv允许开发者在一个单独的线程中处理所有事件,同时也可以利用多线程来扩展处理能力,根据应用需求选择合适的策略。 2. **libuv的主要组件** - **事件循环(Event Loop)**:libuv的...

    libuv 源码+demon

    - **线程池**:libuv 使用线程池来处理一些 CPU 密集型任务,避免了因单线程运行导致的性能瓶颈。 - **文件系统操作**:提供了异步的文件读写、文件属性获取、目录遍历等功能,保证了文件操作的非阻塞性。 - **信号...

    An Introduction to libuv

    libuv通过这些机制为应用程序提供多线程I/O处理的能力。 6. 进程管理:libuv可以用于创建和管理子进程,包括设置子进程参数、分离进程、发送信号以及处理子进程I/O等。这对于需要与外部程序交互的应用程序来说尤其...

    libuv1.11.0(vs2013编译版)

    - 由于与 Node.js 的紧密关系,libuv 有一个活跃的社区,提供了大量的开源示例、插件和工具,方便开发者解决问题和扩展功能。 总结,libuv1.11.0 是一个强大的异步 I/O 库,尤其适合构建高并发的网络应用程序。...

    libuv C++封装

    6. **线程安全**:libuv 在多线程环境中的使用需要额外的同步措施,封装库会处理这些细节,确保在多线程环境下的正确运行。 7. **Windows 和 Linux 兼容**:libuv 已经跨平台,封装库同样如此,能够在 Windows 和 ...

    libuv--可调试(vs2008编译)

    2. **网络支持**: 提供TCP和UDP套接字的支持,可以处理TCP服务器和客户端,以及UDP广播和多播。 3. **文件系统操作**: 包括文件读写、目录遍历、文件同步和异步操作。 4. **进程和线程**: 支持子进程的创建和管理,...

    libuv-v1.9.0.tar.gz

    最后,通过实际项目练习,逐步掌握libuv在解决实际问题中的应用。 总结,libuv-v1.9.0是一个强大的跨平台异步I/O库,它简化了网络编程的复杂性,提高了程序的性能和响应速度。通过理解和熟练使用libuv,开发者可以...

Global site tag (gtag.js) - Google Analytics