added QSE::ThreadR and QSE::ThreadC
This commit is contained in:
@ -39,20 +39,41 @@ public:
|
||||
int stopreq;
|
||||
};
|
||||
|
||||
class Functor
|
||||
{
|
||||
public:
|
||||
int operator() (QSE::Thread* thr)
|
||||
{
|
||||
int i = 0;
|
||||
int* stopreqptr = (int*)thr->getContext();
|
||||
|
||||
while (!*stopreqptr)
|
||||
{
|
||||
qse_mtx_lock (g_prmtx, QSE_NULL);
|
||||
qse_printf (QSE_T("fc %p -> %d\n"), this, i);
|
||||
qse_mtx_unlock (g_prmtx);
|
||||
i++;
|
||||
sleep (1);
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
};
|
||||
|
||||
static int test1 (void)
|
||||
{
|
||||
MyThread thr1;
|
||||
QSE::Thread thr2;
|
||||
QSE::Thread thr3;
|
||||
int localstopreq = 0;
|
||||
QSE::ThreadR thr2;
|
||||
|
||||
int localstopreq = 0;
|
||||
g_prmtx = qse_mtx_open (QSE_MMGR_GETDFL(), 0);
|
||||
|
||||
thr1.setStackSize (64000);
|
||||
thr2.setStackSize (64000);
|
||||
thr3.setStackSize (64000);
|
||||
|
||||
|
||||
auto lambda = [](QSE::Thread* thr) {
|
||||
auto lambda = [](QSE::Thread* thr)->int
|
||||
{
|
||||
int i = 0;
|
||||
int* stopreqptr = (int*)thr->getContext();
|
||||
|
||||
@ -68,7 +89,8 @@ static int test1 (void)
|
||||
return i;
|
||||
};
|
||||
|
||||
auto lambda_with_capture = [&localstopreq](QSE::Thread* thr) {
|
||||
auto lambda_with_capture = [&localstopreq](QSE::Thread* thr)->int
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while (!localstopreq)
|
||||
@ -98,34 +120,62 @@ static int test1 (void)
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
if (thr3.startl(lambda_with_capture, QSE::Thread::SIGNALS_BLOCKED) <= -1)
|
||||
//QSE::LambdaThread thr3;
|
||||
QSE::ThreadC<decltype(lambda)> thr3 (lambda);
|
||||
thr3.setStackSize (64000);
|
||||
thr3.setContext (&localstopreq);
|
||||
if (thr3.start(QSE::Thread::SIGNALS_BLOCKED) <= -1)
|
||||
{
|
||||
qse_printf (QSE_T("cannot start thread3\n"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// turn a lambda with capture to a thread
|
||||
QSE::ThreadC<decltype(lambda_with_capture)> thr4 (lambda_with_capture);
|
||||
thr4.setStackSize (64000);
|
||||
if (thr4.start(QSE::Thread::SIGNALS_BLOCKED) <= -1)
|
||||
{
|
||||
qse_printf (QSE_T("cannot start thread4\n"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// turn a functor to a thread
|
||||
QSE::ThreadC<Functor> thr5;
|
||||
thr5.setStackSize (64000);
|
||||
thr5.setContext (&localstopreq);
|
||||
if (thr5.start(QSE::Thread::SIGNALS_BLOCKED) <= -1)
|
||||
{
|
||||
qse_printf (QSE_T("cannot start thread4\n"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (!g_stopreq)
|
||||
{
|
||||
if (thr1.getState() == QSE::Thread::TERMINATED &&
|
||||
thr2.getState() == QSE::Thread::TERMINATED &&
|
||||
thr3.getState() == QSE::Thread::TERMINATED) break;
|
||||
thr3.getState() == QSE::Thread::TERMINATED &&
|
||||
thr4.getState() == QSE::Thread::TERMINATED &&
|
||||
thr5.getState() == QSE::Thread::TERMINATED) break;
|
||||
sleep (1);
|
||||
}
|
||||
|
||||
if (g_stopreq)
|
||||
{
|
||||
thr1.stopreq = 1;
|
||||
localstopreq = 1;
|
||||
thr1.stopreq = 1;
|
||||
}
|
||||
|
||||
thr1.join ();
|
||||
thr2.join ();
|
||||
thr3.join ();
|
||||
thr4.join ();
|
||||
thr5.join ();
|
||||
|
||||
qse_printf (QSE_T("thread1 ended with retcode %d\n"), thr1.getReturnCode());
|
||||
qse_printf (QSE_T("thread2 ended with retcode %d\n"), thr2.getReturnCode());
|
||||
qse_printf (QSE_T("thread3 ended with retcode %d\n"), thr3.getReturnCode());
|
||||
qse_printf (QSE_T("thread4 ended with retcode %d\n"), thr4.getReturnCode());
|
||||
qse_printf (QSE_T("thread5 ended with retcode %d\n"), thr5.getReturnCode());
|
||||
|
||||
qse_mtx_close (g_prmtx);
|
||||
return 0;
|
||||
|
Reference in New Issue
Block a user