Knowledge Base Nr: 00228 timeout.cpp - http://www.swe-kaiser.de

Downloads:

linux: timeouthandling für beliebige vorgänge

  
#include <setjmp.h>
#include <signal.h>

static jmp_buf g_progzust_timeout;

static void sig_alarm_handler(int /*sig_nr*/)
{
printf("ERROR: sig_alarm_handler() called!");
longjmp(g_progzust_timeout, 1);
}

int CDummyClass::DummyFunc(int nAddr, short* pnValue, int nWordCount)
{
if (signal(SIGALRM, sig_alarm_handler) == SIG_ERR)
{
printf("ERROR: signal(SIGALARM, sig_alarm_handler) failed!");
return -11;
}
if (setjmp(g_progzust_timeout) != 0)
{
printf("ERROR: Timeout CDummyClass::DummyFunc(%d, %p, %d)?!", nAddr, pnValue, nWordCount);
return -12;
}

alarm(1);
int nResult = m_mbusProtocol.readMultipleRegisters(1, nAddr, pnValue, nWordCount);
alarm(0);

if (nResult != FTALK_SUCCESS)
{
printf("ERROR: CDummyClass::DummyFunc(): %s!\n", getErrorText(nResult));
return -1;
}

return 0;
}