[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] Question about threads pool and TLS

From: Christian Grothoff
Subject: Re: [libmicrohttpd] Question about threads pool and TLS
Date: Tue, 14 Jul 2020 15:29:00 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0

Hi Iron Bug,

You may want to look at
for inspiration.

There, we use a
   * Thread-local database connection.
   * Contains a pointer to `struct GNUNET_PQ_Context` or NULL.
  pthread_key_t db_conn_threadlocal;

which is turned into a per-thread DB connection here:

  if (pthread_equal (pc->main_self,
                     pthread_self ()))
    session = pc->main_session;
    session = pthread_getspecific (pc->db_conn_threadlocal);
  if (NULL != session)
    GNUNET_PQ_reconnect_if_down (session->conn);
    return session;

and here:

 if (pthread_equal (pc->main_self,
                     pthread_self ()))
    pc->main_session = session;
    if (0 != pthread_setspecific (pc->db_conn_threadlocal,
      GNUNET_break (0);
      GNUNET_PQ_disconnect (db_conn);
      GNUNET_free (session);
      return NULL;


 if (0 != pthread_key_create (&pg->db_conn_threadlocal,
    TALER_LOG_ERROR ("Cannot create pthread key.\n");
    GNUNET_free (pg->sql_dir);
    GNUNET_free (pg);
    return NULL;

we clean up the thread-specific connection in db_conn_destroy:

static void
db_conn_destroy (void *cls)
  struct TALER_EXCHANGEDB_Session *session = cls;
  struct GNUNET_PQ_Context *db_conn;

  if (NULL == session)
  db_conn = session->conn;
  session->conn = NULL;
  if (NULL != db_conn)
    GNUNET_PQ_disconnect (db_conn);
  GNUNET_free (session);

So by my understanding, this should give you all the pieces you need to
achieve what you asked for.

Happy hacking!


On 7/12/20 9:11 PM, Iron Bug at wrote:
> Greetings, everybody.
> I write a little network utility using ibmicrohttpd on Linux and I use MHD 
> threads pool for better performance.
> My utility uses DB connections and ideally I'd like to have one connection 
> instance per thread to avoid unnecessary locks.
> I can use pthreads means (like pthread_key_create) for local thread storage, 
> but how can I determine when the thread is created and joined? Is there any 
> events or callbacks at every worker thread's start and end where I could hook 
> on to open and close my DB connections?
> Thanks in advance.
> Yana A. Kireyonok aka Iron Bug

Attachment: 0x939E6BE1E29FC3CC.asc
Description: application/pgp-keys

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]