C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-16-2005, 09:27 AM   #1
Registered User
 
Join Date: Aug 2002
Posts: 351
pthread_mutex problem

Hi All,

Consider this [multi-threaded] code fragment...

Code:
int volatile num_threads = 0;

snip...

if(num__threads < MAX_NUM_THREADS)
{
	//mutex
	pthread_mutex_lock(&my_thread_mutex);
	//increment number of used threads
	num_threads++;
	pthread_mutex_unlock(&my_thread_mutex);
	//mutex

	printf("%d:DEBUG: num threads = %d\n",pthread_self(),num_threads);

	//do work
	if(do_work() < 0)
	{
		//mutex
		pthread_mutex_lock(&my_thread_mutex);
		//decrement number of slaves
		num_threads--;
		pthread_mutex_unlock(&my_thread_mutex);
		//mutex

		return 1;
	}

	//mutex
	pthread_mutex_lock(&my_thread_mutex);
	//decrement number of slaves
	num_threads--;
	pthread_mutex_unlock(&my_thread_mutex);
	//mutex

	return 0;
}
I'm trying to throttle the number of pthreads accessing this bit of code. The mutexes are initialised and destroyed elsewhere.

The problem is that num_threads ends up negative i..e somehow the decrement is happening more than the increment.

Can anyone see why?

Are my mutexes used correctly?

Thanks for any help,

rotis23

Last edited by rotis23; 11-16-2005 at 09:29 AM.
rotis23 is offline   Reply With Quote
Old 11-16-2005, 12:53 PM   #2
Registered User
 
Join Date: Aug 2002
Location: Hermosa Beach, CA
Posts: 446
It looks fine. Is num__threads being modified anywhere else? By the way, I recommend the following small change:

Code:
int volatile num_threads = 0;

snip...

if(num__threads < MAX_NUM_THREADS)
{
        int result;
	//mutex
	pthread_mutex_lock(&my_thread_mutex);
	//increment number of used threads
	num_threads++;
	pthread_mutex_unlock(&my_thread_mutex);
	//mutex

	printf("%d:DEBUG: num threads = %d\n",pthread_self(),num_threads);

	//do work
        result = do_work();

	//mutex
	pthread_mutex_lock(&my_thread_mutex);
	//decrement number of slaves
	num_threads--;
	pthread_mutex_unlock(&my_thread_mutex);
	//mutex

        if (result < 0)
            return 1;
        else
            return 0;
}
__________________
The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.
IfYouSaySo is offline   Reply With Quote
Old 11-17-2005, 05:42 AM   #3
Registered User
 
Join Date: Aug 2002
Posts: 351
Thanks for the input.

num__threads should be num_threads in my code fragment.

num_threads does not change anywhere else.

Was thinking of doing that change - much more readable

BTW - this is running on an *old* redhat 8.0 platform: any bugs on that platform that would cause this? <clutching at straws>
rotis23 is offline   Reply With Quote
Old 11-17-2005, 11:55 AM   #4
Registered User
 
Join Date: Aug 2002
Posts: 351
Something very weird going on here...I've seen the debug printf line output MAX_NUM_THREADS+1 !

How is that possible? Either I've implemented the mutexes wrong or they're not working properly.
rotis23 is offline   Reply With Quote
Old 11-17-2005, 05:09 PM   #5
Registered User
 
Jaqui's Avatar
 
Join Date: Feb 2005
Posts: 416
RH8 [ all distros with the 2.4 kernel actually ] had a few small problems with libpthread, that were fixed in the 2.6 kernel.

you my be hitting one of the known issues of the 2.4 kernel with pthreads.
__________________
Quote:
Originally Posted by Jeff Henager
If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.
Jaqui is offline   Reply With Quote
Old 11-21-2005, 12:51 AM   #6
Registered User
 
Join Date: Aug 2002
Location: Hermosa Beach, CA
Posts: 446
I doubt if there were issues with the mutexes in Redhat 8. Maybe other areas, but I bet the mutexes were fairly solid. Anyway, I can see why you would end up with MAX_THREADS+1. Look at the code. You are doing your conditional check on the num_threads outside of the mutex. Do you see why that is a problem? Imagine that you are sitting at num_threads = MAX_THREADS-1 currently. Then thread1 comes along. It says, num_threads less than max, I can continue. Then context switch happens, and control goes to thread2. It looks at num_threads, says num_threads is less than max, I can continue. So then each thread obtains the mutex and increments num_threads. Now you have num_threads == MAX_THREADS+1. So change the logic to something like:

Code:
    pthread_mutex_lock(&mutex);

    if (num_threads > MAX) {
        num_threads++;

        // do work
    }
    else {
        pthread_mutex_unlock(&mutex);
    }
__________________
The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.
IfYouSaySo is offline   Reply With Quote
Old 11-23-2005, 08:01 AM   #7
Registered User
 
Join Date: Aug 2002
Posts: 351
Yes thanks - I can see MAX_THREADS+1 but not num_threads < 0.
rotis23 is offline   Reply With Quote
Old 11-24-2005, 12:15 PM   #8
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,020
Is that your real code? I don't see how that code alone can generate a num_threads value less than zero.
bithub is offline   Reply With Quote
Old 11-25-2005, 09:00 AM   #9
Registered User
 
Join Date: Aug 2002
Posts: 351
I think this might have been a memory violation issue. I was attempting to access a NULL struct later in the thread and I'm guessing this could have potentially violated memory?
rotis23 is offline   Reply With Quote
Old 11-25-2005, 11:50 AM   #10
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,020
Anytime you attempt to dereference invalid data, strange things can (and usually do) happen in your program.
bithub is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help understanding a problem dnguyen1022 C++ Programming 2 04-29-2009 04:21 PM
Memory problem with Borland C 3.1 AZ1699 C Programming 16 11-16-2007 11:22 AM
Someone having same problem with Code Block? ofayto C++ Programming 1 07-12-2007 08:38 AM
A question related to strcmp meili100 C++ Programming 6 07-07-2007 02:51 PM
WS_POPUP, continuation of old problem blurrymadness Windows Programming 1 04-20-2007 06:54 PM


All times are GMT -6. The time now is 03:04 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