当前位置:首页 > 开发 > 开源软件 > 正文

libuv的多线程问题

发表于: 2013-08-14   作者:aigo   来源:转载   浏览次数:
摘要: 原文: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

原文: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的多线程问题

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
一.线程不安全示例: 代码1: class Ticket implements Runnable { public int tick = 100; Object o
编写一个小的抢票程序代码如下: packagecom.hbsi; //模拟临界资源的类 classTickets{ publicinttic
什么是线程的安全问题? 上一篇 Java基础-多线程-①线程的创建和启动 我们说使用实现Runnable接口的
<!---->   Servlet/JSP技术和ASP、PHP等相比,由于其多线程运行而具有很高的执行效率。由于
在多核快速发展的现在,利用多线程技术提高CPU设备的利用率已经成为一种趋势。然而多核计算机体系架
代码1线程不安全: /** * 线程间通信 * 其实就是多线程在操作同一个资源 * 但是操作的动作不同 * */
java多线程最基础的问题 原文(http://java-success.blogspot.com.au/2011/09/java-multi-threading
并发问题的症状 多线程put后可能导致get死循环 从前我们的Java代码因为一些原因使用了HashMap这个东
摘 要:介绍了Servlet多线程机制,通过一个实例并结合Java 的内存模型说明引起Servlet线程不安全的
并发问题的症状 多线程put后可能导致get死循环 从前我们的Java代码因为一些原因使用了HashMap这个东
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号