Is there a #define for Multi-Threading?
-
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
-
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
-
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?
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? ;) -
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? ;) -
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
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. -
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.Blake Miller wrote: _MT Defined when /MD or /MDd (Multithreaded DLL) or /MT or /MTd (Multithreaded) is specified. Bingo! Thank you very much! :)
-
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
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[^]
-
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
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! ;-)