_beginthreadex does not complile

This is a discussion on _beginthreadex does not complile within the Windows Programming forums, part of the Platform Specific Boards category; I've searched the forum but was unable to find a normal guidance on how to make the _beginthreadex compile. "Look ...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    _beginthreadex does not complile

    I've searched the forum but was unable to find a normal guidance on how to make the _beginthreadex compile.
    "Look in the options->linker" is not really useful, sorry.
    error C3861: '_beginthreadex': identifier not found, even with argument-dependent lookup
    Can someone please guide me on how to make the function work?
    Also, what is exactly the CRT ?


    Thank you.
    Last edited by Devil Panther; 12-09-2006 at 12:32 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you should link with Multithreaded version of the run-time library (static or dynamic)
    project settings/C++/code Generation/ run time library

    PS. CRT - C run-time ?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Yes, CRT is C run time lib, can you please explain what is it all about?

    Also, I've change the setting to use MT, but now I get different errors:
    error LNK2001: unresolved external symbol __malloc_dbg

    error LNK2019: unresolved external symbol __free_dbg referenced in function "void __cdecl operator delete(void *,struct std::_DebugHeapTag_t const &,char *,int)" (??3@YAXPAXABU_DebugHeapTag_t@std@@PADH@Z)

    error LNK2019: unresolved external symbol __malloc_dbg referenced in function "void * __cdecl operator new(unsigned int,struct std::_DebugHeapTag_t const &,char *,int)" (??2@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z)

    fatal error LNK1120: 2 unresolved externals
    Here is a sample of my code:
    Code:
    unsigned __stdcall myThread(void *params)
    {
       DWORD dwSemCount=0;
       HANDLE hSemaphore;
    
       hSemaphore = OpenSemaphore(SYNCHRONIZE | SEMAPHORE_MODIFY_STATE, 0, THREAD_SEMAPHORE);
       WaitForSingleObject(hSemaphore, INFINITE);
    
    //  Do Something Here
    
       ReleaseSemaphore(hSemaphore, 1, (long *)&dwSemCount);
       CloseHandle(hSemaphore);
       ExitThread(1);
       return(1);
    }
    
    int main()
    {
       unsigned dwChildId;
    
       _beginthreadex(NULL, 0, &myThread, NULL, 0, &dwChildId);
       return(0);
    }
    Last edited by Devil Panther; 12-09-2006 at 12:53 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Well, I managed to solve the errors, selected MTd, instead of MT... Yet I still don't understand why.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  5. #5
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262
    In what way?

    From what I can perceive, the C runtime library is not thread-safe by default, so you won't find any sort of threading functions implemented in it.

    The multithreaded CRT is thread-safe, so _beginthreadex is in there, but because you're compiling a debug build, the memory debug functions are nowhere to be found.

    MTd refers to... tada... the multithreaded debug CRT!

  6. #6
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by SMurf
    In what way?

    From what I can perceive, the C runtime library is not thread-safe by default, so you won't find any sort of threading functions implemented in it.

    The multithreaded CRT is thread-safe, so _beginthreadex is in there, but because you're compiling a debug build, the memory debug functions are nowhere to be found.

    MTd refers to... tada... the multithreaded debug CRT!
    I'm sorry, I don't really understand what is the CRT anyway, can you please explain to me what is it all about, and why do I need it?

    As for the debug MT, well the regular MT returns linking errors, as you can see above.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  7. #7
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262
    The CRT is the compiler's version of the standard C library. printf(), fopen()... these functions all have to have been written at some point by the people who made the compiler. It also sets up the variables necessary to operate the functions when your program is first started, before handing control to your main(), and tidies up when it returns.

    This gets linked into your program after compilation. If you compiled for example:-
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        return 0;
    }
    You'll find that the completed program isn't about six bytes in size but around 8KB instead. Much of this space is taken up by the CRT. Some of the code it includes you may never use, but is there for performance reasons.

    So... back to the "necessary variables", then. The default CRT always uses the same addresses to keep variables. But if you are using multiple threads, they may be going through different stages of the CRT, so in a simple way...

    Thread 1 calls CRT function which uses static memory
    Thread 2 also calls CRT function which uses the same static memory
    Thread 1 tries to retrieve the static memory... BOOM

    The multithreaded CRT gets around this by allocating memory for each thread. Slight performance hit relative to the standard one, but these days it's moot.

    When making a debug build, things are EVEN MORE COMPLICATED!!!
    (In that extra checks are made that you're not overwriting memory that isn't allocated). So you get two extra copies of the CRT including the _malloc_dbg and _free_dbg in single and multithreaded versions!

    Phew

  8. #8
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    So, I shouldn't use the MT debug ?
    Does it really matter?

    Sorry for the stupid questions
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  9. #9
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262
    You should while making a debug build, just in case something bad happens with the memory you allocate (it will tell you what the problem is).

    When you've finished debugging and are happy with it, change to the multithreaded release CRT (MT).

  10. #10
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by SMurf
    You should while making a debug build, just in case something bad happens with the memory you allocate (it will tell you what the problem is).

    When you've finished debugging and are happy with it, change to the multithreaded release CRT (MT).
    Well that's just it, when I set the project to use MT, I receive Linking errors:
    error LNK2001: unresolved external symbol __malloc_dbg

    error LNK2019: unresolved external symbol __free_dbg referenced in function "void __cdecl operator delete(void *,struct std::_DebugHeapTag_t const &,char *,int)" (??3@YAXPAXABU_DebugHeapTag_t@std@@PADH@Z)

    error LNK2019: unresolved external symbol __malloc_dbg referenced in function "void * __cdecl operator new(unsigned int,struct std::_DebugHeapTag_t const &,char *,int)" (??2@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z)

    fatal error LNK1120: 2 unresolved externals
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    If you have compiled with _DEBUG you should link with MTD
    if you want to link with MT - compile using RELEASE build
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by vart
    If you have compiled with _DEBUG you should link with MTD
    if you want to link with MT - compile using RELEASE build
    Can you please instruct me where do I set the RELEASE build compilation?

    tnx.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Don't you have 2 preconfigured builds in your IDE for release and debug?

    If not - check your preprocessor
    remove something like _DEBUG from preprocessor directives

    and I don't remember - it is possible you need to add NDEBUG instead
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I'm sorry for the ignorance, but I'm not really sure what are you talking about.
    My IDE is VS 2003, maybe you can direct me to where I need to do all this?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    When you create project in VS2003 you already have 2 build "release" and "debug"

    When you enter Project Properties in the "Configuration" combo you will see Active(Debug) for example

    You can choose then the build you want and change settings for this build

    If you choose Release - select Multithreaded RunTime
    If you choose Debug - select MultiThreaded Debug RunTime

    Then only select the suitable build (combo on the standard toolbar) when you compile and link the exe
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you complile a .pc file?
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-29-2002, 02:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21