Ok I took a look and the implementation everything seem very good with one exception, there Semaphore is emulated with a mutex in this function.
- Code: Select all
inline void dgThread::dgCriticalSection::Lock()
{
#ifndef DG_USE_THREAD_EMULATION
#ifdef DG_USE_MUTEX_CRITICAL_SECTION
m_mutex.lock():
#else
while (m_mutex.exchange(1)) {
std::this_thread::yield();
}
#endif
#endif
}
although they do the same job, the do it in a very different way. Semaphore are designed to be real time object while mutex aren't
a semaphore remove a thread for the circular list of thread that the OS is running.
Mutex do not do that, the system is still visiting the tread, this is not different than using Sleep (0) or Yield with some atomic to do the suspension.
in the loop the system is still calling the loop and doing all the work of this_thread::yield(); wick is a lot, the result is that the tread code has those random spike each time '
the Mutex coge in an out of the system, a Semaphore the thread is not suspended, it is simple removed form the list of thread the system gives time slices.
the m_mutex.lock(): will never come back because the OS never run the tread at all, it is some other thread that has to put the tread back but Semaphore Release.
I will see if the semaphore are supported, because I would love to get rid of the pthread dependency.
Semaphores are supported native in Window and Linux, an dI though OS ten did no but after some research I found out that OSX in fact supported semaphore, they just make them uniquely name,
meaning each one have to have a unique OS name, other wise the OS give you a instance of the and existing one.
I did this in this functions
- Code: Select all
dgThread::dgSemaphore::dgSemaphore ()
{
#if defined (_MACOSX_VER) || defined (IOS) || defined (__APPLE__)
char temp[256];
static int semID = 1;
m_nameId = semID ++;
sprintf (temp, "%s%d", DG_SEMAPHORE_NAME, m_nameId);
sem_unlink(temp);
m_sem = sem_open(temp, O_CREAT, S_IRUSR | S_IWUSR, 0);
#else
sem_init (&m_sem, 0, 0);
#endif
}
I am sure Semaphore C++ 11 must have them, or even should be available because there are use a lot in window an Linux.
using Semaphore instead of a Mute to emulate them make a bug different on how smooth the thread switch operates.
It took me a long time to figure out why the thread was never smooth an dis was because originally I did the same you did. and I always had those spikes.
I will do more research to see if I can figure out how to get the Semaphore, because would love to get rid of the pthread dependency in windows. plus I also love the standardization of Atomics.
notice that windows is the only OS that do not support then natively.
The other problem is how well accepted is C++ 11. I still have people how build newton with VS 2008.
I myself use VS 2010 maybe it and this is VS 2013 minimum?