Last change
on this file since 105 was
104,
checked in by sz, 11 years ago
|
introducing object de/serialization - see serialtest.cpp
the core GDK classes can be now used in multiple threads (ifdef MULTITHREADED)
|
-
Property svn:eol-style set to
native
|
File size:
709 bytes
|
Line | |
---|
1 | #ifndef _THREADS_H_ |
---|
2 | #define _THREADS_H_ |
---|
3 | |
---|
4 | #ifdef MULTITHREADED |
---|
5 | |
---|
6 | #include <pthread.h> |
---|
7 | |
---|
8 | template<class T> class ThreadSingleton |
---|
9 | { |
---|
10 | pthread_key_t mt_key; |
---|
11 | |
---|
12 | public: |
---|
13 | |
---|
14 | ThreadSingleton() |
---|
15 | { |
---|
16 | pthread_key_create(&mt_key,&destructor); |
---|
17 | } |
---|
18 | |
---|
19 | static void destructor(void* o) |
---|
20 | { |
---|
21 | if (o) |
---|
22 | delete (T*)o; |
---|
23 | } |
---|
24 | |
---|
25 | T* set(T* new_o) |
---|
26 | { |
---|
27 | T* o=(T*)pthread_getspecific(mt_key); |
---|
28 | pthread_setspecific(mt_key,new_o); |
---|
29 | return o; |
---|
30 | } |
---|
31 | |
---|
32 | T* get() |
---|
33 | { |
---|
34 | T* o=(T*)pthread_getspecific(mt_key); |
---|
35 | if (!o) |
---|
36 | { |
---|
37 | o=new T(); |
---|
38 | pthread_setspecific(mt_key,o); |
---|
39 | } |
---|
40 | return o; |
---|
41 | } |
---|
42 | |
---|
43 | T& getref() {return *get();} |
---|
44 | }; |
---|
45 | |
---|
46 | #else |
---|
47 | |
---|
48 | template<class T> class ThreadSingleton |
---|
49 | { |
---|
50 | T object; |
---|
51 | |
---|
52 | public: |
---|
53 | |
---|
54 | T* get() {return &object;} |
---|
55 | T& getref() {return object;} |
---|
56 | }; |
---|
57 | |
---|
58 | #endif |
---|
59 | |
---|
60 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.