Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using QThreadStorage in any thread
Forum Update on Monday, May 27th 2025

Using QThreadStorage in any thread

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 999 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chopper
    wrote on 6 May 2019, 23:27 last edited by
    #1

    I presume that it is safe to use on any operating system thread as Qt likely relies on the underlying platform support for TLS. (I.e., it doesn't have to be used on threads spawned via QThread.)

    Is that correct?

    Thanks!

    J 1 Reply Last reply 7 May 2019, 04:25
    0
    • C chopper
      6 May 2019, 23:27

      I presume that it is safe to use on any operating system thread as Qt likely relies on the underlying platform support for TLS. (I.e., it doesn't have to be used on threads spawned via QThread.)

      Is that correct?

      Thanks!

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 7 May 2019, 04:25 last edited by
      #2

      @chopper Yes, as long as the operating system supports threads you can use them via QThread.
      What do you mean by TLS in this context?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • C Offline
        C Offline
        chopper
        wrote on 7 May 2019, 13:01 last edited by
        #3

        Thread Local Storage (as opposed to Transport Layer Security ;) )

        Thx!

        This lets me create a QNetworkAccessManager allocator that returns an instance per thread and store that in a map of <threadID,QNAM*>, and because of QThreadStorage I can mark the thread and avoid the situation where a new thread reuses an old thread ID (Qt::HANDLE) and would otherwise get back a QNAM built for a different thread,

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chopper
          wrote on 7 May 2019, 13:09 last edited by
          #4

          I thought I would double check, it turns out that's not correct @jsulm :

          From qthreadstorage.cpp

          void **QThreadStorageData::set(void *p)
          {
              QThreadData *data = QThreadData::current();
              if (!data) {
                  qWarning("QThreadStorage::set: QThreadStorage can only be used with threads started with QThread");
                  return 0;
              }
          
          1 Reply Last reply
          0
          • C Offline
            C Offline
            chopper
            wrote on 7 May 2019, 14:22 last edited by
            #5

            I'm trying, and testing, the following (requires C++ 11)

            thread_local std::unique_ptr<QNetworkAccessManager> tls_pQNetworkAccessManager;
            
            QNetworkAccessManager *getQNetworkAccessManager( void )
            {
            	if( nullptr == tls_pQNetworkAccessManager )
            	{
            		tls_pQNetworkAccessManager.reset( new QNetworkAccessManager() );
            	}
            
            	return tls_pQNetworkAccessManager.get();
            }
            
            J 1 Reply Last reply 8 May 2019, 04:17
            0
            • C chopper
              7 May 2019, 14:22

              I'm trying, and testing, the following (requires C++ 11)

              thread_local std::unique_ptr<QNetworkAccessManager> tls_pQNetworkAccessManager;
              
              QNetworkAccessManager *getQNetworkAccessManager( void )
              {
              	if( nullptr == tls_pQNetworkAccessManager )
              	{
              		tls_pQNetworkAccessManager.reset( new QNetworkAccessManager() );
              	}
              
              	return tls_pQNetworkAccessManager.get();
              }
              
              J Online
              J Online
              jsulm
              Lifetime Qt Champion
              wrote on 8 May 2019, 04:17 last edited by
              #6

              @chopper Do you really need one QNetworkAccessManager instance per thread?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • C Offline
                C Offline
                chopper
                wrote on 8 May 2019, 15:24 last edited by
                #7

                Yes, this is utility code that could be used by any number of threads, and sharing a QNAM between threads requires forcing requests onto another thread - which is a potentially huge performance issue.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  Grove
                  wrote on 13 Sept 2019, 17:00 last edited by Grove
                  #8

                  @chopper said in Using QThreadStorage in any thread:

                  std

                  How did you implemented the TLS?

                  C 1 Reply Last reply 13 Sept 2019, 17:05
                  0
                  • G Grove
                    13 Sept 2019, 17:00

                    @chopper said in Using QThreadStorage in any thread:

                    std

                    How did you implemented the TLS?

                    C Offline
                    C Offline
                    chopper
                    wrote on 13 Sept 2019, 17:05 last edited by
                    #9

                    @Grove
                    Via C++ 11 support:

                    thread_local std::unique_ptr<QNetworkAccessManager> tls_pQNetworkAccessManager;
                    
                    QNetworkAccessManager *getQNetworkAccessManager( void )
                    {
                    	if( nullptr == tls_pQNetworkAccessManager )
                    	{
                    		tls_pQNetworkAccessManager.reset( new QNetworkAccessManager() );
                    	}
                    
                    	return tls_pQNetworkAccessManager.get();
                    }
                    
                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved