如何设置自定义堆栈大小TThread
?我正在尝试重新引入构造函数TThread
但它说ThreadProc
不见了,但它就在那里System.Classes
.
type
TThreadHelper = class helper for TThread
constructor Create(const CreateSuspended: Boolean = False; const StackSize: Integer = 0); reintroduce;
end;
{ TThreadHelper }
constructor TThreadHelper.Create(const CreateSuspended: Boolean; const StackSize: Integer);
begin
Self.FSuspended := not Self.FExternalThread;
Self.FCreateSuspended := CreateSuspended and not Self.FExternalThread;
if not Self.FExternalThread then
begin
Self.FHandle := BeginThread(nil, StackSize, @ThreadProc, Pointer(Self), CREATE_SUSPENDED, Self.FThreadID);
if Self.FHandle = 0 then
raise EThread.CreateResFmt(@SThreadCreateError, [SysErrorMessage(GetLastError)]);
end
else
begin
Self.FHandle := Winapi.Windows.GetCurrentThread;
Self.FThreadId := GetCurrentThreadId;
end;
end;
[dcc32 错误] Project5.dpr(29): E2003 未声明的标识符:
'线程过程'
我不知道,是否可以在创建线程后设置堆栈大小。或许SetThreadStackGuarantee http://msdn.microsoft.com/en-us/library/windows/desktop/ms686283%28v=vs.85%29.aspx有帮助吗?
您可以使用以下命令从头开始创建线程BeginThread
,但是相当复杂。我这里有一个解决方法,使用Detours
。请注意,有几个Detours
变种。我认为只有Cromis.Detours
兼容 x64。
unit IndividualStackSizeForThread;
interface
uses
System.Classes,
Cromis.Detours { http://www.cromis.net/blog/downloads/cromis-ipc/ };
type
TThreadHelper = class helper for TThread
constructor Create(CreateSuspended: Boolean; StackSize: LongWord);
end;
implementation
var
TrampolineBeginThread: function(SecurityAttributes: Pointer; StackSize: LongWord;
ThreadFunc: TThreadFunc; Parameter: Pointer; CreationFlags: LongWord;
var ThreadId: TThreadID): THandle = nil;
threadvar
StackSizeOverride: LongWord;
function InterceptBeginThread(SecurityAttributes: Pointer; StackSize: LongWord;
ThreadFunc: TThreadFunc; Parameter: Pointer; CreationFlags: LongWord;
var ThreadId: TThreadID): THandle;
const
STACK_SIZE_PARAM_IS_A_RESERVATION = $00010000; // http://msdn.microsoft.com/en-us/library/windows/desktop/ms682453(v=vs.85).aspx
begin
if StackSizeOverride <> 0 then
begin
CreationFlags := CreationFlags or STACK_SIZE_PARAM_IS_A_RESERVATION;
StackSize := StackSizeOverride;
StackSizeOverride := 0;
end;
Result := TrampolineBeginThread(SecurityAttributes, StackSize, ThreadFunc,
Parameter, CreationFlags, ThreadId);
end;
constructor TThreadHelper.Create(CreateSuspended: Boolean; StackSize: LongWord);
begin
StackSizeOverride := StackSize;
inherited Create(CreateSuspended);
end;
initialization
TrampolineBeginThread := InterceptCreate(@BeginThread, @InterceptBeginThread);
finalization
InterceptRemove(@TrampolineBeginThread, @InterceptBeginThread);
end.
我不知道为什么 Embt 不允许程序员指定堆栈大小,如果有人知道原因,这对我来说会很有趣。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)