Created
January 12, 2019 13:59
-
-
Save azat/b709865d0ad17ed79e39259aa731304a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <event2/event.h> | |
#include <event2/http.h> | |
#include <event2/thread.h> | |
#include <thread> | |
#include <atomic> | |
using namespace std; | |
/* | |
* | |
*/ | |
class ServerThread { | |
public: | |
struct event_base* base; | |
struct evhttp *http; | |
std::thread *t; | |
std::atomic<bool> isRunning{false}; | |
void Start() { | |
isRunning = true; | |
t = new std::thread([&]() { | |
event_base_dispatch(base); | |
cout << "Loop exited" << endl; | |
isRunning = false; | |
}); | |
}; | |
void Stop() { | |
int counter = 0; | |
event_base_loopexit(base, nullptr); | |
event_base_loopbreak(base); | |
cout << "Trying to exit " << counter << " time" << endl; | |
event_base_free(base); | |
evhttp_free(http); | |
}; | |
ServerThread() { | |
evthread_use_pthreads(); | |
base = event_base_new(); | |
http = evhttp_new(base); | |
evhttp_bind_socket_with_handle(http, "0.0.0.0", 8080); | |
}; | |
virtual ~ServerThread() { | |
Stop(); | |
t->join(); | |
delete t; | |
} | |
}; | |
int main(int argc, char** argv) { | |
ServerThread server; | |
server.Start(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment