mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-19 08:03:14 +08:00
Added multi-threaded example
This commit is contained in:
parent
f5f6edcf5a
commit
f7a705c40a
@ -20,6 +20,7 @@ all:
|
||||
$(CC) hello.c ../mongoose.c -o hello $(CFLAGS)
|
||||
$(CC) qcomm.c ../mongoose.c -o qcomm $(CFLAGS)
|
||||
$(CC) post.c ../mongoose.c -o post $(CFLAGS)
|
||||
$(CC) multi_threaded.c ../mongoose.c -o multi_threaded $(CFLAGS)
|
||||
|
||||
# $(CC) upload.c ../mongoose.c -o upload $(CFLAGS)
|
||||
# $(CC) -DUSE_WEBSOCKET websocket.c ../mongoose.c -o $@ $(CFLAGS)
|
||||
@ -36,6 +37,7 @@ LFLAGS = /link /incremental:no /libpath:$(MSVC)/lib /machine:IX86
|
||||
windows:
|
||||
$(CL) hello.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
|
||||
$(CL) post.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
|
||||
$(CL) multi_threaded.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
|
||||
# $(CL) upload.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
|
||||
# $(CL) /DUSE_WEBSOCKET websocket.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
|
||||
#$(CL) lua_dll.c $(CLFLAGS) $(DLL_FLAGS) /DLL $(LFLAGS) /SUBSYSTEM:WINDOWS /ENTRY:luaopen_lua_dll /EXPORT:luaopen_lua_dll /out:lua_dll.dll
|
||||
|
34
examples/multi_threaded.c
Normal file
34
examples/multi_threaded.c
Normal file
@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include "mongoose.h"
|
||||
|
||||
static int request_handler(struct mg_connection *conn) {
|
||||
mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n"
|
||||
"This is a reply from server instance # %s",
|
||||
(char *) conn->server_param);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void *serve(void *server) {
|
||||
for (;;) mg_poll_server((struct mg_server *) server, 1000);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_server *server1, *server2;
|
||||
|
||||
server1 = mg_create_server("1");
|
||||
server2 = mg_create_server("2");
|
||||
|
||||
mg_add_uri_handler(server1, "/", request_handler);
|
||||
mg_add_uri_handler(server2, "/", request_handler);
|
||||
|
||||
// Make both server1 and server2 listen on the same socket
|
||||
mg_set_option(server1, "listening_port", "8080");
|
||||
mg_set_listening_socket(server2, mg_get_listening_socket(server1));
|
||||
|
||||
// server1 goes to separate thread, server 2 runs in main thread
|
||||
mg_start_thread(serve, server1);
|
||||
serve(server2);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user