]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Working thread test!!!
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>
Thu, 21 Feb 2008 17:27:55 +0000 (17:27 +0000)
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>
Thu, 21 Feb 2008 17:27:55 +0000 (17:27 +0000)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@8983 e03df62e-2008-0410-955e-edbf42e46eb7

include/threadengine.h
src/testsuite.cpp
src/threadengines/threadengine_pthread.cpp

index 56ccce4239f44a0fbbddef9c3fac4a89199c69e1..397b0085e46ba02604ce6cbfd043ca533c6d8ce0 100644 (file)
@@ -82,6 +82,8 @@ class CoreExport ThreadEngine : public Extensible
  */
 class CoreExport Thread : public Extensible
 {
+ private:
+       bool ExitFlag;
  public:
 
        /** Creator thread engine
@@ -90,7 +92,7 @@ class CoreExport Thread : public Extensible
 
        /** Set Creator to NULL at this point
         */
-       Thread() : Creator(NULL)
+       Thread() : ExitFlag(false), Creator(NULL)
        {
        }
 
@@ -107,6 +109,21 @@ class CoreExport Thread : public Extensible
         * threaded code here
         */
        virtual void Run() = 0;
+
+       void SetExitFlag()
+       {
+               ExitFlag = true;
+       }
+
+       void ClearExitFlag()
+       {
+               ExitFlag = false;
+       }
+
+       bool GetExitFlag()
+       {
+               return ExitFlag;
+       }
 };
 
 
index 60817970333b0d0cd85b85bf7d5e5d1873c90953..719da031319c21455ea8ff360cd4a316c21b0bd2 100644 (file)
@@ -33,10 +33,10 @@ class TestSuiteThread : public Thread
 
        virtual void Run()
        {
-               while (1)
+               while (GetExitFlag() == false)
                {
                        cout << "Test suite thread run...\n";
-                       sleep(10);
+                       sleep(5);
                }
        }
 };
@@ -110,19 +110,23 @@ bool TestSuite::DoThreadTests()
        }
        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;
 }
index da120bfab79bbba6eec1cb15d945d33892224ab1..8bdcd6d96cad3e3183848c6ca042cd85f829c406 100644 (file)
@@ -36,6 +36,7 @@ void PThreadEngine::Create(Thread* thread_to_init)
 {
        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)
@@ -44,6 +45,8 @@ void PThreadEngine::Create(Thread* thread_to_init)
                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);
@@ -81,6 +84,10 @@ void PThreadEngine::FreeThread(Thread* thread)
        pthread_t* pthread = NULL;
        if (thread->GetExt("pthread", pthread))
        {
+               thread->SetExitFlag();
+               int rc;
+               void* status;
+               rc = pthread_join(*pthread, &status);
                delete pthread;
        }
 }