我的程序有两个线程,在一个双CPU的机器上,如何强制它们在两个CPU中运行?

opentuxedo 2003-10-16 03:53:55
如上
...全文
238 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
opentuxedo 2003-10-16
  • 打赏
  • 举报
回复
同样是五角星,看来我的不如人家的坚挺
vcforever 2003-10-16
  • 打赏
  • 举报
回复
同意akiko(弥弥):
DWORD_PTR SetThreadAffinityMask (
HANDLE hThread, // handle to thread
DWORD_PTR dwThreadAffinityMask // thread affinity mask
);

补充一点:
在创建线程的时候必须指定线程的安全属性和访问权限,在创建线程的时候安全属性结构中
必须包含THREAD_SET_INFORMATION THREAD_QUERY_INFORMATION这两个标志!只有包含了这两个标志,才能针对此线程使用 SetThreadAffinityMask()这个函数!
opentuxedo 2003-10-16
  • 打赏
  • 举报
回复
多谢弥弥,150分先给你记上。
wuxfBrave 2003-10-16
  • 打赏
  • 举报
回复
The SetThreadAffinityMask function sets a processor affinity mask for the specified thread.


DWORD_PTR SetThreadAffinityMask(
HANDLE hThread,
DWORD_PTR dwThreadAffinityMask
);

Parameters
hThread
[in] Handle to the thread whose affinity mask is to be set.
This handle must have the THREAD_SET_INFORMATION and THREAD_QUERY_INFORMATION access rights. For more information, see Thread Security and Access Rights.

dwThreadAffinityMask
[in] Affinity mask for the thread.

Windows Me/98/95: This value must be 1.


Return Values
If the function succeeds, the return value is the thread's previous affinity mask.


Windows Me/98/95: The return value is 1. To succeed, hThread must be valid and dwThreadAffinityMask must be 1.


If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
A thread affinity mask is a bit vector in which each bit represents the processors that a thread is allowed to run on.

A thread affinity mask must be a proper subset of the process affinity mask for the containing process of a thread. A thread is only allowed to run on the processors its process is allowed to run on.

Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation 3.5 and later, Windows Me, Windows 98, and Windows 95.
Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT Server 3.5 and later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.


akiko 2003-10-16
  • 打赏
  • 举报
回复
dwThreadAffinityMask从右数第几位为1,就允许线程在第几个CPU上运行,如果你只设置了一位,就只能在这个CPU上运行
akiko 2003-10-16
  • 打赏
  • 举报
回复
DWORD_PTR SetThreadAffinityMask(HANDLE hThread,DWORD_PTR dwThreadAffinityMask);

15,472

社区成员

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

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