gpt4 book ai didi

multithreading - 我可以根据 ProcessID 和 ThreadID 创建唯一的文件名吗?

转载 作者:行者123 更新时间:2023-12-03 15:37:11 29 4
gpt4 key购买 nike

我有一个 delphi (Win32) Web 应用程序,可以作为 CGI 应用程序、ISAPI 或 Apache DLL 运行。我希望能够生成一个唯一的文件名前缀(对于给定时刻的所有当前请求都是唯一的),并且认为最好的方法是使用 processID (处理 CGI 模式)以及 threadID (处理dll 模式)。

如何在 Delphi 中获得唯一的进程 ID 和线程 ID?

这些在多核/多处理器情况下(在单个网络服务器计算机上)是否是唯一的?

编辑:请注意,有人建议我不要采用这种方法,因此接受的答案使用不同的方法来生成临时文件名

最佳答案

您在这里提出了很多好主意。

Does it also create an empty file to "get a lock on" the name?

不;我相信我们依靠 Windows 来确保自启动以来同一台计算机上不会两次给出相同的临时文件名。

is there any chance of a clash if there is a split second delay between generating the name and creating the file (if I need to create the file myself).

不;这将是一件非常糟糕的事情。

这是我一直用来获取临时文件的例程。

function GetTemporaryFileName:string;
var
Path, FileName: array[0..MAX_PATH] of Char;
begin
Win32Check(GetTempPath(MAX_PATH, Path) <> 0);
Win32Check(GetTempFileName(Path, '~EX', 0, FileName) <> 0);
Result:=String(Filename);
end;

您可以改为使用 JCL 中 JclFileUtils.pas 中的 FileGetTempName( )。

关于multithreading - 我可以根据 ProcessID 和 ThreadID 创建唯一的文件名吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/272783/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com