Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CppClass.cpp
- #include <iostream>
- #include <pthread.h>
- static pthread_t thread;
- static pthread_mutex_t mutex;
- static pthread_cond_t cond;
- static int shutdown;
- using namespace std;
- class CppClass
- {
- public:
- static void Start()
- {
- cout << "Testing start function." << endl;
- shutdown = 0;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
- pthread_mutex_init(&mutex, NULL);
- pthread_cond_init(&cond, NULL);
- pthread_create(&thread, &attr, run_thread, NULL);
- }
- static void Stop()
- {
- pthread_mutex_lock(&mutex);
- shutdown = 1;
- pthread_cond_broadcast(&cond);
- pthread_mutex_unlock(&mutex);
- }
- static void Join()
- {
- pthread_join(thread, NULL);
- }
- private:
- static void *run_thread(void *pthread_args)
- {
- CppClass *obj = new CppClass();
- pthread_mutex_lock(&mutex);
- while (shutdown == 0)
- {
- struct timespec ts;
- ts.tv_sec = time(NULL) + 3;
- pthread_cond_timedwait(&cond, &mutex, &ts);
- if (shutdown)
- {
- break;
- }
- obj->display();
- }
- pthread_mutex_unlock(&mutex);
- pthread_mutex_destroy(&mutex);
- pthread_cond_destroy(&cond);
- pthread_exit(NULL);
- return NULL;
- }
- void display()
- {
- cout << " Inside display() " << endl;
- }
- };
- // main.cpp
- #include <iostream>
- /*
- * If I remove the comment below and delete the
- * the class declaration part, it works.
- */
- // #include "CppClass.cpp"
- using namespace std;
- class CppClass
- {
- public:
- static void Start();
- static void Stop();
- static void Join();
- };
- int main()
- {
- CppClass::Start();
- while (1)
- {
- int quit;
- cout << "Do you want to end?: (0 = stay, 1 = quit) ";
- cin >> quit;
- cout << "Input: " << quit << endl;
- if (quit)
- {
- CppClass::Stop();
- cout << "Joining CppClass..." << endl;
- CppClass::Join();
- break;
- }
- }
- }
- class NewClass {
- public:
- NewClass();
- int methodA();
- int methodB();
- }; <- don't forget the semicolon
- class NewClass {
- public:
- NewClass();
- int methodA();
- int methodB();
- }; <- don't forget the semicolon
- #include "NewClass.h"
- NewClass::NewClass() {
- // constructor goes here
- }
- int NewClass::methodA() {
- // methodA goes here
- return 0;
- }
- int NewClass::methodB() {
- // methodB goes here
- return 1;
- }
- #include "NewClass.h"
- int main() {
- NewClass nc;
- // do something with nc
- }
- // CppClass.h
- using namespace std;
- class CppClass
- {
- public:
- static void Start();
- static void Stop();
- static void Join();
- private:
- void *run_thread(void *pthread_args);
- void display();
- };
- // CppClass.cpp
- #include <iostream>
- #include <pthread.h>
- #include "CppClass.h"
- using namespace std;
- void CppClass::Start()
- {
- /* method body goes here */
- }
- void CppClass::Stop()
- {
- /* method body goes here */
- }
- void CppClass::Join()
- {
- /* method body goes here */
- }
- void *CppClass::run_thread(void *pthread_args)
- {
- /* method body goes here */
- }
- void CppClass::display() {
- /* method body goes here */
- }
- // main.cpp
- #include "CppClass.h"
- int main()
- {
- /* main method body here */
- }
Add Comment
Please, Sign In to add comment