Hi Iron Bug, You may want to look at https://git.taler.net/exchange.git/src/exchangedb/plugin_exchangedb_postgres.c 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; else 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; } else { if (0 != pthread_setspecific (pc->db_conn_threadlocal, session)) { GNUNET_break (0); GNUNET_PQ_disconnect (db_conn); GNUNET_free (session); return NULL; } } Using if (0 != pthread_key_create (&pg->db_conn_threadlocal, &db_conn_destroy)) { 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) return; 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! Christian On 7/12/20 9:11 PM, Iron Bug at gmail.com 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 >
0x939E6BE1E29FC3CC.asc
Description: application/pgp-keys
signature.asc
Description: OpenPGP digital signature