Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Kaz Kylheku Newsgroups: comp.os.linux.development.system Subject: Re: Calling libiptc API from a separate thread in a C program throws segmentati Date: Tue, 17 Jan 2012 18:48:24 +0000 (UTC) Organization: A noiseless patient Spider Lines: 41 Message-ID: <20120117102310.171@kylheku.com> References: Injection-Date: Tue, 17 Jan 2012 18:48:24 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="UCk3K/YilCUd19+i749f3A"; logging-data="16355"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18RJ4Ws9fY7r8NiMG1HTeb6iIgwLhcAZPQ=" User-Agent: slrn/pre1.0.0-18 (Linux) Cancel-Lock: sha1:eIc5CMynui1P9FyACsfJzIf55L0= Xref: x330-a1.tempe.blueboxinc.net comp.os.linux.development.system:380 On 2012-01-17, kkgarg78 wrote: > I am working on performing iptables update through a custom c program using > libiptc. > > The requirement is to invoke iptc APIs from a separate thread every 2 seconds. Even if there is no change to the rules? Are you monitoring for changes, or updating? > The problem, I am facing is that call to both iptc_init() and iptc_free() > works > well when called from main function. However, call to iptc_free() fails with > "Segmentation Fault" when called from thread_func(). I'm looing at the library sources online. It does not look thread-safe at all, though it's not obvious how that could cause a crash in the above program, since your use of the library is serialized (no concurrrency between your main and thread_func). You should be fine if you have a dedicated thread which calls this library, or put a big mutex around it. > Compilation: > > # gcc -o test thread_iptc.c -lpthread -lext4 -lext6 -lip4tc -lip6tc -liptc > -lxtables -ldl This is not how you compile programs for multithreading. You want: # gcc -o test -pthread thread_iptc.c -lext4 -lext6 -lip4tc -lip6tc -liptc -lxtables -ldl I.e. the -pthread gcc option, not -lpthread. It is not enough to link the threading library (and -pthread does that). One consequence of using -lpthread alone is that the "errno" variable will not be properly thread-specific. At one point -pthread was equivalent to -D_REENTRANT and -lpthread, but that may change in the future if it has not already; it's better to use the -pthread interface.