简体   繁体   English

通过MBED OS上的队列和线程复制char *值

[英]Copying char* values through Queues and threads on MBED OS

I am trying to implement some RTOS threads on Arm MBED OS over a K64F board. 我试图通过K64F板在Arm MBED OS上实现一些RTOS线程。 I am parting from the RTOS examples and I have succesfully run and communicated different threads using Queues. 我脱离了RTOS示例,并且已经成功使用Queue运行并传达了不同的线程。 I am having problems when copying char* values from one struct to another to get a message from one queue to another. 将char *值从一个结构复制到另一个结构以将消息从一个队列复制到另一个队列时遇到问题。 I believe I am misunderstanding something and that my problem is related to pointers and memory handling but I am not able to get through it. 我相信我误会了一些东西,我的问题与指针和内存处理有关,但我无法解决。

I have defined diferent queues to send data to various threads. 我定义了不同的队列以将数据发送到各个线程。 I have also created a basic data structure containing everything I need to go among these threads. 我还创建了一个基本数据结构,其中包含我需要在这些线程之间进行的所有操作。 In this struct I have a char* variable (rHostAddr) containing the remote host address that requested a service. 在此结构中,我有一个char *变量(rHostAddr),其中包含请求服务的远程主机地址。

MemoryPool<cMsg, 16> AMPool;
Queue<cMsg, 16> AMQueue;
MemoryPool<cMsg, 16> ioLedPool;
Queue<cMsg, 16> ioLedQueue;

typedef struct{
    ...

    char* rHostAddr;
    ...
} cMsg;

In the Main Thread I am creating this data structure and putting it in the first queue (AMQueue). 在主线程中,我正在创建此数据结构并将其放在第一个队列(AMQueue)中。

--- Main Thread ---
cMsg *message = AMPool.alloc();

char* rcvaddrs = "111.111.111.111";
message->rHostAddr = "111.111.111.111";

rcvaddrs = (char*)addr.get_ip_address();
message->rHostAddr = rcvaddrs;

AMQueue.put(message);

On the Thread 1 I wait for a message to arrive and on certain conditions I copy the whole structure to a new one created from the corresponding pool and insert it on a new queue (ioLedQueue). 在线程1上,我等待消息到达,并在某些条件下,将整个结构复制到从相应池创建的新结构,并将其插入新队列(ioLedQueue)。

--- Thread 1 ---

cMsg *msg;
cMsg *ledm = ioLedPool.alloc();
osEvent evt = AMQueue.get();
msg = (cMsg*)evt.value.p;

msg.rHostAddr = ledm.rHostAddr;
printf("\t -- Host 1 -- %s\n\r", ledm->rHostAddr);
ioLedQueue.put(ledm);

On the Thread 2 I get the message structure and the data . 在线程2上,我获得了消息结构和数据。

--- Thread 2 ---

cMsg *msg;
osEvent evt = ioLedQueue.get();
msg = (cMsg*)evt.value.p;
printf("\t -- Host 2  -- %s\n\r", msg->rHostAddr);

On this stage rHostAddr is empty. 在此阶段,rHostAddr为空。 I can see the value on the printf "Host 1" but not in the "Host 2" 我可以在printf“主机1”上看到该值,但在“主机2”中看不到

I believe (if I am not wrong) that the problem comes from assigning with = operand, as I am copying the address, not the value, and it is lost when first pool memory is freed. 我相信(如果我没记错的话)问题出在用=操作数赋值,因为我要复制地址而不是值,并且在释放第一个池内存时会丢失它。 I have tried copying the value with memcpy, strcpy and even my own char by char but system hangs when calling this methods. 我尝试用memcpy,strcpy甚至我自己的char通过char复制值,但是调用此方法时系统挂起。

How can I copy the value through this queues? 如何通过此队列复制值?

I move it here as the correct answer was written as a comment. 我将其移至此处,因为正确答案已写为评论。 Converting the value to a array of chars was the way to go, so the string data is part of the struct. 将值转换为chars数组是必须的方法,因此字符串数据是struct的一部分。

char rHostAddr[40];

Now the assignation can be done with srtcpy method and it is passed through all the process correctly: 现在,可以使用srtcpy方法完成分配,并且可以正确地通过所有过程进行分配:

char* rcvaddrs = (char*)addr.get_ip_address();

strcpy(message->rHostAddr,rcvaddrs);

Take a look at this solution from ARM mbed: 从ARM mbed看一下这个解决方案:

https://github.com/ARMmbed/mbed-events https://github.com/ARMmbed/mbed-events

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM