如何在WinRT的并行线程中执行c ++函数? [英] How to execute c++ function in a parallel thread under WinRT?
问题描述
我有一个C ++代码,它使用_beginthreadex()Windows方法在线程中执行一个函数。现在我想把它移植到WinRT组件,以包括它在Windows Phone应用程序。但Windows手机不支持_beginthreadex()。我如何做呢?
I have a C++ code which uses _beginthreadex() Windows method to execute a function in a thread. Now I want to port it to WinRT component to include it in windows phone app. But windows phone does't support _beginthreadex(). How do I do it?
我的功能是:
bool doesWordAppearInDictionarry(char* word);
我的计算机上有4个内核,所以我要并行执行这个函数的4个副本
I have 4 cores on my computer so I want to execute 4 copies of this function in parallel (searching for 4 different words in the dictionary simultaneously).
我阅读了(此处)和( here )关于 Windows :: System :: Threading :: WorkItemHandler
, ThreadPool
s和 IAsyncAction
但是提供的示例激活托管代码,不调用原生函数。
I read (here) and (here) about Windows::System::Threading::WorkItemHandler
, ThreadPool
's and IAsyncAction
But the supplied examples activate managed code and do not call a native function.
我正在寻找的是一个干净的解决方案(最小量的代码行),将取代我目前的Windows桌面代码:
What I am looking for is a clean solution (minimal amount of lines of code) which will replace my current Windows Desktop code:
for (int i=0; i<4; i++){
tInfo[i].id = (HANDLE)_beginthreadex( NULL, stack_size, doesWordAppearInDictionarry,tInfo, 0, word[i]);
}
for (int i=0; i<4; i++){
WaitForSingleObject( tInfo[i].id, INFINITE );
CloseHandle(tInfo[i].id);
}
推荐答案
使用WinRT api模拟_beginthreadex()的代码行很少。
Here is a short solution: Few lines of code that emulate _beginthreadex() using WinRT api.
using namespace Platform;
using namespace Windows::System::Threading;
_Use_decl_annotations_ HANDLE WINAPI _beginthreadex(LPSECURITY_ATTRIBUTES unusedThreadAttributes, SIZE_T unusedStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD unusedThreadId){
// Create a handle for the new thread.
HANDLE threadHandle = CreateEventEx(nullptr, nullptr, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
if (!threadHandle)
return nullptr;
try{
auto workItemHandler = ref new WorkItemHandler([=](IAsyncAction^){
lpStartAddress(lpParameter);
SetEvent(threadHandle); // Signal that the thread has completed (assuming this handle was not destroyed by the caller due to time-out).
}, CallbackContext::Any);
ThreadPool::RunAsync(workItemHandler, WorkItemPriority::High, WorkItemOptions::TimeSliced);
return threadHandle; // Return the handle to the caller (he can wait for this handle until thread terminates).
}
catch (...){
CloseHandle(threadHandle); // Clean up if thread creation fails.
return nullptr;
}
}
此解决方案基于堆栈溢出应答href =http://stackoverflow.com/questions/15839895/porting-createthread-calls-to-win8-winrt-app>这里),其中讨论( this )blog。博客包括对线程的完全仿真,包括CreateThread()Win32 api,在线程运行时共享内存,在线程之间共享内存。我的解决方案是完整模拟器的简化情况。
This solution is based on stack overflow answer (here) which discusses (this) blog. The blog includes a full emulation of threads including the CreateThread() Win32 api, accessing threads while they are running and sharing memory between threads. My solution is a simplified case of the full emulator.
P.s。调用者方法必须等待线程使用 WaitForSingleObjectEx(
)方法,而不是 WaitForSingleObject()
P.s. The caller method must waid for the threads using WaitForSingleObjectEx(
) method and not WaitForSingleObject()
这篇关于如何在WinRT的并行线程中执行c ++函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!