qse/samples/si/thr01.c

144 lines
2.4 KiB
C
Raw Normal View History

2018-01-26 10:03:58 +00:00
#include <qse/si/thr.h>
#include <qse/si/sio.h>
#include <qse/si/os.h>
2018-01-26 10:03:58 +00:00
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
2018-01-26 10:03:58 +00:00
#if defined(_WIN32)
# include <windows.h>
#endif
#include <locale.h>
2018-01-26 10:03:58 +00:00
#include <signal.h>
#include <string.h>
static int g_stopreq = 0;
static qse_ntime_t sleep_interval = { 1, 0 };
2018-01-26 10:03:58 +00:00
struct thr_xtn_t
{
int stopreq;
};
typedef struct thr_xtn_t thr_xtn_t;
2018-01-28 04:51:22 +00:00
static int thr_func (qse_thr_t* thr, void* ctx)
2018-01-26 10:03:58 +00:00
{
int i = 0;
thr_xtn_t* xtn = qse_thr_getxtn(thr);
2018-01-26 10:03:58 +00:00
while (!xtn->stopreq)
{
qse_printf (QSE_T("%d\n"), i);
i++;
qse_sleep (&sleep_interval);
2018-01-26 10:03:58 +00:00
}
return i;
}
static int test1 (void)
{
qse_thr_t* thr;
thr = qse_thr_open (QSE_MMGR_GETDFL(), QSE_SIZEOF(thr_xtn_t));
if (!thr)
{
qse_printf (QSE_T("cannot open thread\n"));
return -1;
}
qse_thr_setstacksize (thr, 64000);
2018-01-27 05:58:20 +00:00
if (qse_thr_start(thr, thr_func, QSE_NULL, QSE_THR_SIGNALS_BLOCKED) <= -1)
2018-01-26 10:03:58 +00:00
{
qse_printf (QSE_T("cannot start thread\n"));
qse_thr_close (thr);
return -1;
}
while (!g_stopreq)
{
if (qse_thr_getstate(thr) == QSE_THR_TERMINATED) break;
qse_sleep (&sleep_interval);
2018-01-26 10:03:58 +00:00
}
if (g_stopreq)
{
/*qse_thr_stop (thr);*/
thr_xtn_t* xtn = qse_thr_getxtn(thr);
xtn->stopreq = 1;
}
qse_thr_join (thr);
qse_printf (QSE_T("thread ended with retcode %d\n"), qse_thr_getretcode(thr));
qse_thr_close (thr);
return 0;
}
static void handle_sigint (int sig, siginfo_t* siginfo, void* ctx)
{
g_stopreq = 1;
}
static void set_signal (int sig, void(*handler)(int, siginfo_t*, void*))
{
struct sigaction sa;
memset (&sa, 0, sizeof(sa));
/*sa.sa_handler = handler;*/
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigemptyset (&sa.sa_mask);
sigaction (sig, &sa, NULL);
}
static void set_signal_to_default (int sig)
{
struct sigaction sa;
memset (&sa, 0, sizeof(sa));
sa.sa_handler = SIG_DFL;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
sigaction (sig, &sa, NULL);
}
int main ()
{
#if defined(_WIN32)
char locale[100];
UINT codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8)
{
/*SetConsoleOutputCP (CP_UTF8);*/
2018-01-26 10:03:58 +00:00
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
}
else
{
qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage);
2018-01-26 10:03:58 +00:00
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
}
#else
setlocale (LC_ALL, "");
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
#endif
set_signal (SIGINT, handle_sigint);
qse_open_stdsios ();
test1();
qse_close_stdsios ();
set_signal_to_default (SIGINT);
return 0;
}