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:
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:
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 08:00 AM..
Reason: adding code tags
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 :])
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.
The thread functions and data types are declared in pthread.h, but the pthread functions are not included in the standard C library "libc". Instead, they are in a separate library "libpthread", so you should add lpthread to the command line when you link your program.
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.
Hi All,
To avoid race condition, instead of using mutex, semaphore, spinlock etc.... Is there any other mechanism by which we can avoid race condition in an multi-threading environment.
-Thanks (6 Replies)
Hello,
I have got some issue with the struct variable with passed arguments
the variable in the sturct is only recognize the last value their assigned to
I'm pretty confused why the mutex didn't work out
here is my program:
#include<stdio.h>
#include<pthread.h>
pthread_mutex_t lock... (3 Replies)
Hi all,
I have a scenario where I need to use the mutex locks. The mutex locks are working fine, but sometimes I am getting into the dead lock situation.
Below is the summary of my code :
MUTEX LOCK
performTask();
MUTEX UNLOCK.
In some cases I get into the situation where... (2 Replies)
Hello Everyone,
I just joined this forum and this is my first post.
I would like to know how can I impliment basic read/write locks in perl. I have a database (file) which can be accessed simultaneously but has to be locked while writing.
If there is no such support in perl, my next... (6 Replies)
Hi, I have two tasks 'Read' and 'Write' which reads and writes on a file "abc.txt" respectively.
Now I need to restrict the Write operation on the file while Read is going on, But can allow two Reads at a time.
ie. two Reads can happen simultaneously, but Write can't happen at Read is going on.
... (3 Replies)
A shell in crontab per 5 min write a file
B shell in crontab per 6 min read a file
how to lock the share file a ;avioid confilict in write and read?
Thx : -) (1 Reply)
Hi all,
I am working in a UNIX/C environment. I would like to understand more about MUTEX and Threads. Can someone explain me these
concepts and how they are related.
Vijay (2 Replies)