mfc >> [Q] Thread safety and messages

by JonB » Wed, 26 Nov 2003 21:37:18 GMT

Hiya

I have an application which creates a worker thread to
handle incoming network data. The thread is created
using the Win32 function CreateThread(). When the worker
thread a received a piece of data, it sends a message to
the main application so it can be processed:

void Blah::Callback() {
theApp.m_pMainWnd->PostMessage(ID_DATA_READY);
}

The main window had a handler for this message which
retrieves and processes the data.

While this has been working fine in my tests, I haven't
been able to confirm that this method is guaranteed to
be thread safe, ie. avoids the situation where the
message is being posted just as the main window starts to
look at its queue.

Anyone know if this technique is safe?

Cheers,



mfc >> [Q] Thread safety and messages

by David A. Mair » Thu, 27 Nov 2003 01:09:07 GMT


If you look at the implementation of CWnd::PostMessage() (or whichever base
class contains PostMessage) then you will probably find that it has some
validation then little more than:

::PostMessage(GetSafeHwnd(), msg, wparam, lparam);

If it truly does call the SDK PostMessage then there is almost no difference
between this and any other application posting messages to this window.
That is, it is the responsibility of Windows to make PostMessage thread
safe, not you. OTOH, I wonder if GetSafeHwnd() is thread safe, I suspect
that it is for the main window.

Note that this is speculation since I didn;t have time to open the MFC
source to confirm my guesses but you can always do that for yourself.








mfc >> [Q] Thread safety and messages

by Scott McPhillips [MVP] » Thu, 27 Nov 2003 10:08:18 GMT






It is guaranteed to be thread safe.

--
Scott McPhillips [VC++ MVP]



Similar Threads

1. hashtable - thread safety clarification

I read the following in the help for VS C# Express edition:

"Hashtable is thread safe for use by multiple reader threads and a single 
writing thread."

if I am using a hashtable just like described above, multiple threads read 
from in but only one thread changes it, should I still be declaring it as 
'volatile'?


2. Async Execution and Thread-Safety - CSharp/C#

3. Thread safety help on Hashtable

I'm sort of unclear on what is thread safe and what isn't when using
Hashtable. The way I understand the documentation is that multiple
threads can simultaneously be reading an instance of Hashtable while
at the same time a single thread is writing to it. Is this correct?
The second part of my question is what exactly is considered a "write"
if I've added a reference object as the value? Is it just calls to
Hashtable methods like .Add and .Remove as well as setting the key to
a new value? Can I safely modify or reassign the reference object in
the Hashtable value? So for instance is this code thread safe?

class Server
{
    public IPEndPoint serverEndPoint;
}

Hashtable ht = new Hashtable();
Server server = new Server();

server.serverEndPoint = new IPEndPoint(123456, 8000);

ht.Add(1, server);

server.serverEndPoint.Port = 8001;


What about?


class Server
{
    public IPEndPoint serverEndPoint;
}

Hashtable ht = new Hashtable();
Server server = new Server();

server.serverEndPoint = new IPEndPoint(123456, 8000);

ht.Add(1, server);

server = new Server();
server.serverEndPoint = new IPEndPoint(123456, 8001);


Thanks for any help

4. Thread safety and member variables - CSharp/C#

5. Thread safety for initializers and linked list assignments?

I have a class that has no ancestors and only static data members of type 
string and ListDictionary. I am not allowed to use a static constructor.



Here is the code:

 class sql{

  private static string LogSource=abc();



  // Hold a list of databases and their configuration values
  private static ListDictionary DBList = Init();

 .

}

Function Init also initializes LogSource. (Don't complain to me, I did not 
write this code. I'm just refactoring it).



Questions:



1.      Is the initializer for LogSource (function abc) guaranteed to 
execute before Init?

2.      Is the assignment part of the call to the initializer for DBList 
thread safe?

3.      Presently, the implementation of function init does everything 
inside a lock statement. Is this lock statement superfluous?



 Thanks!

Siegfried


6. thread-safety - CSharp/C#

7. Thread Safety with Events

Hello, Newsgroupians:

I've yet another question.  I have an assembly that provides the interface 
for a scanner.  The scanner has a method on it called Read().  The prototype 
is as follows...

void TheirScannerClass.Read();

To retrieve the information from the scan, I need to add an event to the 
created object.  EXA:

TheirScannerClass scanner = new TheirScannerCLass();
scanner.ReadEvent += new TheirScannerClass.ReaderReadEvent(SomeMethod);
scanner.Read();

void SomeMethod(ReadEventArgs rea)
{
  MessageBox.Show(rea.Value.ToString());
}

So if I executed the above code, the value read would be located in 
ReadEventArgs.

I am trying to create a wrapper for TheirScannerClass, for the assembly 
architecture is pourly designed.  If I create my own method called Read(), I 
want the return type to be the ReadEventArgs, which is from the event.  For 
instance, I'll create a TheirScannerClass as a member of my class.  In my 
class I'll also create a method called Read, which will return the value 
read.  The question is how can I return the EVENT'S ReadEventArgs for my 
method Read()?

EXA:

class MyScannerClass
{
  TheirScannerClass m_scanner;

  public MyScannerClass
  {
     m_scanner = new TheirScannerClass;
     m_scanner.ReadEvent += new 
TheirScannerClass.ReaderReadEvent(MyScannerClass.ReadEvent);
  }

  public static void ReadEvent(ReadEventArgs rea)
  {
    ...
  }

  public static void Read

  public string Read()
  {
    m_scanner.Read();

    // Some how have the event's value return here

  }
}

I could probably do this using a class variable, but the problem I'd like to 
tackle is thread safety.  How can I ensure that the value in Read() is for 
that specific thread.  Thank you, all, for your time and consideration in 
this matter.


Trecius

8. thread safety / static method - CSharp/C#