Teh first one
[mldemos:kalians-mldemos.git] / _AlgorithmsPlugins / KernelMethods / dlib / threads / threads_kernel_2.cpp
1 // Copyright (C) 2003  Davis E. King (davis@dlib.net)\r
2 // License: Boost Software License   See LICENSE.txt for the full license.\r
3 #ifndef DLIB_THREADS_KERNEL_2_CPp_\r
4 #define DLIB_THREADS_KERNEL_2_CPp_\r
5 \r
6 #include "../platform.h"\r
7 \r
8 #ifdef POSIX\r
9 \r
10 #include "threads_kernel_2.h"\r
11 \r
12 \r
13 namespace dlib\r
14 {\r
15     namespace threads_kernel_shared_helpers\r
16     {\r
17 \r
18     // -----------------------------------------------------------------------------------\r
19 \r
20         struct info\r
21         {\r
22             void* param;\r
23             void (*funct)(void*);\r
24         };\r
25 \r
26     // -----------------------------------------------------------------------------------\r
27 \r
28         void* thread_starter (\r
29             void* param\r
30         )\r
31         {\r
32             info* alloc_p = reinterpret_cast<info*>(param);\r
33             info p = *alloc_p;\r
34             delete alloc_p;\r
35 \r
36             // detach self\r
37             pthread_detach(pthread_self());\r
38 \r
39             p.funct(p.param);\r
40             return 0;\r
41         }\r
42 \r
43     // -----------------------------------------------------------------------------------\r
44 \r
45         bool spawn_thread (\r
46             void (*funct)(void*),\r
47             void* param\r
48         )\r
49         {\r
50             info* p;\r
51             try { p = new info; }\r
52             catch (...) { return false; }\r
53 \r
54             p->funct = funct;\r
55             p->param = param;\r
56 \r
57             pthread_t thread_id;\r
58             if ( pthread_create (&thread_id, 0, thread_starter, p) )\r
59             {\r
60                 delete p;\r
61                 return false;\r
62             }\r
63             return true;\r
64         }\r
65 \r
66     // -----------------------------------------------------------------------------------\r
67 \r
68     }\r
69 \r
70 }\r
71 \r
72 #endif // POSIX\r
73 \r
74 #endif // DLIB_THREADS_KERNEL_2_CPp_\r
75 \r