Some useful tips about developing multi-threading applications

皮皮鲁 2006-04-15 03:29:56
Some useful tips about developing multi-threading applications

这四五个月以来主要做KSP的开发,主要的难点是多线程同步。到现在为止,已经积累了不少开发多线程的经验,尤其是调试多线程和一些多线程开发经验。 下面的tips全部来自开发过程中遇到的问题。

1。在写MFC的DLL时千万要小心。

最好不要在InitInstance中调用一些你不知道它具体会做什么的初始化操作,如果那些初始化操作需要创建一个新的线程,并且需要等待新创建的线程结束时,会造成死锁。
因为InitInstance是在DllMain中被调用,而新创建的线程在调用线程入口函数前,在kernel中需要调用当前进程中的所有DllMain,
但是InitInstance所在线程,也就是DllMain所在线程在等待新创建的线程结束,所以这里造成死锁。新创建的线程会在Kernel中卡住,线程入口函数无法被执行。
为了这个问题,我花了两天时间。真是shit。我为一个新硬件写了一个KSP,而那个调用我的KSPl的程序是个用MFC写的控制面板程序(DLL)。它在InitInstance中调用我KSP的初始化函数,这个初始化函数中要创建一个线程,并且等待它结束,所以造成死锁。因为那个控制面板程序是很早以前写的。我一直没想到是控制面板程序的问题,一直在我的KSP中找原因。
如果你发现你的线程入口函数无法执行,请首先排除是不是这个原因。

2.在多线程中使用GUI函数要尤其小心。

比如EnableWindow这个函数,它会导致线程切换,切换到目的窗口所在的线程。如果当前的线程获取了一个Mutex或者CriticalSection,而窗口所在线程正在Wait这些资源,那么就会造成死锁。
为了这个问题,我也花了一个下午,最后加班到晚上8点还没搞定(当时还不会用4种提到的windbg),然后回去之前忘记打卡,还被罚了100元,真是郁闷。第二天一早才搞定。

3。尽量使保护区域(用mutex或者CriticalSection)中执行的代码最少。

尤其不要执行一些会引起线程调度(这个同2)或者需要Acquire mutex或者CriticalSection的函数。这样很容易造成死锁。这个问题我碰到了好多次了。

4。调试多线程死锁的最好工具(起码我目前知道的)是WinDbg。

将Windbg attach到死锁的进程,它就它可以列出当前进程的所有的线程,并且可以查看堆栈,如果你有symbol的话,可以查看具体的函数名,如果有代码的话,可以查看源码。并且在没有源码的机器上可以先将进程dump出来,然后在有symbol和源码的机器上面进行分析。以前分析死锁都是用原始的日志,为了一个死锁往往要加入n多的日志,并花不少时间来分析,一个麻烦的问题,用去半天的时间很正常。学会用windbg之后,死锁一般都可以在十几分钟搞定。

http://spaces.msn.com/gerogepei/
...全文
241 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
lqm1003 2006-04-17
  • 打赏
  • 举报
回复
哇,分享经验还有那么多分,lz好人
皮皮鲁 2006-04-17
  • 打赏
  • 举报
回复
Inside windows 2000 这本书不错

另外,windbg在配备windows的symbol之后,也可以看出一些windows的执行细节。
BBirdlyh 2006-04-17
  • 打赏
  • 举报
回复
好文,不给分也顶顶顶
下面我想讲个故事:我上学的时候做过一个程序,也是在多线程中用了ENABLEWINDOW,当时也是出现了问题2,但我当时不知道怎么回事,抓狂后就改用其他办法解决了,但是,但是一直不知道为什么。知道我找工作面试时,当时面试的对多线程这块特感兴趣,我就谈了一些这方面的内容,当然上述那个问题也包括,结果面试的问我你知道为什么会发生那样的问题吗,我又抓狂一次
其实我想说的是,我们是生活在微软外面的人,有时候用MFC,本来觉得很简单的一个函数会引起很多问题,我想问楼主和其他各位高手,到底从哪里去获得很多象问题1,2这样的内部细节问题,我需要哪些资料???
lixiaosan 2006-04-17
  • 打赏
  • 举报
回复
既然想人顶,我就帮顶一下。。。不过分分不能少。
皮皮鲁 2006-04-17
  • 打赏
  • 举报
回复
Of course, I'm a good man. HAHA.

In fact, I just want to attract people to UP this post. HEIHEI.
soaroc 2006-04-16
  • 打赏
  • 举报
回复
学习啊
菜牛 2006-04-16
  • 打赏
  • 举报
回复
不错。
kugou123 2006-04-16
  • 打赏
  • 举报
回复
好文
rageliu 2006-04-16
  • 打赏
  • 举报
回复
:)
yuanyou 2006-04-16
  • 打赏
  • 举报
回复
感谢啊
学习
希望以后会有更多的经验交流贴子
henry1006 2006-04-16
  • 打赏
  • 举报
回复
感谢啊
学习 学习 呵呵
Youthllen 2006-04-16
  • 打赏
  • 举报
回复
学习
oyljerry 2006-04-16
  • 打赏
  • 举报
回复
^_^,赞,……
一条晚起的虫 2006-04-16
  • 打赏
  • 举报
回复
up
xing_xing_xing 2006-04-15
  • 打赏
  • 举报
回复
好东西
snowbirdfly 2006-04-15
  • 打赏
  • 举报
回复
感谢搂主~~
学习~~~
皮皮鲁 2006-04-15
  • 打赏
  • 举报
回复
星星比我的三角裤都多。。
DentistryDoctor 2006-04-15
  • 打赏
  • 举报
回复
楼主有些心得.
皮皮鲁 2006-04-15
  • 打赏
  • 举报
回复
我也做做沙发,第一次发200分的帖子。。。。(其实我很少发帖,呵呵)
Java 9 Concurrency Cookbook - Second Edition by Javier Fernandez Gonzalez English | 25 Apr. 2017 | ASIN: B01KOG6V5M | 594 Pages | AZW3 | 4.11 MB Key Features Get detailed coverage of important recipes on multi-threading and parallel programming This book takes a close look at the Java 9 APIs and their impact on concurrency See practical examples on thread safety, high-performance classes, safe sharing, and a whole lot more Book Description Writing concurrent and parallel programming applications is an integral skill for any Java programmer. Java 9 comes with a host of fantastic features, including significant performance improvements and new APIs. This book will take you through all the new APIs, showing you how to build parallel and multi-threaded applications. The book covers all the elements of the Java Concurrency API, with essential recipes that will help you take advantage of the exciting new capabilities. You will learn how to use parallel and reactive streams to process massive data sets. Next, you will move on to create streams and use all their intermediate and terminal operations to process big collections of data in a parallel and functional way. Further, you'll discover a whole range of recipes for almost everything, such as thread management, synchronization, executors, parallel and reactive streams, and many more. At the end of the book, you will learn how to obtain information about the status of some of the most useful components of the Java Concurrency API and how to test concurrent applications using different tools. What you will learn Find out to manage the basic components of the Java Concurrency API Use synchronization mechanisms to avoid data race conditions and other problems of concurrent applications Separate the thread management from the rest of the application with the Executor framework Solve problems using a parallelized version of the divide and conquer paradigm with the Fork / Join framework Process massive data sets in an optimized way using streams and reactive streams See which data structures we can use in concurrent applications and how to use them Practice efficient techniques to test concurrent applications Get to know tips and tricks to design concurrent applications About the Author Javier Fernandez Gonzalez is a software architect with almost 15 years of experience in Java technologies. He has worked as a teacher, researcher, programmer, analyst, and writer, and he now works as an architect in all types of projects related to Java, especially J2EE. As a teacher, has taken over 1,000 hours of training in basic Java, J2EE, and the Struts framework. As a researcher, he has worked in the field of information retrieval, developing applications for processing large amounts of data in Java, and has participated as a coauthor in several journal articles and conference presentations. Recently, he worked on developing J2EE web applications for various clients from different sectors (public administration, insurance, healthcare, transportation, and so on). Currently, he works as a software architect. He is the author of the book, Java 7 Concurrency Cookbook and Mastering Concurrency Programming with Java 8 by Packt. Table of Contents Thread Management Basic Thread Synchronization Thread Synchronization Utilities Thread Executors Fork/Join Framework Parallel and Reactive Streams Concurrent Collections Customizing Concurrency Classes Testing Concurrent Applications Additional Information Concurrent Programming Design

15,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧