C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-04-2007, 07:22 AM   #1
Registered User
 
Join Date: May 2004
Posts: 19
Unhappy Mutex across multiple source files

Hi all!

I'm working on a prog that uses mutexes. Since the code is growing bigger and bigger i decided to split it across multiple C and H files. To my surprise the mutexes stopped working!

I have no idea why this happened.

The general idea of the code is this:

A function that receives the mutex in its argument:
Code:
void function(pthread_mutex_t mut) {
    pthread_mutex_lock(&mut);
    printf("This shows up if function is in the same source file as the calling code.");
    pthread_mutex_unlock(&mut);
}
The code that calls the function:
Code:
pthread_mutex_t mut;
pthread_mutex_init(&mut, NULL);

pthread_mutex_lock(&mut);
printf("This shows up!");
pthread_mutex_unlock(&mut);

function(mut);

pthread_mutex_destroy(&mut);
Sample Makefile:
Code:
CC = gcc
CFLAGS = -Wall -ansi -pedantic -g  -D _XOPEN_SOURCE=600
OBJECTS = main.o function.o

all: $(OBJECTS)
        $(CC) $(CFLAGS) -lpthread -lrt -lm -o main $(OBJECTS)

main.o: main.c main.h
function.o: function.c function.h

clean:
        rm -f *.o main
Does anyone have any idea why this happens, and how to solve it?

Thank you!
Quasar is offline   Reply With Quote
Old 12-04-2007, 07:25 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,630
All other things being equal, splitting the source code into several files won't make a bean of difference.

The final executable has no knowledge of the structure of the source code which created it.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 12-04-2007, 07:28 AM   #3
Registered User
 
Join Date: May 2004
Posts: 19
That's why i think it's a gcc problem, if i move the function to it's original place everything works!

I was wondering if there something lika a -mutex-across-multiple-source-files i should pass to gcc.
Quasar is offline   Reply With Quote
Old 12-04-2007, 07:38 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
No, it's not gcc's fault - or anyone elses. You are passing a COPY of the mutex to "function" - this mutex is NOT the same one that you were using in the other code.

It's not because you are using multiple files, but simply that you are copying an existing mutex, and making it a different mutex.

Code:
void function(pthread_mutex_t *mut) {
    pthread_mutex_lock(mut);
    printf("This shows up if function is in the same source file as the calling code.");
    pthread_mutex_unlock(mut);
}
...
function(&mut);
should work.


--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 12-04-2007, 07:53 AM   #5
Registered User
 
Join Date: May 2004
Posts: 19
It's more complicated that that. I simplified the example...

I'm passing this structure:

Code:
typedef struct {        /* Country mutex structure */
        pthread_mutex_t resA, resE, resP, resM;
        pthread_mutex_t purchase1, purchase2;
} CountryMutex;
Into this function:
Code:
 
void findDeficit(const Config *config, const CountryStruct *country, CountryDeficit *cdeficit, CountryMutex *cmutex) {
pthread_mutex_lock(&cmutex->resA);
...
pthread_mutex_unlock(&cmutex->resA);
}
Using this call:
Code:
CountryMutex cmutex;
pthread_mutex_init(&cmutex.resA, NULL);
pthread_mutex_init(&cmutex.resE, NULL);
pthread_mutex_init(&cmutex.resP, NULL);
pthread_mutex_init(&cmutex.resM, NULL);

findDeficit(config, country, cdeficit, &cmutex);   << EDIT
Shouldn't this work? Passing a pointer to a structure of mutexes?

Last edited by Quasar; 12-04-2007 at 07:58 AM. Reason: Wrong code
Quasar is offline   Reply With Quote
Old 12-04-2007, 07:55 AM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Your calling code doesn't look like it's passing pointers, so I'm not sure if it's at all working - but yes, passing a structure pointer that contains mutexes should work. The point is that you can't make copies of a mutex and still think that it's the same mutex.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 12-04-2007, 07:59 AM   #7
Registered User
 
Join Date: May 2004
Posts: 19
Sorry, missing &.

Still the same problem, argh...

Rebooting the server, maybe that works.
Quasar is offline   Reply With Quote
Old 12-04-2007, 08:25 AM   #8
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by Quasar View Post
Sorry, missing &.

Still the same problem, argh...

Rebooting the server, maybe that works.
Rebooting the server doesn't sound like the right thing - mutexes should be "infinite" resources [as in, they are only limited by amount of memory, so if you can create the mutex itself, you can use the mutex].

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple source files in one project mintsmike C++ Programming 8 06-27-2009 07:20 AM
Confusion on header and source files dnguyen1022 C++ Programming 4 01-17-2009 03:42 AM
Working with multiple source files abh!shek C Programming 17 06-03-2008 11:23 AM
Multiple Source Files!?!? Padawan C Programming 14 04-04-2004 12:19 AM
Using multiple source files: Multiple Declarations & Wrong line numbers Inquirer C++ Programming 1 05-01-2003 02:52 PM


All times are GMT -6. The time now is 09:54 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22