Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is there a #define for Multi-Threading?

Is there a #define for Multi-Threading?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
8 Posts 5 Posters 1 Views 1 Watching
  • 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.
  • X Offline
    X Offline
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    wrote on last edited by
    #1

    I know the compiler options for a multi-threaded program in VC++. Is there also a preprocessor #define that indicates multi-threading? So that I can do e.g. something like

    #ifdef _MULTI_THREAD

    define doSomething(x) doSomethingMulti(x)

    #else

    define doSomething(x) doSomethingSingle(x)

    #endif

    B B B A 4 Replies Last reply
    0
    • X XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      I know the compiler options for a multi-threaded program in VC++. Is there also a preprocessor #define that indicates multi-threading? So that I can do e.g. something like

      #ifdef _MULTI_THREAD

      define doSomething(x) doSomethingMulti(x)

      #else

      define doSomething(x) doSomethingSingle(x)

      #endif

      B Offline
      B Offline
      berndg
      wrote on last edited by
      #2

      Since it is your process that actually creates those threads, wouldn't you be the best person to know whether or how many threads are in use? It seems a bit hard expecting Microsoft to predict your source code. What am I missing?

      X 1 Reply Last reply
      0
      • B berndg

        Since it is your process that actually creates those threads, wouldn't you be the best person to know whether or how many threads are in use? It seems a bit hard expecting Microsoft to predict your source code. What am I missing?

        X Offline
        X Offline
        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        wrote on last edited by
        #3

        berndg wrote: Since it is your process that actually creates those threads, wouldn't you be the best person to know whether or how many threads are in use? It seems a bit hard expecting Microsoft to predict your source code. I want to compile the whole program (my own code + MS code) either in 'single-threaded' or 'multi-threaded' mode. Depending on the threading-mode I want to call different functions. This should be done with macros (what else). To make it work I need a #define similar to

        #define _POSIX_THREADS

        Yes, I can #define my own macro, but that's not the point. What am I missing? ;)

        B 1 Reply Last reply
        0
        • X XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

          berndg wrote: Since it is your process that actually creates those threads, wouldn't you be the best person to know whether or how many threads are in use? It seems a bit hard expecting Microsoft to predict your source code. I want to compile the whole program (my own code + MS code) either in 'single-threaded' or 'multi-threaded' mode. Depending on the threading-mode I want to call different functions. This should be done with macros (what else). To make it work I need a #define similar to

          #define _POSIX_THREADS

          Yes, I can #define my own macro, but that's not the point. What am I missing? ;)

          B Offline
          B Offline
          berndg
          wrote on last edited by
          #4

          I see. Duno.

          1 Reply Last reply
          0
          • X XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

            I know the compiler options for a multi-threaded program in VC++. Is there also a preprocessor #define that indicates multi-threading? So that I can do e.g. something like

            #ifdef _MULTI_THREAD

            define doSomething(x) doSomethingMulti(x)

            #else

            define doSomething(x) doSomethingSingle(x)

            #endif

            B Offline
            B Offline
            Blake Miller
            wrote on last edited by
            #5

            I know ... :cool: From MSDN: C/C++ Preprocessor Reference : Predefined Macros See Also Macros | Preprocessor Operators | Preprocessor Directives The compiler recognizes 10 predefined ANSI C macros, and the Microsoft C++ implementation provides several more. These macros take no arguments and cannot be redefined. Their value, except for __LINE__ and __FILE__, must be constant throughout compilation. Some of the predefined macros listed below are defined with multiple values. _MT Defined when /MD or /MDd (Multithreaded DLL) or /MT or /MTd (Multithreaded) is specified.

            X 1 Reply Last reply
            0
            • B Blake Miller

              I know ... :cool: From MSDN: C/C++ Preprocessor Reference : Predefined Macros See Also Macros | Preprocessor Operators | Preprocessor Directives The compiler recognizes 10 predefined ANSI C macros, and the Microsoft C++ implementation provides several more. These macros take no arguments and cannot be redefined. Their value, except for __LINE__ and __FILE__, must be constant throughout compilation. Some of the predefined macros listed below are defined with multiple values. _MT Defined when /MD or /MDd (Multithreaded DLL) or /MT or /MTd (Multithreaded) is specified.

              X Offline
              X Offline
              XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
              wrote on last edited by
              #6

              Blake Miller wrote: _MT Defined when /MD or /MDd (Multithreaded DLL) or /MT or /MTd (Multithreaded) is specified. Bingo! Thank you very much! :)

              1 Reply Last reply
              0
              • X XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

                I know the compiler options for a multi-threaded program in VC++. Is there also a preprocessor #define that indicates multi-threading? So that I can do e.g. something like

                #ifdef _MULTI_THREAD

                define doSomething(x) doSomethingMulti(x)

                #else

                define doSomething(x) doSomethingSingle(x)

                #endif

                B Offline
                B Offline
                Bob Stanneveld
                wrote on last edited by
                #7

                If you use this kind of macro magic just to implement your synchronization, this is bad! It is better to define a helper class that implements your lock, and define a macro that creates the lock or does nothing depending on the _MT switch. Example:

                class CSemaphoreLock
                {
                public:
                CSemaphoreLock(HANDLE hSem) : m_hSem(hSem) { ::WaitForSingleObject(hSem, INFINITE); }
                ~CSemaphoreLock() { ::ReleaseSemaphore(m_hSem, 1, NULL); }

                private:
                const HANDLE m_hSem;
                };

                #ifdef _MT

                define SEMAPHORE_LOCK(HSEM) CSemaphoreLock TempLock_(HSEM)

                #else

                define SEMAPHORE_LOCK(HSEM)

                #endif // _MT

                Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                1 Reply Last reply
                0
                • X XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

                  I know the compiler options for a multi-threaded program in VC++. Is there also a preprocessor #define that indicates multi-threading? So that I can do e.g. something like

                  #ifdef _MULTI_THREAD

                  define doSomething(x) doSomethingMulti(x)

                  #else

                  define doSomething(x) doSomethingSingle(x)

                  #endif

                  A Offline
                  A Offline
                  Alexander M
                  wrote on last edited by
                  #8

                  Yes there is: /MT Defines _MT so that multithread-specific versions of the run-time routines are selected from the standard header (.h) files. This option also causes the compiler to place the library name LIBCMT.lib into the .obj file so that the linker will use LIBCMT.lib to resolve external symbols. Either /MT or /MD (or their debug equivalents /MTd or /MDd) is required to create multithreaded programs. /MTd Defines _DEBUG and _MT. Defining _MT causes multithread-specific versions of the run-time routines to be selected from the standard .h files. This option also causes the compiler to place the library name LIBCMTD.lib into the .obj file so that the linker will use LIBCMTD.lib to resolve external symbols. Either /MTd or /MDd (or their non-debug equivalents /MT or MD) is required to create multithreaded programs. Don't try it, just do it! ;-)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups