Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
#include <list> #include <vector> #if defined(_WIN32) # include <windows.h> #endif enum EContainerLocks { CONTAINERLOCK_NONE, CONTAINERLOCK_READ, CONTAINERLOCK_WRITE, }; template < template < class T, class Allocator = std::allocator<T> > class C > class LockableContainer { public: LockableContainer() : Lock_(CONTAINERLOCK_NONE), LockCount_(0) { } LockableContainer(const C &other) : List(other), Lock_(CONTAINERLOCK_NONE), LockCount_(0) { } ~LockableContainer() { } /* Functions */ void lock(const EContainerLocks Lock) { if (Lock != CONTAINERLOCK_NONE) { /* Wait until the list is unlocked */ if (Lock == CONTAINERLOCK_WRITE) { while (Locked_ != CONTAINERLOCK_NONE) yield(); } else if (Lock == CONTAINERLOCK_READ) { while (Locked_ == CONTAINERLOCK_WRITE) yield(); } /* Set new lock mode and increment lock count */ Locked_ = Lock; ++LockCount_; } } void unlock() { if (LockCount_) { --LockCount_; if (!LockCount_) Locked_ = CONTAINERLOCK_NONE; } } /* Inline functions */ inline EContainerLocks mode() const { return Locked_; } inline bool locked() const { return Locked_ == CONTAINERLOCK_WRITE; } /* Members */ C List; private: /* Functions */ inline void yield() { #if defined(_WIN32) Sleep(1); #elif defined(__linux__) usleep(1000); #endif } /* Members */ EContainerLocks Lock_; unsigned int LockCount_; }; template < class T, class Allocator = std::allocator<T> > typedef LockableContainer< std::list<T, Allocator> > LockableList; template < class T, class Allocator = std::allocator<T> > typedef LockableContainer< std::vector<T, Allocator> > LockableVector; /* Example of how to use: LockableList<int> MyList; MyList.lock(CONTAINERLOCK_WRITE); // <- waits here until the list is ready to be written MyList.List.push_back(rand() % 100); MyList.List.push_back(rand() % 100); MyList.List.push_back(rand() % 100); MyList.unlock(); */ |
Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von »LukasBanana« (02.08.2011, 10:49)
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
// Lockable list #include <list> #include <vector> #if defined(_WIN32) # include <windows.h> #endif template < template < class T, class Allocator = std::allocator<T> > class C > class LockableContainer { public: LockableContainer() { createMutex(); } LockableContainer(const C &other) : List(other) { createMutex(); } ~LockableContainer() { } /* Functions */ void lock(const EContainerLocks Lock) { /* Wait until the list is unlocked */ while (WaitForSingleObject(Mutex_, INFINITE) == WAIT_OBJECT_0) yield(); } void unlock() { /* Unlock list -> release mutex */ ReleaseMutex(Mutex_); } /* Members */ C List; private: /* Functions */ inline void yield() { #if defined(_WIN32) Sleep(1); #elif defined(__linux__) usleep(1000); #endif } inline void createMutex() { Mutex_ = CreateMutex(NULL, FALSE, "Mutex1"); } /* Members */ HANDLE Mutex_; }; template < class T, class Allocator = std::allocator<T> > typedef LockableContainer< std::list<T, Allocator> > LockableList; template < class T, class Allocator = std::allocator<T> > typedef LockableContainer< std::vector<T, Allocator> > LockableVector; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#ifdef _WIN32 class MY_CRITICALSECTION { mutable CRITICAL_SECTION critcal; void operator = (const MY_CRITICALSECTION&) {} public: MY_CRITICALSECTION(const MY_CRITICALSECTION&) { InitializeCriticalSection(&critcal); } MY_CRITICALSECTION(void) { InitializeCriticalSection(&critcal); } ~MY_CRITICALSECTION(void) { DeleteCriticalSection(&critcal); } void Lock(void) const { EnterCriticalSection(&critcal); } void Unlock(void) const { LeaveCriticalSection(&critcal); } }; #else class MY_CRITICALSECTION { mutable pthread_mutex_t critcal; void operator = (const MY_CRITICALSECTION&) {} void create(void) { pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&critcal, &attr); } public: MY_CRITICALSECTION(const MY_CRITICALSECTION&) { create(); } MY_CRITICALSECTION(void) { create(); } ~MY_CRITICALSECTION(void) { pthread_mutex_destroy(&critcal); } void Lock(void) const { pthread_mutex_lock(&critcal); } void Unlock(void) const { pthread_mutex_unlock(&critcal); } }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
#pragma once #ifndef DONT_INCLUDE_HEADERS #include "Portable.h" #include <list> #endif namespace helpers { //!threadsafe (but not exceptionsafe!) std::list template <class T> class SecureList : private std::list<T> { MY_CRITICALSECTION Critic; void operator = (const SecureList&) { } public: typedef typename std::list<T> p; typedef typename p::iterator iterator; typedef typename p::const_iterator citerator; typedef typename p::size_type size_type; SecureList(const p &c) { p::insert(begin(true), c.begin(), c.end()); unlock(); } SecureList(const SecureList &c) : p() { p::insert(begin(true), c.begin(true), c.end()); c.unlock(); unlock(); } SecureList(void) { } ~SecureList() { } //! calls lock of the criticalsection /mutex void lock(void) const { Critic.Lock(); } //! calls unlock of the criticalsection /mutex void unlock(void) const { Critic.Unlock(); } //! calls lock, std::list::begin and returns the result citerator begin(bool lockit) const { if(lockit) lock(); return p::begin(); } //!calls std::list::end and returns the result citerator end(void) const { return p::end(); } //! calls lock, std::list::begin and returns the result iterator begin(bool lockit) { if(lockit) lock(); return p::begin(); } //!calls std::list::end and returns the result iterator end(void) { return p::end(); } //!calls std::list::erase. Container should be locked before iterator erase(iterator it) { return p::erase(it); } //! calls lock, std::list::clear and unlock void clear(bool lockit) { if(lockit) lock(); p::clear(); if(lockit) unlock(); } //! calls lock, std::list::push_back and unlock void push_back(const T& e, bool lockit) { if(lockit) lock(); p::push_back(e); if(lockit) unlock(); } //! calls lock, std::list::push_front and unlock void push_front(const T& e, bool lockit) { if(lockit) lock(); p::push_front(e); if(lockit) unlock(); } //! calls lock, std::list::remove and unlock void remove(const T& e, bool lockit) { if(lockit) lock(); p::remove(e); if(lockit) unlock(); } //! calls lock, std::list::sort and unlock void sort(bool lockit) { if(lockit) lock(); p::sort(); if(lockit) unlock(); } //! calls lock, std::list::unique and unlock void unique(bool lockit) { if(lockit) lock(); p::unique(); if(lockit) unlock(); } //! calls lock, std::list::splice and unlock void spliceIn(p &list, bool lockit) { if(lockit) lock(); list.splice(list.begin(), *this); if(lockit) unlock(); } //! calls lock, std::list::merge and unlock void merge(p &list, bool lockit) { if(lockit) lock(); p::merge(list); if(lockit) unlock(); } //! calls lock for both, std::list::splice and unlock void splice(SecureList<T> &list, bool lockit) { if(lockit) { lock(); list.lock(); } p::splice(begin(false), list); if(lockit) { list.unlock(); unlock(); } } //! calls lock for both, std::list::merge and unlock void merge(SecureList<T> &list, bool lockit) { if(lockit) { lock(); list.lock(); } p::merge(list); if(lockit) { list.unlock(); unlock(); } } //! calls std::list::size and returns the result size_type size(void) const { return p::size(); } //! calls std::list::empty and returns the result bool empty(void) const { return p::empty(); } //template<class _Iter> void insert(iterator _Where, _Iter _First, _Iter _Last) //{ // std::list<T>::insert(_Where, _First, _Last); //} //! calls lock, std::list::insert and returns the result iterator insert(iterator _Where, const T& _Val, bool lockit) { if(lockit) lock(); return p::insert(_Where, _Val); } }; }; |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Nox« (03.08.2011, 11:31)
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 |
template <typename T> class MyList : private std::list<T> { /* ... */ void clear(bool lockit = true) // MIT DEFAULT WERT { std::list<T>::clear(); } /* ... */ } |
Werbeanzeige