These two functions are both infinite loops,
and the programe hangs once called in the same thread.
gtk_main();
...
pcap_loop(adhandle, 0, packet_handler, NULL);
When I click the “start” button,I want pcap to start working;And if I press the “stop” button,pcap stop.
How do I create a child thread and run pcap_loop(adhandle, 0, packet_handler, NULL); instead?
Unless I am missing something, your problem is that both GTK+ and
libpcap are organized around events fired from a main loop. Without
looking through the documentation, I don’t know about GTK+, but there
is another mode of operation for libpcap: You can use
pcap_next()orpcap_next_ex()without giving up control over your program flow.It should be possible to register a function that periodically calls
one of these two functions using
g_timeout_add()org_idle_add(),removing the need to mess around with threads and mutex mechanisms
altogether.