History

Use the Windows Runtime ThreadPool API for creating threads on Windows Phone

Windows Phone lacks the old CreateThread/beginthreadex APIs for creating threads. (Technically, the functions still do exist, but they aren't officially supported and aren't visible in the headers when targeting Windows Phone.) Building code that uses the Windows Runtime language extensions requires building with the -ZW option.

Martin Storsjö committed 12 years ago

Commit b35c2121 parent f293d262 files · +17 −0Browse files

Showing the changes to codec/common/src/WelsThreadLib.cpp (1 of 2 files). Show all

Unified · Split

codec/common/src/WelsThreadLib.cpp+16−0
@@ -68,6 +68,11 @@ · 67 unmodified lines
6868
6969 #ifdef WINAPI_FAMILY
7070 #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
71+using namespace Platform;
72+using namespace Windows::Foundation;
73+using namespace Windows::System::Threading;
74+#define USE_THREADPOOL
75+
7176 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
7277 #define GetSystemInfo(x) GetNativeSystemInfo(x)
7378 #define CreateEvent(attr, reset, init, name) CreateEventEx(attr, name, ((reset) ? CREATE_EVENT_MANUAL_RESET : 0) | ((init) ? CREATE_EVENT_INITIAL_SET : 0), EVENT_ALL_ACCESS)
@@ -169,7 +174,18 @@ WELS_THREAD_ERROR_CODE WelsEventClose (WELS_EVENT* event, const char* event_n · 95 unmodified lines
169174
170175 WELS_THREAD_ERROR_CODE WelsThreadCreate (WELS_THREAD_HANDLE* thread, LPWELS_THREAD_ROUTINE routine,
171176 void* arg, WELS_THREAD_ATTR attr) {
177+#ifdef USE_THREADPOOL
178+ HANDLE h = CreateEvent(NULL, FALSE, FALSE, NULL);
179+ HANDLE h2;
180+ DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), &h2, 0, FALSE, DUPLICATE_SAME_ACCESS);
181+ ThreadPool::RunAsync(ref new WorkItemHandler([=](IAsyncAction^) {
182+ routine(arg);
183+ SetEvent(h2);
184+ CloseHandle(h2);
185+ }, CallbackContext::Any), WorkItemPriority::Normal, WorkItemOptions::TimeSliced);
186+#else
172187 WELS_THREAD_HANDLE h = CreateThread (NULL, 0, routine, arg, 0, NULL);
188+#endif
173189
174190 if (h == NULL) {
175191 return WELS_THREAD_ERROR_GENERAL;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t