/* * BlinkingLight.cpp * * This class will manage the blinking of an LED on a given port. * It will blink at a given rate, though that rate can be adjusted up or * down as is necessary. * * Created on: Mar 7, 2015 * Author: student */ #include "ChildExampleThread.h" #include #include #include #include #include #include using namespace std; /** * This is the constructor for the class. * @param port This is the port onto which the LED that is to be blinked is connected. */ ChildExampleThread::ChildExampleThread(const char * name) { this->name = name; threadRunning = true; count = 0; this->thread=0; } void ChildExampleThread::printResult() { cout << "The result for thread " <thread << ") is " << this->count << ".\n"; } /** * This method will free any and all allocated resources from the class. */ ChildExampleThread::~ChildExampleThread() { } /** * This method will stop the given thread. */ void ChildExampleThread::stopThread() { threadRunning = false; } /** * This is the run method for the class. In essence, it contains the actions that should be taken by a thread within this class. * In this particular class, it will turn the light on, sleep for a period, turn the light off, and then sleep again. */ void ChildExampleThread::run() { unsigned int loop; while (threadRunning==true) { count += 1; for (loop = 0; loop < count; loop++) { // DO nothing. } //printf("%d %ld\n", this->thread, this->count); usleep(100); } } /** * This method is a friend method for the given class. It will start the given thread running. * @param instance */ void* runBlinkingLight(void* instance){ ChildExampleThread *myinstance = static_cast(instance); myinstance->run(); return NULL; } /** * This method will spawn a new thread and cause the thread to start executing. */ void ChildExampleThread::start(pthread_attr_t* threadAttributes) { printf("Starting new thread.\n"); pthread_create(&thread, threadAttributes, &runBlinkingLight, this); } /** * This method will return the given thread that this class is executing on top of. * @return The return will be the executing thread. */ pthread_t ChildExampleThread::getThread() { return thread; }