Another thread question - separate threads/objects

I'm (fairly) new to c++ (been programming in Java and high level scripting languages for a while now.) I'm working on porting a java program to c++ which needs to poll/react to data to/from a serial port as well as accept/transmit data that it has processed via a socket connected to a server. I've got the serial communication working and I have the socket portion working as well, but in two test programs.

In Java, I have two separate class objects that run in separate threads that handle these two operations. I have a third class with "synchonized methods" (functions) that I use to pass data to/from the other two classes (I have it separate because I also use it for other secondary things as well.) I basically load it up first, then pass it as a parameter to the other two on init.

There is a "synchonized method" to track whether there is anything to react to from the serial port, and another for the socket. So the threads check those methods with each loop. Then depending on the data returned from the check, it calls another method to react/work on what it needs to.

I'm having a hard time wrapping my head around whether to assign "friend" function(s)/class(es), use the main thread to somehow monitor the other two and pass info back/forth, or some other procedure to achieve what I need. In java it seems a bit easier because it is a higher level language and a lot of the grunt work is all ready there in the language. My steepest learning curve to jump to c++ has been to learn to code what I need at a base level, or try to find a library that has all ready implemented it for me and use that. For my testing, I've been using the boost libraries. I'm using them in another context for the regex functions they provide.

Let me know if there's anything else I can provide to make things clearer. Thanks for your help!
So, let me get this straight. The flow of information is like this, right?
(serial port) ----> (your code) ----> (network)
If so, I don't really see the point of even using threads at all. Correct me if I'm wrong.
@helios: Thanks for the reply!

Well let's see. There should be an arrow pointing in both directions in your diagram. The serial port is being polled in a loop. Not everything that is received from the serial port needs to go to the socket. Also, not everything from the socket needs to go the serial port. The socket thread needs to be able to interact with the server without impacting the serial port thread and vice versa.

...some code...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
main.cpp
SerialPort serialPort = SerialPort(<port>, <timeout>, DEBUG);
ClientSocket client = ClientSocket(<server>,<port>, DEBUG);

<create serialPort thread>
<create client thread>

serialPort code...

string sendMsg = defaultMsg;
string serialMsg;

while(running) {
    if (sendCommand(sendMsg) == 0) {
        serialMsg = this->getMsg();

        if (serialMsg == "XXXX") {
            // all quiet on the port, so do some busy work
            // check the client for server queries
            if (client.hasMsg) {
                sendMsg = client.getMsg;
            }
        } else {
            client.SendMsg(serialMsg);
            sendMsg = defaultMsg;
        }
    } else {
        // we have a serial port problem, deal with it!
    }
}


Basically, the socket client is similar to this, but it does more with messages from the server that don't pertain to the serial port operations, like write local config files.
Last edited on
Ah, alright. Like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const int timeout=100;

void serial_thread(Serial &serial,Queue &to_socket_thread,Queue &from_socket_thread){
    while (some_condition){
        from_socket_thread.wait_with_timeout(timeout);
        while (from_socket_thread.size())
            serial.push(from_socket_thread.pop());

        serial.wait_with_timeout(timeout);
        while (serial.size())
            to_socket_thread.push(serial.pop());
    }
}

void socket_thread(Socket &socket,Queue &from_serial_thread,Queue &to_serial_thread){
    while (some_condition){
        from_serial_thread.wait_with_timeout(timeout);
        while (from_serial_thread.size()){
            Message m=from_serial_thread.pop();
            if (should_be_sent_to_socket(m))
                 socket.push(m);
        }

        socket.wait_with_timeout(timeout);
        while (socket.size()){
            Message m=socket.pop();
            if (should_be_sent_to_serial(m))
                 to_serial_thread.push(m);
        }
    }
}
@helios: thanks again!

I'm wondering, can you recommend a book or other resource that might help me to get a better grasp on thread programming in c++? I've been browsing Amazon, and I've tried to read everything I can from a rudimentary Google search. But I'm not sure I'm over the learning hump yet to be able to sit down and churn out some usable code.
Topic archived. No new replies allowed.