Skip to content
Snippets Groups Projects
Commit 1b838fb1 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

workerthread.cc: cleanup

parent 978ecfec
No related branches found
No related tags found
1 merge request!345M214003/develop
......@@ -97,44 +97,47 @@ WorkerThread::~WorkerThread()
// g++ -g -Wall -O2 -DTEST_WORKERTHREAD workerthread.cc
void
myfunc(void *arg)
func(int &intArg)
{
intArg = -1;
printf("run myfunc\n");
return nullptr;
}
void
mytask1(void)
worker1(void)
{
WorkerThread task;
WorkerThread workerThread;
void *myarg = nullptr;
int ivalue = 0;
std::function<void()> my_task = std::bind(func, std::ref(ivalue));
task.start(myfunc, myarg);
task.wait();
workerThread.doAsync(my_task);
workerThread.wait();
printf("worker1: %d\n", ivalue);
}
void
mytask2(void)
worker2(void)
{
bool useTask = true;
auto task = useTask ? std::make_unique<WorkerThread>() : nullptr;
bool useWorkerThread = true;
auto workerThread = useWorkerThread ? std::make_unique<WorkerThread>() : nullptr;
int ivalue = 0;
std::function<void()> my_task = std::bind(func, std::ref(ivalue));
void *myarg = nullptr;
useWorkerThread ? workerThread->doAsync(my_task) : my_task();
if (useTask)
task->start(myfunc, myarg);
else
myfunc(myarg);
if (useWorkerThread) workerThread->wait();
if (useTask) task->wait();
printf("worker2: %d\n", ivalue);
}
int
main(int argc, char **argv)
{
mytask1();
mytask2();
worker1();
worker2();
return 0;
}
......
......@@ -26,9 +26,6 @@ private:
std::function<void()> function;
void *(*routine)(void *) = nullptr;
void *arg = nullptr;
State state{ State::SETUP };
std::thread thread;
std::mutex workMutex;
......@@ -40,6 +37,7 @@ private:
public:
WorkerThread();
~WorkerThread();
void doAsync(const std::function<void()> &_function);
void wait();
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment