Thread: Communicating with threads

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    Communicating with threads

    Hi Guys

    I'm not very experienced with thread programming. I have studied the concepts of thread programming at university so i understand how they work and the importance of setting up mutex's etc but never actually used them in real practice so i have a very general question about how two threads should communicate.

    The application im writing is a chat program using winsock. The main thread is being used for window gui programming, like displaying windows and handling the windows message queue. Whilst the other thread is the server thread which listens for incoming connections and talks to different clients on different sockets. This thread is mainly the select function and reposinsible the sending and receiving of data.

    Now what i want to know is what is the standard way of communication between the two threads? So if the user wants to chat to another user they double click their name in the window which opens up a new window for them to start chatting. The windows thread registers the user double clicking on someone elses name and then needs to inform the winsock thread of the new communication that needs to be opened. However this thread is currently blocking on the winsock select function.

    Thanks for any help

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You can either use a mutex or create an event. However Winsock provides its own WSACreateEvent() and WSAEventSelect() for this very thing.

    I use this system to determine when a client disconnects in my code.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Hi bubba, thanks for the reply.

    I've been looking into this WSACreateEvent and WSAEventSelect and from what i've gathered these events are only used when an event happepns on the associated socket, like there is something to read, or something to accept.

    So how would i use this to actually trigger the event myself rather than the event being triggered itself by a new connection etc. So i want to be able to manually trigger the event from the windows thread and handle it in the winsock thread?

    Thanks again for your help mate it's greatly appreciated

    Oh and by the way, when i originally posted this thread it was more aimed at general communication between threads and was not meant to be winsock specific which is why i posted it in the general forum. However now it has become winsock specific (which is fine), perhaps it belongs in the network forum?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For normal Windows, SetEvent, ResetEvent, WaitForSingleObject, WaitForMultipleObjects are event functions.
    Critical Sections, Mutexes are for thread synchronization.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This depends on what you are trying to do. If you simply want to know when you can send or there is something to receive or the socket has closed down or had an error the WSA events are what you need. If you look closely the data type of the events is HANDLE which is what you pass to the wait functions. So you can use these to communicate from a socket thread to a non-socket thread.

    To do what you want you need to create an event handle with CreateEvent() and wait for it with WaitForSingleObject(). This sounds like you want to basically send a message to another thread saying something happened.

    Mutexes and critical sections are for when a thread is accessing something that another thread may also be accessing at the exact same time. These provide a mechanism for dealing with that problem.

    I'm gonna move this to the Windows board since it has more to do with events than just a C++ question.
    Last edited by VirtualAce; 12-24-2007 at 01:46 PM.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Generally speaking, each chat window would run in its ownthread, so when the user dbl clicks a users name, it owudl create a new thread specifically to handle the connection to that client. That way the thread can use blockign code without blockign the rest of the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM