ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm struggling with the following program (sample.cpp):
Code:
#include <stdio.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include "MyLibrary.h"
void someDummyfunction()
{
// Comment this out, and program finishes with exit_group(0)
MyLibraryFunction(0, NULL, 0);
}
int main(int argc, char* argv[])
{
// OR, comment this out, and the program finishes with exit_group(0)
gtk_init( &argc, &argv );
return (0);
}
MyLibrary is a .so that I recently created, and I'm trying to write a GTK enabled program that uses it. I have stripped this program down to just the above to demonstrate the problem I'm having.
When I run this program as shown above, the program 'hangs' -- meaning it runs, but never ends and returns back to the command prompt -- I have to Ctrl-C it. When I strace the program, the last thing it does is a _exit(0).
However, if I comment out either the call to gtk_init(), OR, comment out the reference to MyLibraryFunction() (note that it's simplying referencing the function, it does not call it). -- the program terminates fine. When I strace this variant, I see that the last thing it does it a exit_group(0).
Why the difference?
I've been using the library MyLibrary with non-gtk apps for a while now, and they seem to work fine (they all seem to end with a exit_group). Now MyLibrary is a fairly complicated library, and it too references a number of other libraries. One of these libraries is pthreads, as it does utilize multi-threading. However, I believe all of the threading stuff is 'dynamic' -- only created at runtime (which isn't happening as we are not even calling into it).
Anyways -- I believe the issue is somewhere in MyLibrary, but I was wondering if perhaps someone had some ideas of what to look for based on the _exit vs exit_group behavior I am seeing.
gtk_init calls exit when it fails. That's why you see the particular behavior. gtk_init_check won't call exit, it will return false. gtk_init_with_args does the same but has an error parameter that might help. It contains a message field.
NevemTeve --> I added the definition of the compile option _THREAD_SAFE as you suggested.
SoftSprocekt --> What you stated sounded promising, but alas, it is not my problem. Both gtk_init and gtk_init_check are returning fine (in the latter case, it returns TRUE). In fact if I put a printf just before the return (0), it prints fine. The _exit(0) is coming from the return(0) of main()??
I had read your initial comment, but didn't respond to it -- sorry about that.
Yes, gtk_init might be creating some additional process(es) or thread(s), and I'm not calling any of the 'latter' gtk functions that might clean these up (such as gtk_main(). However, a more 'complete' gtk program is where I started when I discovered this error, using some simple and widly distributed gtk applications that did all of this stuff -- but my program continued to misbehave. I stripped it down to what you see here. Furthermore, if I comment our the reference to MyLibraryFunction, than the program runs fine; both gtk_init() and main() return as expected -- I get the exit_group that seems to be larger is scope (thread-wise).
I also tried using the -f option for strace. Here is what I see at the very end ...
Yes, it certainly does. However, if I don't define the MyLibraryFunction, then it too will show three threads -- but will then do a exit_group() (as opposed to _exit()) and terminate properly.
With that stated however, I've done more experimentation, and my example program now looks as follows:
This program fails in the same way. Basically, if I do the pthread_exit() in main (as I understand I am supposed to do if I am using pthreads) -- then main will 'hang' on this operation if I had previously called gtk_init_check() (or gtk_init()).
Incidentally, I also went back to try my more complete variant of the gtk program (one that actually has a gtk_main()), and it too will hang up main() if I add a call to pthread_exit().
the usual way is to use -pthread both for compiling and linking, otherwise there can be surprises (and do not use -lpthread). Probably the best thing is to use
Code:
CXX=g++ -pthread
and LIBS is not required. But I'm not really sure that will solve this issue.
Your test motivated me to try this same program on another Ubuntu platform. This new platform also has gtk+-3.0 on it BUT... it terminates correctly (with the exit_group).
I need to look more closely at library versions on my initial machine.
just to know: exit is the usual way in a single-threaded app and exit_group is used by multithreaded ones. The behaviour is defined by the (existence of) -pthread.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.