Thread: Multithread security problem

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    42

    Multithread security problem

    I maintian a MFC program which invokes some class written in plain C++ code. These plain c++ class contain multithread member functions and the MFC program invokes those member functions. I've read some articles that say in MFC program you should use AfxBeginThread instead of CreateThread and _beginthreadex, if not to do the replacement, there'll cause some mulitithread security problems.

    I'm curious that will it cause security problems in my MFC program? If will, how can I resolve them? Should I use MFC mulitithread function in these plain c++ code?

    PS. In those plain c++ code, they use CreateThread to run thread functions now and using /MD switch. I consider I may use _beginthreadex to replace those CreateThread()s for safty. Am I right?

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Read the last paragraph of the 'remarks' section in the description for CreateThread for the circumstances under which it's safe to use that api function, although I suspect they probably mean __beginthreadex when referring to _beginthread.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    There are two issues here:

    First, you should use the MFC function AfxBeginThread() if the thread being created uses any MFC api's or objects. MFC maintains thread specific data needed to make the MFC framework work. Technically, it's not always bad to not use AfxBeginThread(), but understanding when it isn't bad requires understanding how the framework works...

    The second issue is the one Ken is refering to. Understanding why this leak can occur is alot easier than understanding the MFC framework. Scroll down to the bottom of this thread for a nice summary of the situation.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM