LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2004, 06:10 PM   #1
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Rep: Reputation: 30
pthread mutex issue


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.

Last edited by gauge73; 04-19-2004 at 08:34 PM.
 
Old 04-19-2004, 08:11 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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++


Cheers,
Tink
 
Old 04-19-2004, 08:33 PM   #3
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Original Poster
Rep: Reputation: 30
I'm not sure if I have more than one version installed. Does this help any?

Code:
$find / -name *pthread*
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/include/gc_pthread_redirects.h
/usr/lib/perl5/5.8.0/i386-linux-thread-multi/bits/pthreadtypes.ph
/usr/lib/libpthread_nonshared.a
/usr/lib/libpthread.a
/usr/lib/libpthread.so
/usr/lib/libgnomevfs-pthread.so
/usr/share/man/man3/pthread_attr_destroy.3thr.gz
/usr/share/man/man3/pthread_atfork.3thr.gz
/usr/share/man/man3/pthread_attr_getinheritsched.3thr.gz
/usr/share/man/man3/pthread_attr_getdetachstate.3thr.gz
/usr/share/man/man3/pthread_attr_setinheritsched.3thr.gz
/usr/share/man/man3/pthread_attr_getschedparam.3thr.gz
/usr/share/man/man3/pthread_attr_getschedpolicy.3thr.gz
/usr/share/man/man3/pthread_attr_getscope.3thr.gz
/usr/share/man/man3/pthread_attr_init.3thr.gz
/usr/share/man/man3/pthread_attr_setdetachstate.3thr.gz
/usr/share/man/man3/pthread_cleanup_pop_restore_np.3thr.gz
/usr/share/man/man3/pthread_attr_setschedparam.3thr.gz
/usr/share/man/man3/pthread_attr_setschedpolicy.3thr.gz
/usr/share/man/man3/pthread_attr_setscope.3thr.gz
/usr/share/man/man3/pthread_cancel.3thr.gz
/usr/share/man/man3/pthread_cleanup_pop.3thr.gz
/usr/share/man/man3/pthread_cleanup_push_defer_np.3thr.gz
/usr/share/man/man3/pthread_cleanup_push.3thr.gz
/usr/share/man/man3/pthread_condattr_destroy.3thr.gz
/usr/share/man/man3/pthread_cond_broadcast.3thr.gz
/usr/share/man/man3/pthread_cond_destroy.3thr.gz
/usr/share/man/man3/pthread_cond_init.3thr.gz
/usr/share/man/man3/pthread_cond_signal.3thr.gz
/usr/share/man/man3/pthread_cond_timedwait.3thr.gz
/usr/share/man/man3/pthread_cond_wait.3thr.gz
/usr/share/man/man3/pthread_kill_other_threads_np.3thr.gz
/usr/share/man/man3/pthread_condattr_init.3thr.gz
/usr/share/man/man3/pthread_create.3thr.gz
/usr/share/man/man3/pthread_detach.3thr.gz
/usr/share/man/man3/pthread_equal.3thr.gz
/usr/share/man/man3/pthread_exit.3thr.gz
/usr/share/man/man3/pthread_getschedparam.3thr.gz
/usr/share/man/man3/pthread_join.3thr.gz
/usr/share/man/man3/pthread_key_create.3thr.gz
/usr/share/man/man3/pthread_kill.3thr.gz
/usr/share/man/man3/pthread_mutexattr_destroy.3thr.gz
/usr/share/man/man3/pthread_mutex_destroy.3thr.gz
/usr/share/man/man3/pthread_mutex_init.3thr.gz
/usr/share/man/man3/pthread_mutex_lock.3thr.gz
/usr/share/man/man3/pthread_mutex_trylock.3thr.gz
/usr/share/man/man3/pthread_mutex_unlock.3thr.gz
/usr/share/man/man3/pthread_mutexattr_getkind_np.3thr.gz
/usr/share/man/man3/pthread_mutexattr_gettype.3thr.gz
/usr/share/man/man3/pthread_mutexattr_init.3thr.gz
/usr/share/man/man3/pthread_mutexattr_setkind_np.3thr.gz
/usr/share/man/man3/pthread_mutexattr_settype.3thr.gz
/usr/share/man/man3/pthread_once.3thr.gz
/usr/share/man/man3/pthread_self.3thr.gz
/usr/share/man/man3/pthread_setcancelstate.3thr.gz
/usr/share/man/man3/pthread_setcanceltype.3thr.gz
/usr/share/man/man3/pthread_setschedparam.3thr.gz
/usr/share/man/man3/pthread_sigmask.3thr.gz
/usr/share/man/man3/pthread_testcancel.3thr.gz
/usr/include/g++-3/pthread_alloc
/usr/include/g++-3/pthread_alloc.h
/usr/include/bits/pthreadtypes.h
/usr/include/pthread.h
/usr/include/c++/3.2.2/bits/pthread_allocimpl.h
/usr/include/c++/3.2.2/bits/stl_pthread_alloc.h
/lib/i686/libpthread-0.10.so
/lib/i686/libpthread.so.0
/lib/tls/libpthread-0.29.so
/lib/tls/libpthread.so.0
/lib/libpthread-0.10.so
/lib/libpthread.so.0
According to what I read pthreads are perfectly valid in C++. Maybe I didn't understand what you meant. I'm a total programming newb.
 
Old 04-20-2004, 01:38 PM   #4
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Original Poster
Rep: Reputation: 30
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...

Code:
pthread_t clientThread;
clientThread=pthread_create(&clientThread, NULL, &fnptr, (void *)dummy);
This works because I pass the pthread_t instantiation by reference. However, if I choose to use a pointer, it doesn't work. This code does not work:

Code:
pthread_t *clientThread;
clientThread=pthread_creat(clientThread, NULL, &fnptr, (void *)dummy);
I assume these issues are related, and I still haven't solved them.
 
Old 04-20-2004, 04:27 PM   #5
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 04-20-2004, 04:56 PM   #6
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
There's exectly the same problem in both pieces of code. The first one should be
Code:
int main(void)
{
pthread_mutex_t test;
pthread_mutex_init(&test, NULL);
}
(or dynamicall alocate it).
 
Old 04-20-2004, 05:29 PM   #7
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
First program:
include<pthread.h>

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"
}

The other code has the same issue.

Hope that helps!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Named mutex in Linux tbruer1138 Programming 6 05-17-2016 09:25 AM
timeout for a mutex lock dmail Programming 6 10-24-2005 07:22 AM
Pthread.... stuffs ... onwer of mutex rajsun Programming 1 07-26-2005 07:40 AM
Mutex destroy failure?? What the magis Linux - Newbie 15 03-04-2005 02:13 AM
How does pthread_mutex_lock() lock mutex in pthread icoming Programming 0 12-04-2004 08:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration