Raised thread error with our own application (source code herebelow) , trap into the call to _beginthreadex .
but can not provide errno since protection prevent from modifying VC thread file.
Microsoft Visual Studio Professional 2019 Version 16.11.20
VisualStudio.16.Release/16.11.20+32929.386
Microsoft .NET Framework Version 4.8.04084
Version installée : Professional
Microsoft Visual C++ 2019 00435-20050-35417-AA319
System Windows 10 professional build 19041.1415
pragma once
include <functional>
include <chrono>
include <mutex>
include <map>
include <thread>
using namespace std;
template<class callable>
class timerHandler
{
public:
template <class timerevent>
timerHandler(int timerId, long after, timerevent* pclass, void (timerevent::* f) (int))
{
m_pobjects<timerevent>[timerId] = pclass;
m_pmemfuncs<timerevent>[timerId] = f;
m_period = after;
m_timerId = timerId;
wakeup = true;
}
template <class timerevent>
void start()
{
std::this_thread::sleep_for(std::chrono::milliseconds(m_period));
long cr = WaitForSingleObject(runningtimers_mutex, INFINITE);
if (wakeup == true)
(m_pobjects<timerevent>[m_timerId]->*m_pmemfuncs<timerevent>[m_timerId])(m_timerId);
runningtimers_map<callable>.erase(m_timerId);
cr = ReleaseMutex(runningtimers_mutex);
}
void stop(void)
{
wakeup = false;
}
long m_period;
int m_timerId;
bool wakeup;
};
template <class timerevent>
static map <int, timerevent*> m_pobjects;
template <class timerevent>
static map <int, void (timerevent::*)(int)> m_pmemfuncs;
template<class callable>
static std::map <int, timerHandler <callable>*> runningtimers_map;
static HANDLE runningtimers_mutex;
template<class callable>
class C_Timer {
public:
C_Timer()
{}
void stop(int timerId)
{
long cr = WaitForSingleObject(runningtimers_mutex, INFINITE);
if (runningtimers_map <callable>.count(timerId) == 1)
runningtimers_map <callable>[timerId]->stop();
cr = ReleaseMutex(runningtimers_mutex);
}
/////////////////////////////////////////////
//
// start
// lance le timer pour une durée de after ms
// sur timer échu, appelle la fonction void (callable::*f) (int) désigné par la classe template
// callable* pclass
//
/////////////////////////////////////////////
template <class timerevent>
int start(int after, timerevent* pclass, void (timerevent::* f) (int))
{
if (runningtimers_mutex == nullptr)
runningtimers_mutex = CreateMutex(NULL, FALSE, NULL);
long cr = WaitForSingleObject(runningtimers_mutex, INFINITE);
auto iter = runningtimers_map <callable>.begin();
int i = 0;
for (; iter != runningtimers_map <callable>.end(); iter++)
{
if (iter->second == nullptr)
{
m_timerId = i; break;
}
else
i++;
}
if (iter == runningtimers_map<callable>.end())
m_timerId = i;
m_ptimerHdl = new timerHandler <callable>(m_timerId, after, pclass, f);
if (iter == runningtimers_map<callable>.end())
{
runningtimers_map<callable>.insert({ i, m_ptimerHdl });
}
else
{
runningtimers_map<callable>[i] = m_ptimerHdl;
}
thread t(&timerHandler <callable> ::start<timerevent>, m_ptimerHdl); << trap here somtimes>>
t.detach();
cr = ReleaseMutex(runningtimers_mutex);
return i;
}
private:
timerHandler <callable>* m_ptimerHdl;
int m_timerId;
};