C中线程之间的全局变量同步

时间:2016-03-22 04:53:39

标签: c multithreading communication semaphore

我正在开发一种由Arduino控制的机器人。它将通过TCP从我的PC上的程序接收命令,并通过串行通信转发到Arduino。一旦Arduino执行命令,安装在其上的传感器将环境状态返回到PC。然后程序将运行一些算法来决定接下来要采取的操作。我能够接收方向命令以从Android应用程序手动移动机器人。我使用Raspberry Pi作为消息传递/控制设备。

PC,Android应用程序和Arduino草图上的程序已经完成并经过测试。但是在Rpi上运行的程序中的几个线程之间的通信问题仍然存在。

到目前为止,我有以下内容(TCP,串口,蓝牙套接字代码被省略,因为它们超出了本问题的范围):

int canForwardToPC, recvFromPC, recvFromAndroid, recvFromAr, canSendCommand, isWaitingForInstruc, isWaitingForPos;

//all the int are initialized to be 0

void *ar_send(){
    int status;

    do{
        if(canSendCommand){
            status = write(ser, output, BUFFER_SIZE);
            if(status != -1) {

                printf("Sent to Arduino: %s\n", output);

                memset(&output[0], 0, sizeof(output));

                isWaitingForGridStr = 1;
                canSendCommand = 0;

                usleep(1000000);

            }else{
                ar_isConnected = 0;
                printf("Error Writing\n");
            }
        }
    }while(1);

}

void *tcp_send(){

    int status;

    do{
        if (canForwardToPC && tcp_isConnected){
            status = write(newsockfd, output, strlen(output));
            printf("Sent to PC: %s\n", output);
            memset(&output[0], 0, sizeof(output));
            if (status > 0) {

                isWaitingForInstruc = 1;
            }

            else{
                tcp_isConnected = 0;
                usleep(10000000);
            }
            canForwardToPC = 0;
        }
    } while (1);

}

void *bt_recv(){

    int bytes_read;

    do{
        bytes_read = read(client, bt_buffer, sizeof(bt_buffer));
        if(bytes_read > 0) {

            printf("Received \"%s\" from Android\n", bt_buffer);
            recvFromAndroid = 1;

        }
        else{

            bt_isConnected = 0;

        }
    }while(1);

}

void *ar_recv(){

    do{
        if(isWaitingForGridStr){

            n  = read(ser, ar_buffer, BUFFER_SIZE);

            if(n <= 0) continue;

            ar_buffer[BUFFER_SIZE] = '\0';

            printf("Received %s from Arduino.\n", ar_buffer);

            isWaitingForGridStr = 0;

            recvFromAr = 1;

        }
    }while(1);

}

void *tcp_recv(){

    do{
        if (tcp_isConnected && isWaitingForInstruc){


            n = read(newsockfd, tcp_buffer, BUFFER_SIZE);


            isWaitingForInstruc = 0;

            printf("Received %s from PC.\n", tcp_buffer);
            if (n > 0){
                recvFromPC = 1;

            }else{
                tcp_isConnected = 0;
                usleep(10000000);
            }
        }
    } while (1);

}

void *controller(){
do{
    if(recvFromAndroid){

        recvFromAndroid = 0;



        char temp[1];
        temp[0] = bt_buffer[0];

        canForwardToPC = 1;

        strncpy(output, temp, sizeof(temp));
        memset(&bt_buffer[0], 0, sizeof(bt_buffer));


    }

    else if(recvFromAr){

        recvFromAr = 0;


        canForwardToPC = 1;

        isWaitingForInstruc = 1;]

        strncpy(output, ar_buffer, sizeof(ar_buffer));
        memset(&ar_buffer[0], 0, sizeof(ar_buffer));

    }

    else if(recvFromPC){

        recvFromPC = 0;

        canSendCommand = 1;

        strncpy(output, tcp_buffer, sizeof(tcp_buffer));
        memset(&tcp_buffer[0], 0, sizeof(tcp_buffer));

    }

    }while(1);

}

每个方法都是一个线程,将在主函数中创建并加入。

从我看到的,输出受到所有整数的良好保护,接收和发送线程正确组织。代码似乎是合法的。

但我有以下部分输出,不按计划进行:

Sent to PC: W1
Received W1 from PC.
Sent to Arduino: W1
Received 0:0:0:2:0:3 from Arduino.
Sent to PC: 0:0:0:2:0:3
Received W1W1W1W1W1D180W1A180W1 from PC.
Sent to Arduino: W1W1W1W1W1D180W1A180W1
Received -1:0:-1:2:0:3 from Arduino.
Received W1D180W1A180W1W1W1W1A180 from PC.
Sent to PC: -1:0:-1:2:0:3
Received W1 from PC.
Sent to Arduino: W1
Received -1:-1:-1:-1:0:3 from Arduino.
Received W1D180W1D180W1W1W1A180W1 from PC.
Sent to PC: -1:-1:-1:-1:0:3
Received W1 from PC.
Sent to Arduino: W1
Received -1:-1:-1:-1:0:3 from Arduino.
Received D180W1W1W1W1W1W1D180W1 from PC.
Sent to PC: D180W1W1W1W1W1W1D180W1
Received A180 from PC.
Sent to Arduino: A180
Received -1:-1:-1:1:0:3 from Arduino.
Sent to PC: -1:-1:-1:1:0:3
Received W1W1W1D180W1W1W1W1 from PC.
Received W1 from PC.

我唯一要发送给PC的是位置字符串,其格式为x:x:x:x:x:x。但是有一个实例,D180W1W1W1W1W1W1D180W1被发送到PC。

预期输出应重复如下: 发送到PC 从PC接收 发送给Arduino 收到Arduino

我怀疑发生了一些导致意外输出的全局变量不一致。

我找到了几篇关于信号量和互斥量的文章,但这些只是2个线程的解决方案。我在这里运行了6个线程,我不知道如何实现信号量。

如何解决数据不一致问题?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

根据你告诉我的内容: 您应该为每个传入和传出通道实​​现不同的消息队列。每个通信线程仅负责将传入消息放入队列,或从队列中获取消息并将其发送到其通信端口。每个队列都有锁(互斥或其他)。主控制器(1个线程)将可以访问所有队列,并将处理它们之间的消息移动。每个队列的互斥锁将由控制器或其通信线程锁定。

过程示例:

  1. 从PC读取的线程处理接收消息。
  2. 在获取锁定后将其置于来自PC的传入消息队列中,然后释放锁定。
  3. 控制器唤醒(通过我推荐的定期调度,或通过某种事件机制)并尝试获取来自PC的传入消息队列的锁定。
  4. 它获取锁并在那里读取第一条消息。它发现它有一条消息给arduino。
  5. 接收消息,从PC锁定释放传入。
  6. 获取对arduino锁的传出,并将消息放入传出到arduino队列中。
  7. 发送到arduino线程唤醒,获取其队列锁定,获取所有消息并发送到arduino。
  8. 发送到arduino线程释放锁定并进入睡眠状态。
  9. 可以定期或在控制器发生事件时使所有传出线程处​​理。 希望这会有所帮助。