简体   繁体   English

main()和thread()之间的通信

[英]communication between main() and thread()

I have a question about threads communication. 我有一个关于线程通信的问题。 there is client and server. 有客户端和服务器。

server: 服务器:

  1. main function- its job is to listen to some port (TCP communication) and get commands from the client main函数 - 它的工作是监听某个端口(TCP通信)并从客户端获取命令
  2. the thread job is to transmit video fluently to the client. 线程工作是将视频流畅地传输到客户端。

client: 客户:

  1. main function- transmit commands to server main函数 - 向服务器发送命令
  2. thread- watch the video 线程 - 观看视频

the TCP\\video part works fine. TCP \\ video部分工作正常。 after the main function of the server, got the command from the client, I need to send the command to the video thread and send back from the video thread to the server's main- "ok" . 在服务器的主要功能之后,从客户端获取命令,我需要将命令发送到视频线程并从视频线程发送回服务器的主 - “ok”。

the problem is to send commands from the server's main, to the video thread and vice versa. 问题是从服务器的main发送命令到视频线程,反之亦然。

its enough that the command will be one variable.. 足够的命令将是一个变量..

any ideas? 有任何想法吗? thanks! 谢谢!

The pipe is a bad approach towards two way communication, you could use, shared memory. 管道是双向通信的坏方法,你可以使用共享内存。 In shared memory, both processes have access to some memory that can be used read or write, such that writes in one are visible in the reads of the other and vice versa. 在共享内存中,两个进程都可以访问一些可以用于读取或写入的内存,这样一个中的写入在另一个的读取中是可见的,反之亦然。

for more details on shared memory http://www.cs.cf.ac.uk/Dave/C/node27.html 有关共享内存的更多详细信息, 请访问http://www.cs.cf.ac.uk/Dave/C/node27.html

if threads and one variable then use atomic variable. 如果线程和一个变量然后使用原子变量。 if object then use locking (trylock inside video streaming loop and lock write command inside main). 如果对象然后使用锁定(trylock内部视频流循环和锁定写入命令在主内部)。 if you want commands as queue-ed then use safe concurent queue 如果您希望命令为队列,则使用安全的concurent队列

I think on your case: I would do it with two Wait-free ring buffer from boost examples. 我想你的情况:我会用两个来自boost示例Wait-free ring buffer Making two single producer -consumer.In one producer will be main function consumer other thread,on another one vice versa. 制作两个单一的生产者 - 消费者。在一个生产者中将是主要功能消费者的其他线程,在另一个生产者上反之亦然。 (it be like using two pipes in unix but efficient one) (就像在unix中使用两个管道但是效率很高)

A wait-free ring buffer provides a mechanism for relaying objects from one single "producer" thread to one single "consumer" thread without any locks. 无等待环形缓冲区提供了一种机制,用于将对象从一个“生产者”线程中继到一个单独的“消费者”线程而无需任何锁定。 The operations on this data structure are "wait-free" which means that each operation finishes within a constant number of steps. 对该数据结构的操作是“无等待”,这意味着每个操作在恒定步数内完成。 This makes this data structure suitable for use in hard real-time systems or for communication with interrupt/signal handlers. 这使得该数据结构适用于硬实时系统或与中断/信号处理器通信。

Wait-free ring buffer 无等待环形缓冲区

But considering that I am not aware of your situation there are might be more right ways.Maybe redesining at all 但考虑到我不了解你的情况,可能会有更多正确的方法。也许可以重新考虑

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

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