Knight online için yapmış olduğum bir programdı. KnightOnline.exe çalışmaya başladıktan sonra ilk açılan 2 thread…
Author: damrakoc
Açılmayan harici diski açınca, kurcalarken, 17 nisan 2013 tarihinde okulda ödev olarak verilen programın koduna hiç dokunmadan atayım…
The 2015 MSVC C++ runtime’s std::mutex (and potentially other) implementation is significantly slower than the…
Ne yazık ki Boost dökümanları (http://www.boost.org/doc/libs/1_65_0/more/getting_started/windows.html) oldukça ayrıntılı çünkü tüm OS ve ortamlara dikkat etmeye çalışılıyor. İşte…
Merhaba arkadaşlar. Bu yazıda, sizlere “Boost C++” kütüphanesi hakkında genel bilgiler vermeye çalışacağım. İleriki zamanlarda…
Öncelikle yapılması gerekenler, Visual Studio 2017, Tools -> NuGet Package Manager -> Package Manager Console…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#pragma once #include <condition_variable> #include <deque> class Condition { public: Condition(); ~Condition(); void BeginSynchronized(); void EndSynchronized(); uint32_t Wait(time_t timeout); uint32_t Wait(); void Signal(); void Broadcast(); private: int m_nLockCount; std::condition_variable m_condition; std::mutex m_lock; }; |
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 |
#include "stdafx.h" #include "Condition.h" Condition::Condition() : m_nLockCount(0) {} void Condition::BeginSynchronized() { m_lock.lock(); ++m_nLockCount; } void Condition::EndSynchronized() { --m_nLockCount; m_lock.unlock(); } uint32_t Condition::Wait(time_t timeout) { std::unique_lock<std::mutex> lock(m_lock); m_condition.wait_for(lock, std::chrono::milliseconds(timeout)); return 0; } uint32_t Condition::Wait() { std::unique_lock<std::mutex> lock(m_lock); m_condition.wait(lock); return 0; } void Condition::Signal() { m_condition.notify_one(); } void Condition::Broadcast() { m_condition.notify_all(); } Condition::~Condition() {} |
Usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#pragma once #include "Condition.h" class RWLock { public: RWLock(); void AcquireReadLock(); void ReleaseReadLock(); void AcquireWriteLock(); void ReleaseWriteLock(); private: Condition _cond; volatile uint32_t _readers; volatile uint32_t _writers; }; |
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 |
#include "stdafx.h" #include "RWLock.h" RWLock::RWLock() { _readers = _writers = 0; } void RWLock::AcquireReadLock() { _cond.BeginSynchronized(); _readers++; _cond.EndSynchronized(); } void RWLock::ReleaseReadLock() { _cond.BeginSynchronized(); if (!(--_readers)) if (_writers) _cond.Signal(); _cond.EndSynchronized(); } void RWLock::AcquireWriteLock() { _cond.BeginSynchronized(); _writers++; if (_readers) _cond.Wait(); } void RWLock::ReleaseWriteLock() { if (--_writers) _cond.Signal(); _cond.EndSynchronized(); } |
Yazılım ömrü döngüsündeki gereksinimler, mimari model, tasarım özellikleri, kaynak kodu ve test komut dosyaları gibi…
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 |
public enum RecyclerViewLayoutManager { LINEAR { public ViewGroup.LayoutParams getLayoutParams() { return new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } ; }, GRID { public ViewGroup.LayoutParams getLayoutParams() { return new GridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } ; }, STAGGERED { public ViewGroup.LayoutParams getLayoutParams() { return new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } ; }; public abstract ViewGroup.LayoutParams getLayoutParams(); } |
Thread Safe Singleton Abstract class
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 |
package com.damrakoc.java; import android.util.Log; import java.lang.reflect.Constructor; import java.util.HashMap; import java.util.Map; /** * Created by damra on 08/08/2017. * USAGE: public class TestSingleton extends ThreadSafeSingleton<TestSingleton> { public static TestSingleton getInstance() { return ThreadSafeSingleton.getInstance(TestSingleton.class); } } */ @SuppressWarnings({"all"}) public abstract class ThreadSafeSingleton<T> { private static final Map<Class<? extends ThreadSafeSingleton>, ThreadSafeSingleton> INSTANCES = new HashMap<>(); private static final String TAG = ThreadSafeSingleton.class.getSimpleName(); private static class SingletonHolder<T> { private static <T> T getInstance(Class<T> clazz){ Constructor<T> constructor = (Constructor<T>) clazz.getDeclaredConstructors()[0]; constructor.setAccessible(true); try{ return constructor.newInstance(null); }catch (Exception e){ Log.d(TAG, "getInstance: "+e.getLocalizedMessage()); return null; } } } protected ThreadSafeSingleton() { Log.d(TAG, "Initializing ThreadSafeSingleton"); } protected static <T extends ThreadSafeSingleton<T>> T getInstance(Class<T> clazz) { if(INSTANCES.containsKey(clazz)) { return (T) INSTANCES.get(clazz); } else { try{ T instance = SingletonHolder.getInstance(clazz); INSTANCES.put(clazz, instance); return instance; }catch (Exception e ){ return null; } } } protected static <T extends ThreadSafeSingleton<T>> void putInstance(Class<T> clazz, T instance) { if(!INSTANCES.containsKey(clazz)) { INSTANCES.put(clazz, instance); } } } |