Thread: Thread safety in WinAPI

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Thread safety in WinAPI

    Assuming 2 parallel threads (duh!) is it thread-safe for them to send messages to the same window without a mutex lock, like for changing the title and size on the window at the same time?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Yes, but be careful of deadlocks.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I got some random troubles but using SendMessageTimeout instead of SendMessage seemed to solve it.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    This will probably sound like a stupid question, but in MSVC I still compile as Runtime Library - Single Threaded. Should I change to multi-threaded? Isn't winapi threadsafe without it?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The WinAPI is independent of the runtime library, but you should use the multi-threaded runtime library if you are using C or C++ library functions. Several of the C/C++ functions use static data (rand, localtime, etc), which would create havoc if you used the single thread runtime library in a multi-threaded environment.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    And in MSVC 2005 .NET the single-threaded libraries are no longer an option.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinAPI & threading
    By @nthony in forum Windows Programming
    Replies: 17
    Last Post: 10-15-2007, 04:41 PM
  2. Replies: 2
    Last Post: 07-01-2007, 07:11 AM
  3. Messaging Between Threads, Exiting Thread On Demand, Etc.
    By HyperShadow in forum C++ Programming
    Replies: 10
    Last Post: 06-09-2007, 01:04 PM
  4. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  5. WinAPI (useless thread)
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 06-11-2003, 02:34 AM