1 // Copyright (C) 2003  Davis E. King (davis@dlib.net)
2 // License: Boost Software License   See LICENSE.txt for the full license.
3 #ifndef DLIB_THREADS_KERNEL_1_CPp_
4 #define DLIB_THREADS_KERNEL_1_CPp_
5 
6 #include "../platform.h"
7 
8 #ifdef WIN32
9 
10 #include "threads_kernel_1.h"
11 
12 #include <process.h>
13 
14 
15 namespace dlib
16 {
17     namespace threads_kernel_shared_helpers
18     {
19 
20     // -----------------------------------------------------------------------------------
21 
22         struct info
23         {
24             void* param;
25             void (*funct)(void*);
26         };
27 
28     // -----------------------------------------------------------------------------------
29 
thread_starter(void * param)30         unsigned int __stdcall thread_starter (
31             void* param
32         )
33         {
34             info* alloc_p = static_cast<info*>(param);
35             info p = *alloc_p;
36             delete alloc_p;
37 
38             p.funct(p.param);
39             return 0;
40         }
41 
42     // -----------------------------------------------------------------------------------
43 
spawn_thread(void (* funct)(void *),void * param)44         bool spawn_thread (
45             void (*funct)(void*),
46             void* param
47         )
48         {
49             info* p;
50             try { p = new info; }
51             catch (...) { return false; }
52 
53             p->funct = funct;
54             p->param = param;
55 
56 
57             unsigned int garbage;
58 
59             HANDLE thandle = (HANDLE)_beginthreadex (NULL,0,thread_starter,p,0,&garbage);
60             // make thread and add it to the pool
61 
62             // return false if _beginthreadex didn't work
63             if ( thandle == 0)
64             {
65                 delete p;
66                 return false;
67             }
68 
69             // throw away the thread handle
70             CloseHandle(thandle);
71             return true;
72         }
73 
74     // -----------------------------------------------------------------------------------
75 
76     }
77 
78 }
79 
80 #endif // WIN32
81 
82 #endif // DLIB_THREADS_KERNEL_1_CPp_
83 
84