valgrind在多线程套接字程序中停止 [英] valgrind stalls in multithreaded socket program

查看:249
本文介绍了valgrind在多线程套接字程序中停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用valgrind运行多线程套接字程序.客户端将通过TCP向服务器发送请求,然后忙于等待布尔值.当调用服务于服务器响应的回调函数时,将设置布尔值.一旦收到响应(并设置了布尔标志),服务器将再次发出请求,并在循环中重复执行此请求.

我意识到对共享变量(布尔值)的非同步访问会导致线程问题,但是我尝试使用pthread互斥锁,程序速度降低了大约20%(速度在这里很重要).我相信写入共享的布尔变量是可以的,因为它可以在单个周期内完成.

该程序在valgrind之外可以正常运行,但是在使用valgrind运行时通常会停顿.我让程序整夜运行..通常需要几秒钟才能完成,所以我认为没有等待足够长的时间才能完成程序的情况.线程由开源引擎框架(快速修复)管理,因此我认为线程的创建/管理方式没有问题.

有人知道关于多线程程序/繁忙的等待循环/套接字通信(或这些的组合)的valgrind有任何问题吗?

解决方案

虽然其他答案集中在坚持要求您采用标准同步方法(我完全同意),但我认为我应该回答您有关Valgrind的问题. /p>

据我所知,Valgrind在多线程环境中运行没有问题.我相信Valgrind会强制应用程序在单个内核上运行,但除此之外,它不会影响您的线程.

Valgrind可能对您的应用程序所做的事情正在改变线程之间的时序和交互,从而可能暴露出独立运行时通常看不到的代码中的错误和竞态条件.

在我看来,您用来确定该错误可能不在您正在使用的开源线程框架中的逻辑也适用于Valgrind.我建议您将这些挂起视为代码中的错误并对其进行调试,因为这很有可能是它们.

作为旁注,使用互斥锁可能对您描述的问题有些过大.您应该改为研究信号量或条件变量.

祝你好运.

I'm running a multithreaded socket program with valgrind. The client will send out a request to the server over TCP, and then busy wait on a boolean. The boolean will be set when the callback function which services the response from the server is called. Once the response is received (and the boolean flag is set), the server will again send out a request, and do this repeatedly in a loop.

I realise that unsychronised access to shared variables (the boolean) can cause threading issues, but I've tried using pthread mutexes, and the program slows down by about 20% (speed is of importance here). I'm confident that writing to the shared boolean variable is fine as it can be done in a single cycle.

The program runs fine outside of valgrind, but will often stall when run with valgrind. I left the program to run overnight.. usually it takes a few seconds to complete, so I don't think it's a case of not waiting long enough for the program to finish. The threading is managed by the open source engine framework (quick fix), so I don't think it's a problem with how the threads are created/managed.

Does anyone know of any problems with valgrind around multi threaded programs/busy wait loops/socket communications (or a combination of these)?

解决方案

While other answers focus on insisting that you take the standard synchronization approach (something I fully agree with), I thought instead I should answer your question regarding Valgrind.

As far as I know there are no issues with Valgrind running in multi-threaded environment. I believe Valgrind forces the application to run on a single core, but other than that it should not affect your threads.

What Valgrind is probably doing to your application is altering the timings and interactions between your threads in ways that might be exposing bugs and race conditions in your code that you don't normally see while running stand-alone.

The same logic you applied to decide that the bug could not be in the open source threading framework you are using also applies to Valgrind in my opinion. I recommend that you consider these hangs as bugs in your code and debug them as such, because that is most likely what they are.

As a side note, using a mutex is probably overkill for the problem you described. You should investigate semaphores or condition variables instead.

Good luck.

这篇关于valgrind在多线程套接字程序中停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆