Skip to content

Commit

Permalink
Request a reasonably small thread stack
Browse files Browse the repository at this point in the history
This somewhat mitigates the problem that now the buffer is allocated in
addition to the already allocated thread stack.
  • Loading branch information
mixi committed Apr 3, 2021
1 parent a165922 commit fcfcff9
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion fiche.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Use netcat to push text - example:
#include <stdlib.h>
#include <string.h>

#include <errno.h>
#include <pwd.h>
#include <time.h>
#include <unistd.h>
Expand Down Expand Up @@ -520,12 +521,18 @@ static void dispatch_connection(int socket, Fiche_Settings *settings) {

// Spawn a new thread to handle this connection
pthread_t id;
pthread_attr_t attr;

if ( pthread_create(&id, NULL, &handle_connection, c) != 0 ) {
if ( (errno = pthread_attr_init(&attr)) ||
(errno = pthread_attr_setstacksize(&attr, 16*1024)) ||
(errno = pthread_create(&id, &attr, &handle_connection, c)) ) {
pthread_attr_destroy(&attr);
print_error("Couldn't spawn a thread!");
return;
}

pthread_attr_destroy(&attr);

// Detach thread if created succesfully
// TODO: consider using pthread_tryjoin_np
pthread_detach(id);
Expand Down

0 comments on commit fcfcff9

Please sign in to comment.