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
Administrator
Administrator
Zitat von »"rewb0rn"«
aso das wusste ich nicht, das konzept habe ich aus der uni und da haben wir nur mit c an unserem eigenen betriebssystem gearbeitet viel anders werden die das aber intern auch nicht machen denke ich.
Zitat von »"rewb0rn"«
kannst du ein beispiel dafür geben wie das dann aussehn würde? man muss ja den eintritt in criticalsection auch noch absichern.
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 |
void readlock() { if(InterlockedIncrement(&Readers)) EnterCriticalSection(CriticalSection); } void readunlock() { if(!InterlockedDecrement(&Readers)) LeaveCriticalSection(CriticalSection); } |
C-/C++-Quelltext |
|
1 2 |
__atomic_add __exchange_and_add |
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 |
struct ScopedLock; struct Lockable { friend struct ScopedLock; virtual ~Lockable() {} private: virtual void Lock() = 0; virtual void Unlock() = 0; }; struct ScopedLock : private Uncopyable { ScopedLock( Lockable& obj ) : locked( obj ) { locked.Lock(); } ~ScopedLock() { locked.Unlock(); } private: Lockable& locked; }; class LockPolicyMutex : Uncopyable { public: LockPolicyMutex() { hMutex = CreateMutex( 0, false, 0 ); } ~LockPolicyMutex() { CloseHandle( hMutex ); } void Lock() { WaitForSingleObject( hMutex, INFINITE ); } void Unlock() { ReleaseMutex( hMutex ); } private: HANDLE hMutex; }; class LockPolicyCriticalSection : Uncopyable { public: LockPolicyCriticalSection() { InitializeCriticalSection( &cs ); } ~LockPolicyCriticalSection() { DeleteCriticalSection( &cs ); } void DoLock() { EnterCriticalSection( &cs ); } void DoUnlock() { LeaveCriticalSection( &cs ); } private: CRITICAL_SECTION cs; }; template< class LockPolicy = LockPolicyCriticalSection > class Mutex : Uncopyable, public Lockable { public: Mutex() {} private: void Lock() { locker.DoLock(); } void Unlock() { locker.DoUnlock(); } LockPolicy locker; }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
/* utility */ struct Uncopyable { protected: Uncopyable() {} private: Uncopyable( const Uncopyable& ); Uncopyable& operator=( const Uncopyable& ); }; |
C-/C++-Quelltext |
|
1 2 3 |
Mutex<> x; ScopedLock lock( x ); |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 |
inline void atomic_add( int* p ) { _asm mov esi, [p] _asm lock inc dword ptr[esi] } inline void atomic_dec( int* p ) { _asm mov esi, [p] _asm lock dec dword ptr[esi] } |
Zitat von »"Theprogrammer"«
Hey,
ich hab nur mal ne kleine Anmerkung:
Wenn ich Scott Meyers Buch richtig in Erinngerung habe sollte man unter allen Umständen vermeiden von Containerklassen wie list und co. zu erben! ( siehe Effektiv C++ Programmieren S. 60 )
mfg
TheProgrammer
Sicaine
unregistriert
Werbeanzeige