线程创建,CRT和DLL的是怎么回事? [英] Thread creation, the CRT and DLL's how is it meant to be done?

查看:160
本文介绍了线程创建,CRT和DLL的是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我理解CreateThread和CRT可以导致内存泄漏,信号不工作,应该使用_beginthread或_beginthreadex函数。

So I understand that CreateThread and the CRT can result in memory leaks, signal doesn't work, and one should use the _beginthread or _beginthreadex functions.

在编写应用程序时非常好,但是那些正在编写DLL和其他应用程序(无论是简单的c dll,com对象,插件等)的内容。没有办法保证如何创建线程调用DLL,即使他们使用__beginthread(ex)它很可能打赌他们有一个不同的CRT实现\version。

That is all very well when writing an application, but what about those that are writing dll's and such for other applications (be it a plain c dll, com objects, plugins, etc). There is no way to guarantee how a thread calling into a DLL was created, even if they used __beginthread(ex) its a pretty likely bet they have a different CRT implementation\version.

那么程序员应该做什么呢?不使用CRT?生成一个内部线程并卸载所有的工作(不使用CRT与调用线程)?

So what exactly is it that programmers are expected to do? Not use the CRT? Spawn an internal thread and offload all work to that (without using the CRT with the calling thread)? Some trick with DllMain and the attach/detach stuff to correctly setup and shutdown all threads regardless of how they are created?

推荐答案

$ dllMain和attach / detach可以正确设置和关闭所有线程b $ b

CreateThread和CRT可导致内存泄漏

CreateThread and the CRT can result in memory leaks

不,这是一个误解,难以摆脱因为某些原因。在上个世纪,使用_beginthread()是必要的,VS6是VS的最后一个版本,有一个CRT仍然需要它。

No, that's a misunderstanding that's incredibly difficult to get rid of for some reason. Using _beginthread() was necessary back in the previous century, VS6 was the last version of VS that had a CRT that still required it.

部分来处理对添加到Windows 2000的线程池的支持。显然你不能在CRT中做任何事情来处理它们,这些线程是由操作系统本身启动的。实现非常简单。无论CRT需要一个线程局部变量,如strtok()需要的变量,它首先检查是否已经为线程分配了TLS存​​储。

That's been fixed, in no small part to deal with the support for the thread pool added to Windows 2000. Clearly you can't do anything in the CRT to deal with them, those threads are started by the OS itself. The implementation is pretty straight-forward. Wherever the CRT needs a thread-local variable, like the one needed by strtok(), it first checks if TLS storage was already allocated for the thread. If not, it allocates it on-the-fly.

只要使用CreateThread()而不用担心,当然假设你不再使用14岁的编译器。

Just use CreateThread() without fear, implicit of course with the assumption that you no longer use a 14 year old compiler.

这篇关于线程创建,CRT和DLL的是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆