博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to get rid of threads
阅读量:2340 次
发布时间:2019-05-10

本文共 3018 字,大约阅读时间需要 10 分钟。

It appears that school, tutorials, or whatever is teaching about threads a whole lot these days, and most designs I see from people learning the ropes involves a number of threads. However, threads cause bugs, and cause extra synchronization cost that's not easily visible, and not easily redeemable.

I worked on the BeOS, where we used threads out the wazoo. We made threads very, very cheap, but in the end, we were still actually too heavy on threads in our designs -- live and learn.

A typical threaded server design proposal looks something like:

  • Task Scheduler Thread
  • Socket Reader Thread
  • Processing Thread
  • Socket Writer Thread

 

for(;;) {    while( (t = next_event_time()) <= now() ) {      remove_and_run_one_event();    }    // this if() and the next while() can be combined with select()    if( there_is_no_input_data() && outgoing_queue_is_empty() ) {      sleep_until_there_is_data_or_time_happens( t );    }    while( there_is_more_input_data() ) {      read_data_from_socket();      put_data_in_todo_queue();    }    while( todo_queue_is_not_empty() ) {      remove_and_run_one_todo_item();    }    while( outgoing_queue_is_not_empty() && outgoing_socket_is_writable() ) {      remove_and_send_from_outgoing_queue();    }  }

This approach accomplishes the same thing, but it does it without the subtle race conditions and synchronization penalties that come from a threaded design.

There is a danger that scheduled tasks will be started after their scheduled time. The total time they could be delayed by is the sum of time it takes to read things from input, process them, and send to the output. However, this is a bounded amount of time, and on a single-CPU system, this bound is lower than the maximum latency you'd get with a four-thread program on a dual-CPU system in the worst case. If you need near-real-time scheduled tasks, then a general-purpose OS may not be your best choice. If you still need this, then perhaps those special tasks should actually go in their own thread, because that might be one of the special cases...

When should you use threads?

I've seen threads really work well in the following cases:

  • For scientific computing that's CPU bound, not memory bound, on multi-CPU systems.
  • For turning synchronous system APIs (gethostbyname(), file I/O etc) into asynchronous APIs.
  • For very-low-latency tasks that are device dependent, such as feeding sound cards or tracking the mouse. However, this typically happens in the OS, and the API at the user level is properly asynchronous.
  • When there are truly independent tasks in an application that don't need to talk to each other; i e a word processor with two separate documents open.

[Summary]

优点

1. 在多处理器上的多个thread并行: 尤其是计算密集型任务

2. 多个thread的并发, 能改善响应速度和性能, 防止因为I/O等阻塞操作导致服务不可用, 同时可以把turning synchronous system APIs (gethostbyname(), file I/O etc) into asynchronous APIs, 而且asynchronous I/O 通常比thread更复杂。
3. 更清晰的编程模型。、
4. I/O 密集型任务采用多thread来等待不同的I/O操作,也能提高性能。

 

缺点:

1. Thread的同步,死锁, 时序依赖关系, race condition, thread之间的切换 所带来的额外开销。

2. 难以调试。

 

转载地址:http://ydzvb.baihongyu.com/

你可能感兴趣的文章
JAVA Webservice
查看>>
Hibernate自动生成实体类
查看>>
Java Memcached
查看>>
JAVA WebSpider
查看>>
XML自动建表/存库
查看>>
Java实现Web服务器
查看>>
C# readonly与const的区别
查看>>
MFC 自定义消息的一般过程
查看>>
剖析Windows消息处理机制
查看>>
多线程入门教程(二)基本概念
查看>>
多线程入门教程(三)线程控制
查看>>
多线程入门教程(四)线程间通信
查看>>
多线程入门教程(五)MFC的多线程
查看>>
多线程入门教程(六)综合实例
查看>>
C/C++ 多线程学习心得
查看>>
C/C++四种退出线程的方法
查看>>
多线程编程要点
查看>>
c++CreateEvent函数在多线程中使用及实例
查看>>
c++多线程同步(1)
查看>>
Windows 下 C/C++ 多线程编程入门参考范例
查看>>