组播程序丢失数据

发布于 2024-11-01 03:50:28 字数 267 浏览 1 评论 0原文

我有一个用 C 编写的多线程程序,一个线程正在从网络接收多播数据并将其存储在队列中,另一个线程继续读取队列并将其写入文件。一切都工作得很好,即没有数据从多播网络丢失。

线程1:读取组播数据并存入队列 线程2:从队列中读取并将其写入文件。

现在我有来自网络的另一个多播数据源,我需要另一个线程来读取网络数据,然后我只需添加一个for循环来为多播数据创建另一个线程,然后当2个多播线程来回切换时,我多播网络丢失数据!

任何人都知道如果使用 2 个线程,为什么会丢失数据报。谢谢

I've a multithread program written in C, one thread is receiving multicast data from the network and store it in a queue, another thread keep reading the queue and write it to file. Everything work just great i.e. no data lost from the multicast network.

Thread 1: Read Multicast data and store it into a queue
Thread 2: Read from queue and write it to file.

now I have another source of multicast data from network, I need another thread to read the network data, then I just go and add a for loop to create another thread for multicast data, then when the 2 multicast threads switching back and forth, I lost data from the multicast network!

Anyone has idea about why there are lost datagrams if 2 threads are used. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

颜漓半夏 2024-11-08 03:50:28

您可能没有使用任何并发机制,例如信号量或互斥体。一个经典的解决方案是监视器。监视器提供锁来调解并发访问和条件信号,以允许独立进程阻塞访问(无忙等待)。简单来说,这意味着一次只有一个线程可以访问数据。这可以防止读取线程读取写入线程尚未完成写入的数据。它还允许读取线程读取其他读取线程尚未读取的数据。实现此目的的一种方法是使用读写互斥体和访问信号量。每个想要访问数据的线程都会递减访问信号量,该线程将被授予访问权限或休眠,直到它轮到为止。读写互斥锁将阻止读取线程读取数据,直到写入一些数据。

It is likely you are not using any concurrency mechanisms like semaphore or mutexs. A classic solution is a monitor. A monitor provides a lock to mediate concurrent access and condition signals to allow independent processes blocking access (no busy waiting). In plain English this means that only one thread can access the data at a time. This prevents a reading thread from reading data that the writing thread has not yet finished writing. It also allows the reading threads to read data that the other reading thread has not yet read. An approach to implement this is to use a read-write mutex and an access semaphore. Each thread that wants to access the data decrements the access semaphore, the thread will either be granted access or sleep until it gets it turn. The read-write mutex will prevent a read thread from reading until some data has been written.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文