*/
class CoreExport Thread : public Extensible
{
+ private:
+ bool ExitFlag;
public:
/** Creator thread engine
/** Set Creator to NULL at this point
*/
- Thread() : Creator(NULL)
+ Thread() : ExitFlag(false), Creator(NULL)
{
}
* threaded code here
*/
virtual void Run() = 0;
+
+ void SetExitFlag()
+ {
+ ExitFlag = true;
+ }
+
+ void ClearExitFlag()
+ {
+ ExitFlag = false;
+ }
+
+ bool GetExitFlag()
+ {
+ return ExitFlag;
+ }
};
virtual void Run()
{
- while (1)
+ while (GetExitFlag() == false)
{
cout << "Test suite thread run...\n";
- sleep(10);
+ sleep(5);
}
}
};
}
cout << "Creation success!\n";
- cout << "Creating new thread of type TestSuiteThread\n";
-
+ cout << "Creating new thread of type TestSuiteThread...\n";
TestSuiteThread* tst = new TestSuiteThread();
+ cout << "Create new thread based on TestSuiteThread...\n";
te->Create(tst);
- cout << "Press enter to end test.";
- cin >> anything;
+ cout << "Press any key to end test.\n";
+ getchar();
- /* Auto frees thread */
+ /* Thread engine auto frees thread on delete */
+ cout << "Waiting for thread to exit...";
delete tst;
+ cout << "Done!\n";
+ cout << "Delete ThreadEngine... ";
delete te;
+ cout << "Done!\n";
return true;
}
{
pthread_attr_t attribs;
pthread_attr_init(&attribs);
+ pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE);
pthread_t* MyPThread = new pthread_t;
if (pthread_create(MyPThread, &attribs, PThreadEngine::Entry, (void*)this) != 0)
throw CoreException("Unable to create new PThreadEngine: " + std::string(strerror(errno)));
}
+ pthread_attr_destroy(&attribs);
+
NewThread = thread_to_init;
NewThread->Creator = this;
NewThread->Extend("pthread", MyPThread);
pthread_t* pthread = NULL;
if (thread->GetExt("pthread", pthread))
{
+ thread->SetExitFlag();
+ int rc;
+ void* status;
+ rc = pthread_join(*pthread, &status);
delete pthread;
}
}