added the Condition class

This commit is contained in:
2018-07-16 04:18:02 +00:00
parent b510d11c55
commit a346d27d5f
8 changed files with 286 additions and 7 deletions

View File

@ -65,17 +65,19 @@ if ENABLE_CXX
CXXLIB = -lqsesixx -lqsecmnxx
bin_PROGRAMS += sck01 spl02 tcpsvr01 thr02
bin_PROGRAMS += sck01 spl02 tcpsvr01 thr02 thr03
sck01_SOURCES = sck01.cpp
spl02_SOURCES = spl02.cpp
tcpsvr01_SOURCES = tcpsvr01.cpp
thr02_SOURCES = thr02.cpp
thr03_SOURCES = thr03.cpp
sck01_LDADD = $(CXXLIB) $(LDADD)
spl02_LDADD = $(CXXLIB) $(LDADD)
tcpsvr01_LDADD = $(CXXLIB) $(LDADD)
thr02_LDADD = $(CXXLIB) $(LDADD)
thr03_LDADD = $(CXXLIB) $(LDADD)
endif

View File

@ -95,7 +95,7 @@ bin_PROGRAMS = aio01$(EXEEXT) dir01$(EXEEXT) fio01$(EXEEXT) \
sio01$(EXEEXT) sio02$(EXEEXT) sio03$(EXEEXT) spl01$(EXEEXT) \
task01$(EXEEXT) thr01$(EXEEXT) $(am__EXEEXT_1)
@WCHAR_TRUE@@WIN32_TRUE@am__append_1 = $(UNICOWS_LIBS)
@ENABLE_CXX_TRUE@am__append_2 = sck01 spl02 tcpsvr01 thr02
@ENABLE_CXX_TRUE@am__append_2 = sck01 spl02 tcpsvr01 thr02 thr03
subdir = samples/si
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/m4/ax_check_sign.m4 \
@ -113,7 +113,8 @@ CONFIG_HEADER = $(top_builddir)/include/qse/config.h
CONFIG_CLEAN_FILES =
CONFIG_CLEAN_VPATH_FILES =
@ENABLE_CXX_TRUE@am__EXEEXT_1 = sck01$(EXEEXT) spl02$(EXEEXT) \
@ENABLE_CXX_TRUE@ tcpsvr01$(EXEEXT) thr02$(EXEEXT)
@ENABLE_CXX_TRUE@ tcpsvr01$(EXEEXT) thr02$(EXEEXT) \
@ENABLE_CXX_TRUE@ thr03$(EXEEXT)
am__installdirs = "$(DESTDIR)$(bindir)"
PROGRAMS = $(bin_PROGRAMS)
am_aio01_OBJECTS = aio01.$(OBJEXT)
@ -223,6 +224,11 @@ am__thr02_SOURCES_DIST = thr02.cpp
thr02_OBJECTS = $(am_thr02_OBJECTS)
@ENABLE_CXX_TRUE@thr02_DEPENDENCIES = $(am__DEPENDENCIES_1) \
@ENABLE_CXX_TRUE@ $(am__DEPENDENCIES_3)
am__thr03_SOURCES_DIST = thr03.cpp
@ENABLE_CXX_TRUE@am_thr03_OBJECTS = thr03.$(OBJEXT)
thr03_OBJECTS = $(am_thr03_OBJECTS)
@ENABLE_CXX_TRUE@thr03_DEPENDENCIES = $(am__DEPENDENCIES_1) \
@ENABLE_CXX_TRUE@ $(am__DEPENDENCIES_3)
AM_V_P = $(am__v_P_@AM_V@)
am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
am__v_P_0 = false
@ -282,7 +288,8 @@ SOURCES = $(aio01_SOURCES) $(dir01_SOURCES) $(fio01_SOURCES) \
$(pio01_SOURCES) $(rwl01_SOURCES) $(sck01_SOURCES) \
$(sio01_SOURCES) $(sio02_SOURCES) $(sio03_SOURCES) \
$(spl01_SOURCES) $(spl02_SOURCES) $(task01_SOURCES) \
$(tcpsvr01_SOURCES) $(thr01_SOURCES) $(thr02_SOURCES)
$(tcpsvr01_SOURCES) $(thr01_SOURCES) $(thr02_SOURCES) \
$(thr03_SOURCES)
DIST_SOURCES = $(aio01_SOURCES) $(dir01_SOURCES) $(fio01_SOURCES) \
$(fio02_SOURCES) $(fs01_SOURCES) $(fs02_SOURCES) \
$(fs03_SOURCES) $(glob01_SOURCES) $(log01_SOURCES) \
@ -291,7 +298,7 @@ DIST_SOURCES = $(aio01_SOURCES) $(dir01_SOURCES) $(fio01_SOURCES) \
$(sio01_SOURCES) $(sio02_SOURCES) $(sio03_SOURCES) \
$(spl01_SOURCES) $(am__spl02_SOURCES_DIST) $(task01_SOURCES) \
$(am__tcpsvr01_SOURCES_DIST) $(thr01_SOURCES) \
$(am__thr02_SOURCES_DIST)
$(am__thr02_SOURCES_DIST) $(am__thr03_SOURCES_DIST)
am__can_run_installinfo = \
case $$AM_UPDATE_INFO_DIR in \
n|no|NO) false;; \
@ -517,10 +524,12 @@ thr01_SOURCES = thr01.c
@ENABLE_CXX_TRUE@spl02_SOURCES = spl02.cpp
@ENABLE_CXX_TRUE@tcpsvr01_SOURCES = tcpsvr01.cpp
@ENABLE_CXX_TRUE@thr02_SOURCES = thr02.cpp
@ENABLE_CXX_TRUE@thr03_SOURCES = thr03.cpp
@ENABLE_CXX_TRUE@sck01_LDADD = $(CXXLIB) $(LDADD)
@ENABLE_CXX_TRUE@spl02_LDADD = $(CXXLIB) $(LDADD)
@ENABLE_CXX_TRUE@tcpsvr01_LDADD = $(CXXLIB) $(LDADD)
@ENABLE_CXX_TRUE@thr02_LDADD = $(CXXLIB) $(LDADD)
@ENABLE_CXX_TRUE@thr03_LDADD = $(CXXLIB) $(LDADD)
all: all-am
.SUFFIXES:
@ -700,6 +709,10 @@ thr02$(EXEEXT): $(thr02_OBJECTS) $(thr02_DEPENDENCIES) $(EXTRA_thr02_DEPENDENCIE
@rm -f thr02$(EXEEXT)
$(AM_V_CXXLD)$(CXXLINK) $(thr02_OBJECTS) $(thr02_LDADD) $(LIBS)
thr03$(EXEEXT): $(thr03_OBJECTS) $(thr03_DEPENDENCIES) $(EXTRA_thr03_DEPENDENCIES)
@rm -f thr03$(EXEEXT)
$(AM_V_CXXLD)$(CXXLINK) $(thr03_OBJECTS) $(thr03_LDADD) $(LIBS)
mostlyclean-compile:
-rm -f *.$(OBJEXT)
@ -730,6 +743,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tcpsvr01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/thr01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/thr02.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/thr03.Po@am__quote@
.c.o:
@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<

186
qse/samples/si/thr03.cpp Normal file
View File

@ -0,0 +1,186 @@
#include <qse/si/Thread.hpp>
#include <qse/si/Condition.hpp>
#include <qse/si/mtx.h>
#include <qse/si/sio.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/HeapMmgr.hpp>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#include <unistd.h>
#include <signal.h>
#include <string.h>
static int g_stopreq = 0;
static qse_mtx_t* g_prmtx = QSE_NULL;
QSE::HeapMmgr g_heap_mmgr (QSE::Mmgr::getDFL(), 30000);
struct rq_data_t
{
rq_data_t(): stop(0) {}
int stop;
QSE::Mutex mtx;
QSE::Condition cnd;
};
class Waiter
{
public:
int operator() (QSE::Thread* thr)
{
int i = 0;
rq_data_t* rqdata = (rq_data_t*)thr->getContext();
while (1)
{
rqdata->mtx.lock ();
if (rqdata->stop)
{
rqdata->mtx.unlock ();
break;
}
rqdata->cnd.wait(rqdata->mtx);
rqdata->mtx.unlock ();
qse_mtx_lock (g_prmtx, QSE_NULL);
qse_printf (QSE_T("[%p] -> loop %d\n"), this, i);
qse_mtx_unlock (g_prmtx);
i++;
}
qse_mtx_lock (g_prmtx, QSE_NULL);
qse_printf (QSE_T("[%p] -> exiting\n"), this);
qse_mtx_unlock (g_prmtx);
return i;
}
};
static int test1 (void)
{
g_prmtx = qse_mtx_open (QSE_MMGR_GETDFL(), 0);
rq_data_t rqdata;
QSE::ThreadF<Waiter> thr[3];
for (int i = 0; i < 3; i++)
{
thr[i].setStackSize (64000);
thr[i].setContext (&rqdata);
if (thr[i].start(QSE::Thread::SIGNALS_BLOCKED) <= -1)
{
qse_printf (QSE_T("cannot start thread%d\n"), i);
return -1;
}
}
while (1)
{
if (g_stopreq)
{
qse_mtx_lock (g_prmtx, QSE_NULL);
qse_printf (QSE_T("broadcasting stop ---> 1\n"));
qse_mtx_unlock (g_prmtx);
rqdata.mtx.lock ();
rqdata.stop = 1;
rqdata.mtx.unlock ();
rqdata.cnd.broadcast ();
}
int nterm = 0;
for (int i = 0; i < 3; i++)
{
if (thr[i].getState() == QSE::Thread::TERMINATED) nterm++;
}
if (nterm == 3) break;
qse_mtx_lock (g_prmtx, QSE_NULL);
qse_printf (QSE_T("signalling ....(nterm = %d)\n"), nterm);
qse_mtx_unlock (g_prmtx);
rqdata.cnd.signal ();
sleep (1);
}
for (int i = 0; i < 3; i++)
{
thr[i].join();
}
for (int i = 0; i < 3; i++)
{
qse_printf (QSE_T("thread%d ended with retcode %d\n"), i, thr[i].getReturnCode());
}
qse_mtx_close (g_prmtx);
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);*/
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
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;
}