Add HelloWorldThreaded and (actually) fix race
This commit is contained in:
@@ -6,6 +6,12 @@ examples:
|
|||||||
clang++ -flto -O3 -s *.o -o HelloWorld
|
clang++ -flto -O3 -s *.o -o HelloWorld
|
||||||
rm *.o
|
rm *.o
|
||||||
|
|
||||||
|
# HelloWorldThreaded (non-SSL, non-Zlib compile)
|
||||||
|
clang -DLIBUS_NO_SSL -flto -O3 -c -IuSockets/src uSockets/src/*.c uSockets/src/eventing/*.c
|
||||||
|
clang++ -DLIBUS_NO_SSL -DUWS_NO_ZLIB -flto -O3 -c -std=c++17 -Isrc -IuSockets/src examples/HelloWorldThreaded.cpp
|
||||||
|
clang++ -lpthread -flto -O3 -s *.o -o HelloWorldThreaded
|
||||||
|
rm *.o
|
||||||
|
|
||||||
# EchoServer (non-SSL, non-Zlib compile)
|
# EchoServer (non-SSL, non-Zlib compile)
|
||||||
clang -DLIBUS_NO_SSL -flto -O3 -c -IuSockets/src uSockets/src/*.c uSockets/src/eventing/*.c
|
clang -DLIBUS_NO_SSL -flto -O3 -c -IuSockets/src uSockets/src/*.c uSockets/src/eventing/*.c
|
||||||
clang++ -DLIBUS_NO_SSL -DUWS_NO_ZLIB -flto -O3 -c -std=c++17 -Isrc -IuSockets/src examples/EchoServer.cpp
|
clang++ -DLIBUS_NO_SSL -DUWS_NO_ZLIB -flto -O3 -c -std=c++17 -Isrc -IuSockets/src examples/EchoServer.cpp
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#include "App.h"
|
||||||
|
#include <thread>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
/* Overly simple hello world app, using multiple threads */
|
||||||
|
std::vector<std::thread *> threads(std::thread::hardware_concurrency());
|
||||||
|
|
||||||
|
std::transform(threads.begin(), threads.end(), threads.begin(), [](std::thread *t) {
|
||||||
|
return new std::thread([]() {
|
||||||
|
|
||||||
|
uWS::App().get("/*", [](auto *res, auto *req) {
|
||||||
|
res->end("Hello world!");
|
||||||
|
}).listen(3000, [](auto *token) {
|
||||||
|
if (token) {
|
||||||
|
std::cout << "Thread " << std::this_thread::get_id() << " listening on port " << 3000 << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << "Thread " << std::this_thread::get_id() << " failed to listen on port 3000" << std::endl;
|
||||||
|
}
|
||||||
|
}).run();
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
std::for_each(threads.begin(), threads.end(), [](std::thread *t) {
|
||||||
|
t->join();
|
||||||
|
});
|
||||||
|
}
|
||||||
+6
-2
@@ -24,7 +24,7 @@
|
|||||||
#include <libusockets_new.h>
|
#include <libusockets_new.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <atomic>
|
#include <thread>
|
||||||
|
|
||||||
namespace uWS {
|
namespace uWS {
|
||||||
struct Loop {
|
struct Loop {
|
||||||
@@ -86,9 +86,13 @@ private:
|
|||||||
public:
|
public:
|
||||||
/* Returns the default loop if called from one thread, or a dedicated per-thread loop if called from multiple threads */
|
/* Returns the default loop if called from one thread, or a dedicated per-thread loop if called from multiple threads */
|
||||||
static Loop *defaultLoop() {
|
static Loop *defaultLoop() {
|
||||||
|
/* Lock this whole function */
|
||||||
|
static std::mutex m;
|
||||||
|
std::lock_guard<std::mutex> lock(m);
|
||||||
|
|
||||||
/* Deliver and attach the default loop to the first thread who calls us */
|
/* Deliver and attach the default loop to the first thread who calls us */
|
||||||
static thread_local bool ownsDefaultLoop;
|
static thread_local bool ownsDefaultLoop;
|
||||||
static std::atomic<Loop *> defaultLoop;
|
static Loop *defaultLoop;
|
||||||
if (!defaultLoop) {
|
if (!defaultLoop) {
|
||||||
ownsDefaultLoop = true;
|
ownsDefaultLoop = true;
|
||||||
defaultLoop = create(true);
|
defaultLoop = create(true);
|
||||||
|
|||||||
Reference in New Issue
Block a user