Domanda

So di poter impostare il nome del thread (quello visibile in GDB e Htop) in Linux usando prctl(). Ma con un altro sistema operativo molto probabilmente non funzionerà. Inoltre, potrei provare usando pthread_setname_np(), che è un po 'più disponibile tra i sistemi Posix, ma manca ancora della piena compatibilità.

Quindi mi piacerebbe avere un modo più portatile, forse qualcosa QThread Fornisce che non ho trovato. C'è un modo del genere?

È stato utile?

Soluzione

Non c'è niente nel QThread API per gestire manualmente il nome del sistema del thread, tuttavia, poiché la versione 4.8.3, QT imposterà automaticamente il nome del thread sul nome dell'oggetto thread (QObject::objectName()).

Questo è gestito nelle implementazioni di QThread come descritto sotto.

Hai qualcosa di simile qthread_unix.cpp:

#if (defined(Q_OS_LINUX) || defined(Q_OS_MAC) || defined(Q_OS_QNX))
static void setCurrentThreadName(pthread_t threadId, const char *name)
{
#  if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE)
    Q_UNUSED(threadId);
    prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);
#  elif defined(Q_OS_MAC)
    Q_UNUSED(threadId);
    pthread_setname_np(name);
#  elif defined(Q_OS_QNX)
    pthread_setname_np(threadId, name);
#  endif
}
#endif

/* 
 * [...]
 */

QString objectName = thr->objectName();

if (Q_LIKELY(objectName.isEmpty()))
    setCurrentThreadName(thr->d_func()->thread_id, thr->metaObject()->className());
else
    setCurrentThreadName(thr->d_func()->thread_id, objectName.toLocal8Bit());

E l'equivalente in qthread_win.cpp:

typedef struct tagTHREADNAME_INFO
{
    DWORD dwType;      // must be 0x1000
    LPCSTR szName;     // pointer to name (in user addr space)
    HANDLE dwThreadID; // thread ID (-1=caller thread)
    DWORD dwFlags;     // reserved for future use, must be zero
} THREADNAME_INFO;

void qt_set_thread_name(HANDLE threadId, LPCSTR threadName)
{
    THREADNAME_INFO info;
    info.dwType = 0x1000;
    info.szName = threadName;
    info.dwThreadID = threadId;
    info.dwFlags = 0;

    __try
    {
        RaiseException(0x406D1388, 0, sizeof(info)/sizeof(DWORD), (const ULONG_PTR*)&info);
    }
    __except (EXCEPTION_CONTINUE_EXECUTION)
    {
    }
}

/* 
 * [...]
 */

QByteArray objectName = thr->objectName().toLocal8Bit();
qt_set_thread_name((HANDLE)-1, objectName.isEmpty() ? thr->metaObject()->className() : objectName.constData());

Si noti che su Windows, il codice sopra non verrà eseguito se QT_NO_DEBUG è impostato, quindi Non funzionerà Pubblicazione modalità.

Altri suggerimenti

In Documentazione QT potete trovare:

Per scegliere il nome che verrà fornito il thread (come identificato dal comando ps -l su Linux, ad esempio), è possibile Chiama setObjectName () prima di avviare il thread. Se non si chiama setObjectName (), il nome dato al tuo thread sarà il nome della classe del tipo di runtime del tuo oggetto thread (ad esempio, "renderthread" nel caso dell'esempio Mandelbrot, in quanto questo è il nome della sottoclasse QThread). Si noti che questo non è attualmente disponibile con build di rilascio su Windows.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top