Threads.cpp

Example on how to employ "ProcessThread" threads.

// Local
#include "config.h"
// STDC++
#include <iostream>
#include <fstream>
#include <vector>
// POSIX C
// C++ Libraries
#include <ui-utilcpp/Time.hpp>
#include <ui-utilcpp/Thread.hpp>
template <typename T>
class MyThread: public T
{
private:
int run()
{
for (int i(0); i < 5; ++i)
{
std::cout << "(PID " << T::getPID() << ": blurp)..." << std::flush;
UI::Util::nssleep(1);
}
return(42);
}
};
void test(UI::Util::ProcessThread * t)
{
std::cout << "Starting thread: " << std::flush;
t->start();
int exitCode(t->wait());
std::cout << "finished with status: " << exitCode << std::endl;
}
int main()
{
try
{
MyThread<UI::Util::ForkThread> ft;
test(&ft);
}
catch(...)
{
std::cerr << "Exception" << std::endl;
}
}
Thread.hpp
Thread, descriptors and sockets.
Time.hpp
UI::Util::ProcessThread
Simple encapsulation for any process based threading system.
Definition Thread.hpp:40
UI::Util::ProcessThread::wait
int wait()
Wait for this "thread", and return the process' exit status.
Definition Thread.cpp:109
UI::Util::ProcessThread::start
virtual void start()=0
Start fork "thread".
UI::Util::nssleep
unsigned int nssleep(unsigned int seconds)
No Signal Seconds Sleep: Using POSIX nanosleep(2).
Definition Time.cpp:47