|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Mutex will not work
I am trying to use mutex in my multi-tread project, but they don't seem to work. I have created a simple demonstration of the problem. This is NOT how I would use a mutex, only a demonstration of the problem: Code:
#include <stdio.h>
#include <pthread.h>
int main()
{
int val;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
printf("Test of mutex\n");
val = pthread_mutex_lock(&mutex);
printf("Lock 1 returned %d\n",val);
val = pthread_mutex_lock(&mutex);
printf("Lock 2 returned %d\n",val);
val = pthread_mutex_trylock(&mutex);
printf("Lock 3 returned %d\n",val);
return(0);
}Basically, I have a mutex which I lock. It returns 0, its OK. I then try to lock it a second time. It should block (I think). The program comes straight back with val = 0? There is also a compilation problem: The call pthread_mutex_trylock() will compile OK (it is prototyped in pthread.h) but it will not link! It says 'undefined reference' It has to be commented out for the program to run. I am running ubuntu, and GCC - v gives the following: Code:
Using built-in specs. Target: i486-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.2-1ubuntu12' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu Thread model: posix gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu12) I am sorry for the length of the post, but I wanted to get the relevant facts. Can anybody see where I have gone wrong? Thanks in advance. Chris. Last edited by Franklin52; 02-16-2009 at 07:00 AM.. Reason: adding code tags |
| Sponsored Links | |
|
|
|
#2
|
|||
|
|||
|
I guess you didn't linked the library :
$ gcc -c simple.c $ gcc -o simple simple.o -lpthread $ ./simple the first lock works and the second lock block the program because he is waiting the first lock to be unlocked. (which cannot be done since the program is waiting the lock to be unlocked before continue :]) |
| Sponsored Links | ||
|
|
|
#3
|
|||
|
|||
|
Thanks for the reply.
Yes that makes the difference. It works for me now as well. The strange behaviour is why did it link WOTHOUT the added library? Its as though there are some dummy functions for when you don't link in the library! Chris. |
|
#4
|
|||
|
|||
|
Quote:
That's also an issue with trigonometric functions such as sin and cos, they are declared in math.h but the functions are in the library "libm". To compile programs with these functions you have to add -lm to the command line. Regards |
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Mutex in Perl | superuser84 | Shell Programming and Scripting | 6 | 11-12-2007 11:55 PM |
| what is diff b/w semaphore and mutex | amitpansuria | UNIX for Dummies Questions & Answers | 1 | 08-21-2007 01:45 AM |
| mutex | sagar | UNIX for Dummies Questions & Answers | 2 | 02-04-2002 05:00 PM |
| Threads and Mutex | vthasan | Programming | 2 | 11-02-2001 06:34 PM |
|
|