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 am trying to do a project for school in which I have to write a multithreaded program using pthreads. I can't seem to get the basics down on this, though. Here's my problem - basically my program looks like this:
Code:
#include<pthread.h>
int main(void)
{
pthread_mutex_t *test;
pthread_mutex_init(test, NULL);
}
Now, when I compile this using the following command line it runs just fine...
g++ -o test test.cpp
However, if I use this command line to compile, I get a segmentation fault when I run the resulting binary...
g++ -lpthread -o test test.cpp
I have to use the -lpthread option, however, since the project has to be multithreaded. So, what is the deal? This is an extremely simple program and it doesn't run. Is there an option that I need to be adding that I'm not aware of?
Also, I'm not sure if this is helpful or not, but here it is... if I run the same program with an actual instantiation of the pthread_mutex_t object and pass by reference instead of using a pointer, I get no segmentation faults either way. I need to create these mutexes dynamically, though, so I can't do it this way. Thanks in advance for any help.
That's odd ... have you got more than one version
of libpthread installed?
The thing is that the first version must implicitly
find the library, otherwise you'd be getting linker
errors...
Another thing. My understanding is that pthread
is a C-library (not sure whether gcc comes with
c++ wrappers for it, never used it), but you're compiling
that snippet with g++
I have another problem involving pthreads now... I have to be able to create the threads dynamically because the number of client threads varies. Here is a snippet of code that works...
uhh, u need to initialize those pointers with dynamically allocated memory, they point to nothing. or u need to use a plain pthread_t and pass it by reference.
int main(void)
{
pthread_mutex_t *test; <-- BAD!!! You didn't point the pointer to anything, use static object instead (i.e. pthread_mutex_t test
pthread_mutex_init(test, NULL); <-- "&test" for static object, not "test"
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.