Skip to content

Instantly share code, notes, and snippets.

@bgunton
Created January 21, 2013 23:18
Show Gist options
  • Save bgunton/4590476 to your computer and use it in GitHub Desktop.
Save bgunton/4590476 to your computer and use it in GitHub Desktop.
# Compile with "g++ -pthread -std=gnu++0x test.cpp"
#include <thread>
#include <iostream>
#include <vector>
class Socketor
{
public:
std::vector<std::thread> threads;
static void processMessages(Socketor* s) {
std::string msg = "Hello";
while(true) {
s->processMessage(msg);
}
}
virtual void processMessage(std::string) = 0;
Socketor() {}
~Socketor() { for(auto &i : threads) i.join(); }
};
class Logger : public Socketor
{
public:
void processMessage(std::string msg) {
std::cout << "Message: " << msg << std::endl;
}
void start() {
threads.push_back(std::thread(processMessages, this));
}
};
int main(int argc, const char** argv)
{
Logger l;
l.start();
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment