{"id":4508,"date":"2014-03-30T12:52:44","date_gmt":"2014-03-30T12:52:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/portable-way-to-give-thread-name-with-qt-collection-of-common-programming-errors\/"},"modified":"2014-03-30T12:52:44","modified_gmt":"2014-03-30T12:52:44","slug":"portable-way-to-give-thread-name-with-qt-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/portable-way-to-give-thread-name-with-qt-collection-of-common-programming-errors\/","title":{"rendered":"Portable way to give thread name with Qt?-Collection of common programming errors"},"content":{"rendered":"<p>There&#8217;s nothing in the <code>QThread<\/code> API to manually manage the system name of the thread, however, since version 4.8.3, Qt will automatically set the name of your thread to the name of the thread object (<code>QObject::objectName()<\/code>).<\/p>\n<p>This is handled in the implementations of <code>QThread<\/code> as described below.<\/p>\n<p>You have something like this in <code>qthread_unix.cpp<\/code>:<\/p>\n<pre><code>#if (defined(Q_OS_LINUX) || defined(Q_OS_MAC) || defined(Q_OS_QNX))\nstatic void setCurrentThreadName(pthread_t threadId, const char *name)\n{\n#  if defined(Q_OS_LINUX) &amp;&amp; !defined(QT_LINUXBASE)\n    Q_UNUSED(threadId);\n    prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);\n#  elif defined(Q_OS_MAC)\n    Q_UNUSED(threadId);\n    pthread_setname_np(name);\n#  elif defined(Q_OS_QNX)\n    pthread_setname_np(threadId, name);\n#  endif\n}\n#endif\n\n\/* \n * [...]\n *\/\n\nQString objectName = thr-&gt;objectName();\n\nif (Q_LIKELY(objectName.isEmpty()))\n    setCurrentThreadName(thr-&gt;d_func()-&gt;thread_id, thr-&gt;metaObject()-&gt;className());\nelse\n    setCurrentThreadName(thr-&gt;d_func()-&gt;thread_id, objectName.toLocal8Bit());\n<\/code><\/pre>\n<p>And the equivalent in <code>qthread_win.cpp<\/code>:<\/p>\n<pre><code>typedef struct tagTHREADNAME_INFO\n{\n    DWORD dwType;      \/\/ must be 0x1000\n    LPCSTR szName;     \/\/ pointer to name (in user addr space)\n    HANDLE dwThreadID; \/\/ thread ID (-1=caller thread)\n    DWORD dwFlags;     \/\/ reserved for future use, must be zero\n} THREADNAME_INFO;\n\nvoid qt_set_thread_name(HANDLE threadId, LPCSTR threadName)\n{\n    THREADNAME_INFO info;\n    info.dwType = 0x1000;\n    info.szName = threadName;\n    info.dwThreadID = threadId;\n    info.dwFlags = 0;\n\n    __try\n    {\n        RaiseException(0x406D1388, 0, sizeof(info)\/sizeof(DWORD), (const ULONG_PTR*)&amp;info);\n    }\n    __except (EXCEPTION_CONTINUE_EXECUTION)\n    {\n    }\n}\n\n\/* \n * [...]\n *\/\n\nQByteArray objectName = thr-&gt;objectName().toLocal8Bit();\nqt_set_thread_name((HANDLE)-1, objectName.isEmpty() ? thr-&gt;metaObject()-&gt;className() : objectName.constData());\n<\/code><\/pre>\n<p>Note that on Windows, the above code won&#8217;t be executed if <code>QT_NO_DEBUG<\/code> is set, thus <strong>it won&#8217;t work in <em>Release<\/em> mode<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s nothing in the QThread API to manually manage the system name of the thread, however, since version 4.8.3, Qt will automatically set the name of your thread to the name of the thread object (QObject::objectName()). This is handled in the implementations of QThread as described below. You have something like this in qthread_unix.cpp: #if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4508","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4508"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4508\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}