diff --git a/doc/APIchanges b/doc/APIchanges
index 4f6ac2a031f0e32dac02bf50f5a876c63a8c2a1b..8d305d5867facc59b5d2f3bf76215c5d961bb40d 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,9 @@ libavutil:     2017-10-21
 
 API changes, most recent first:
 
+2018-04-xx - xxxxxxxxxx - lavu 56.16.100 - threadmessage.h
+  Add av_thread_message_queue_nb_elems().
+
 -------- 8< --------- FFmpeg 4.0 was cut here -------- 8< ---------
 
 2018-04-03 - d6fc031caf - lavu 56.13.100 - pixdesc.h
diff --git a/libavutil/threadmessage.c b/libavutil/threadmessage.c
index 872e9392b19f2dbed7d0e5053b758c25f0c7b1ef..764b7fb8131e493dcf4d2bd0630cf428f087fd56 100644
--- a/libavutil/threadmessage.c
+++ b/libavutil/threadmessage.c
@@ -102,6 +102,19 @@ void av_thread_message_queue_free(AVThreadMessageQueue **mq)
 #endif
 }
 
+int av_thread_message_queue_nb_elems(AVThreadMessageQueue *mq)
+{
+#if HAVE_THREADS
+    int ret;
+    pthread_mutex_lock(&mq->lock);
+    ret = av_fifo_size(mq->fifo);
+    pthread_mutex_unlock(&mq->lock);
+    return ret / mq->elsize;
+#else
+    return AVERROR(ENOSYS);
+#endif
+}
+
 #if HAVE_THREADS
 
 static int av_thread_message_queue_send_locked(AVThreadMessageQueue *mq,
diff --git a/libavutil/threadmessage.h b/libavutil/threadmessage.h
index 8480a0a3dbd337864d669071eac097cae46db684..42ce655f365ac4527b0b4bf223b82db902efdc41 100644
--- a/libavutil/threadmessage.h
+++ b/libavutil/threadmessage.h
@@ -95,6 +95,14 @@ void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq,
 void av_thread_message_queue_set_free_func(AVThreadMessageQueue *mq,
                                            void (*free_func)(void *msg));
 
+/**
+ * Return the current number of messages in the queue.
+ *
+ * @return the current number of messages or AVERROR(ENOSYS) if lavu was built
+ *         without thread support
+ */
+int av_thread_message_queue_nb_elems(AVThreadMessageQueue *mq);
+
 /**
  * Flush the message queue
  *
diff --git a/libavutil/version.h b/libavutil/version.h
index 387421775f696589a122e8dd84ed933bc8dbd852..23567000a333d4c5ef2bfba30e7accc8c41b0407 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  56
-#define LIBAVUTIL_VERSION_MINOR  15
+#define LIBAVUTIL_VERSION_MINOR  16
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
diff --git a/tests/api/api-threadmessage-test.c b/tests/api/api-threadmessage-test.c
index 05a8062b8c15a450ee8a356fab7c135bdecd03c5..3c693a70d1e418e0948ef73393225386249855d4 100644
--- a/tests/api/api-threadmessage-test.c
+++ b/tests/api/api-threadmessage-test.c
@@ -130,7 +130,9 @@ static void *receiver_thread(void *arg)
 
     for (i = 0; i < rd->workload; i++) {
         if (rand() % rd->workload < rd->workload / 10) {
-            av_log(NULL, AV_LOG_INFO, "receiver #%d: flushing the queue\n", rd->id);
+            av_log(NULL, AV_LOG_INFO, "receiver #%d: flushing the queue, "
+                   "discarding %d message(s)\n", rd->id,
+                   av_thread_message_queue_nb_elems(rd->queue));
             av_thread_message_flush(rd->queue);
         } else {
             struct message msg;