线程和 pcap 问题
我有一个允许用户扫描网络的 GUI 程序,问题是当调用 pcap_loop 函数时,我的 GUI 程序变得无响应。(pcap_loop 阻止当前线程)。
当我尝试使用 pthreads 时,我在 pcap_loop 函数处收到 SIGSEGV 错误。为什么?就好像线程看不到 procPacket 函数本身。
void procPacket(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet)
{
//show packets here
}
void* pcapLooper(void* param)
{
pcap_t* handler = (pcap_t*) param;
pcap_loop(handler, 900 ,procPacket, NULL );
}
//some function that runs when a button is pressed
//handler has been opened through pcap_open_live
pthread_t scanner;
int t = pthread_create(&scanner,NULL,&pcapLooper, &handler );
if(t)
{
std::cout << "failed" << std::endl;
}
pthread_join(scanner,NULL);
//do other stuff.
I have a GUI program that allows a user a scan a network, the issue is that when the pcap_loop function is called, my GUI program becomes unresponsive.(the pcap_loop blocks the current thread).
When i try to use pthreads, i got a SIGSEGV fault at the pcap_loop function.Why?It's as if the thread can't see the procPacket function itself.
void procPacket(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet)
{
//show packets here
}
void* pcapLooper(void* param)
{
pcap_t* handler = (pcap_t*) param;
pcap_loop(handler, 900 ,procPacket, NULL );
}
//some function that runs when a button is pressed
//handler has been opened through pcap_open_live
pthread_t scanner;
int t = pthread_create(&scanner,NULL,&pcapLooper, &handler );
if(t)
{
std::cout << "failed" << std::endl;
}
pthread_join(scanner,NULL);
//do other stuff.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我强烈建议不要使用线程,除非绝对必要。
问题是您必须非常小心,以避免竞争条件和其他同步问题。例如,您的 GUI 框架库可能不希望从多个线程调用,因此您的
//showpacketshere
例程可能会使其非常困惑。相反,如果可能的话,我建议从主线程读取数据包。你没有说你正在使用哪个 GUI 框架;因为您使用的是 C++,所以我假设使用 Qt,因为这很常见,但所有其他框架都有类似的功能。
您需要做的是:
(至于为什么您的代码当前崩溃 - 请注意,您可能希望将
handler
而不是&handler
作为参数传递给pthread_create()
。但是仅仅解决这个问题可能会导致以后出现奇怪的不可靠性 - 所以单线程几乎肯定是前进的方向!)I would strongly suggest not using threads unless you absolutely have to.
The problem is that you have to be extremely careful to avoid race conditions and other synchronisation problems. For instance, your GUI framework library is probably not expecting to be called from multiple threads, and so your
//show packets here
routine may confuse it a lot.Instead I would suggest, if possible, reading the packets from the main thread. You don't say which GUI framework you're using; since you're using C++ I'll assume Qt since that's quite common, but all other frameworks have similar functionality.
What you need to do is:
(As to why your code is currently crashing - note that you probably want to pass
handler
rather than&handler
as the parameter topthread_create()
. But just fixing that may lead to strange unreliability later - so going single-threaded is almost certainly the way forward!)你需要更少的“&”。假设
使用
&handle
将是pcap_t **
类型,但您的线程函数将其强制转换回(顺便说一下,强制转换也是毫无意义/冗余的)到pcap_t *
,这会导致使用时出现未定义的行为,并且通常会出错。更好的:You need much less "&"s. Assuming
using
&handle
will be of typepcap_t **
, but your thread function casts it back (the cast by the way is also pointless/redundant) topcap_t *
, which leads to undefined behavior when using it, and is usually going to go wrong. Better: