问题很简单,但解决方案使我望而却步。我想要调用两个函数并让它们同时运行(在不同的线程中),但我只能调用void function1()
并且void function2()
仅在之后不运行。我为处理器1和2设置了线程亲和性(我有一个多核处理器,希望你也有一个)。
我看到一次只调用一个函数的方式仅仅是因为我只得到function 1
的输出,而通常我会看到function 1
和function 2
的混合。
随意重新调整代码以使其工作成为可能但请尽量保持原始方法的完整性,以及类中线程调用函数的方式。这是完整的代码。
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <iostream>
class thread_class
{
private:
public:
void function1()
{
for(int count = 0; count < 1000; count++)
std::cout<<"function 1"<<std::endl;
}
void function2()
{
for(int count = 0; count < 1000; count++)
std::cout<<"function 2"<<std::endl;
}
thread_class(){}
~thread_class(){}
DWORD_PTR WINAPI threadMain0()
{
function1();
return 0;
}
DWORD_PTR WINAPI threadMain1()
{
function2();
return 0;
}
void thread()
{
HANDLE *m_threads = NULL;
DWORD_PTR c = 2;
m_threads = new HANDLE[c];
DWORD_PTR i = 0;
DWORD_PTR m_id0 = 0;
DWORD_PTR m_mask0 = 1 << i;
m_threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadMain0(), (LPVOID)i, NULL, &m_id0);
SetThreadAffinityMask(m_threads[i], m_mask0);
wprintf(L"Creating Thread %d (0x%08x) Assigning to CPU 0x%08x\r\n", i, (LONG_PTR)m_threads[i], m_mask0);
i = 1;
DWORD_PTR m_id1 = 0;
DWORD_PTR m_mask1 = 1 << i;
m_threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadMain1(), (LPVOID)i, NULL, &m_id1);
SetThreadAffinityMask(m_threads[i], m_mask1);
wprintf(L"Creating Thread %d (0x%08x) Assigning to CPU 0x%08x\r\n", i, (LONG_PTR)m_threads[i], m_mask1);
}
};
int main()
{
thread_class* MAIN_THREADS;
MAIN_THREADS = new thread_class();
MAIN_THREADS->thread();
delete MAIN_THREADS;
return 0;
}
编辑:这是稍微修改后的代码版本,显示其未运行并行。
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <iostream>
class thread_class
{
private:
public:
void function1()
{
int exit = 0;
while(exit == 0)
{
std::cout<<"enter 1 to exit:"<<std::endl;
std::cin>>exit;
};
}
void function2()
{
for(int count = 0; count < 1000; count++)
std::cout<<"function 2"<<std::endl;
}
thread_class(){}
~thread_class(){}
DWORD_PTR WINAPI threadMain0()
{
function1();
return 0;
}
DWORD_PTR WINAPI threadMain1()
{
function2();
return 0;
}
void thread()
{
HANDLE *m_threads = NULL;
DWORD_PTR c = 2;
m_threads = new HANDLE[c];
DWORD_PTR i = 0;
DWORD_PTR m_id0 = 0;
DWORD_PTR m_mask0 = 1 << i;
m_threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadMain0(), (LPVOID)i, NULL, &m_id0);
SetThreadAffinityMask(m_threads[i], m_mask0);
wprintf(L"Creating Thread %d (0x%08x) Assigning to CPU 0x%08x\r\n", i, (LONG_PTR)m_threads[i], m_mask0);
i = 1;
DWORD_PTR m_id1 = 0;
DWORD_PTR m_mask1 = 1 << i;
m_threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadMain1(), (LPVOID)i, NULL, &m_id1);
SetThreadAffinityMask(m_threads[i], m_mask1);
wprintf(L"Creating Thread %d (0x%08x) Assigning to CPU 0x%08x\r\n", i, (LONG_PTR)m_threads[i], m_mask1);
}
};
int main()
{
thread_class* MAIN_THREADS;
MAIN_THREADS = new thread_class();
MAIN_THREADS->thread();
delete MAIN_THREADS;
return 0;
}
答案 0 :(得分:2)
所以,有一些事情:
1)您不能将常规成员函数用作ThreadProc。如果你必须将它转换为编译它可能是错误的。 ThreadProc函数需要是自由的或静态的。它们也有错误的签名,因为ThreadProc只接受一个void *参数。
2)当您真正需要DWORD时,有几个地方使用DWORD_PTR,例如ThreadProc的返回值,c
,i
等。
3)来自CreateProcess
文档:
A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multithreaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.
写给cout
的机会最终会击中CRT。它可能没有,即使它可能没有问题,但如果你这样做是一个好看的地方。
4)I / O根本不保证是交错的,因此写入cout
不是判断线程是否同时运行的好方法。我已经为线程添加了一些Sleep
调用,并且最初也创建了它们,因此我可以尽可能地将它们放在一起,以使I / O看起来像是交错的,但这可能只是巧合。一旦我确实看到你可能也是正确的,当线程被启动时,打印的字符串和endl
没有相互连接,这就是我看到两个字符串后跟两个行结束。之后,它有点交错。
5)你总是希望在从它们下面删除类之前等待线程退出。您通常也希望在完成后关闭手柄。
我删除了构造函数/析构函数,因为它们是空的和其他绒毛只是为了尽可能地缩短它。
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <iostream>
class thread_class
{
public:
void function1()
{
Sleep(0);
for(int count = 0; count < 10; count++)
{
std::cout<<"function 1"<<std::endl;
Sleep(0);
}
}
void function2()
{
Sleep(0);
for(int count = 0; count < 10; count++)
{
std::cout<<"function 2"<<std::endl;
Sleep(0);
}
}
static DWORD WINAPI threadMain0(LPVOID param)
{
thread_class* This = static_cast<thread_class*>(param);
This->function1();
return 0;
}
static DWORD WINAPI threadMain1(LPVOID param)
{
thread_class* This = static_cast<thread_class*>(param);
This->function2();
return 0;
}
void thread()
{
HANDLE m_threads[2] = {};
DWORD threadIDs[2] = {};
LPTHREAD_START_ROUTINE threadProcs[2] = {threadMain0, threadMain1};
DWORD_PTR mask = 0;
for(int i = 0; i < 2; ++i)
{
m_threads[i] = CreateThread(NULL, 0, threadProcs[i], this, CREATE_SUSPENDED, &threadIDs[i]);
mask = 1 << i;
SetThreadAffinityMask(m_threads[i], mask);
wprintf(L"Creating Thread %d (0x%08p) Assigning to CPU 0x%08p\r\n", i, m_threads[i], mask);
}
for(int i = 0; i < 2; ++i)
{
ResumeThread(m_threads[i]);
}
WaitForMultipleObjects(2, m_threads, TRUE, INFINITE);
for(int i = 0; i < 2; ++i)
{
CloseHandle(m_threads[i]);
}
}
};
int main()
{
thread_class* MAIN_THREADS;
MAIN_THREADS = new thread_class();
MAIN_THREADS->thread();
delete MAIN_THREADS;
return 0;
}
答案 1 :(得分:1)
我在代码的两个版本中看到的问题之一是NULL传递给CreateThread()函数的dwCreationFlags参数。这意味着创建的线程将在CreateThread()退出后立即启动(如果您配备了多核CPU和幸运计划,则更快)。所以看起来你的第一个线程在第二个线程有机会出生之前完成了他的工作(因为线程创建在时间方面是几十或几百毫秒的costy操作)。
您可以尝试以下方法: