Sponsored Content
Top Forums Programming help me out with my threaded c++ mudbase - c++, pthread_cond_wait Post 302133644 by sonicx on Friday 24th of August 2007 11:30:21 AM
Old 08-24-2007
help me out with my threaded c++ mudbase - c++, pthread_cond_wait

hello,
in my free time i am writing on a c++ mud codebase, a while ago i decided that i would move to pthreads, so i could make use of smp. now i have a problem which i just cant fix - for weeks now. i have a main thread which spawns my threads, as soon as spawned they get a pthread_cond_wait, so all new threads are sleeping, until i signal them to wake and to my bidding - what is what they wont do. they just wont receive my pthread_cond_signal.
the important part of my code is this i guess:
Code:
void Thread::signal(int signal){
	int r;
	msg("Thread::signal(): '%s' received signal.\r\n", name->c_str()); 
	switch(signal)
	{
		case S_GO:
			msg("Thread::signal(): '%s' signal: S_GO\r\n", name->c_str()); 
			try { 	
				pthread_mutex_lock( &this->condition_mutex );
			}
			catch ( exception& e ) { 
				msg("EXCEPTION: Thread::signal(): '%s'.\r\n", e.what()); 
			}
			msg("Thread::signal(): '%s' is locked\r\n", name->c_str()); 
			r = pthread_cond_signal( &this->condition ); 
			msg("Thread::signal(%i): signal returns: %i\r\n", &this->condition ,r); 
			pthread_mutex_unlock( &this->condition_mutex );
			msg("Thread::signal(): '%s' is unlocked\r\n", name->c_str()); 
			break;;
		case S_PAUSE:
		break;;
		case S_DIE:
		break;;
		default:
			msg("Thread::signal(): Undefined signal '%i'.", signal);			
	};
	msg("Thread::signal(): Signal '%i' was processed.\r\n", signal); 
}

void Thread::do_loop(){
	bool loop = true,pause = true;
	int tic = itime();
	struct timespec next;        /* timeout value for the timed_wait function */
	msg("Thread::do_loop(): '%s' called me, and here i am.\r\n", name->c_str()); 
	if(name->compare("initHelper") == 0) pause = false;
	while( loop == true ){
		//if(tic == itime()) {
			msg("Thread::do_loop(): '%s' tries to lock its condition.\r\n", name->c_str()); 
			try { 	pthread_mutex_lock( &condition_mutex ); }
			catch ( exception& e ) { msg("Thread::do_loop(1): '%s'.\r\n", e.what()); }
			msg("Thread::do_loop(): '%s' locked its condition and tries to go to sleep.\r\n", name->c_str()); 
			try {
				if( pause == true ) {
					if( state == TS_NEW ){
						state = TS_READY;
						msg("NOTE: Thread '%s' is ready now.\r\n",name->c_str());
					}
					msg("Thread::do_loop(%d): '%s' id %i goes to sleep.\r\n", &this->condition,name->c_str(),this->id); 
					
					pthread_cond_wait( &(this->condition), &condition_mutex ); 
					msg("Thread::do_loop(): '%s' just awoke.\r\n", name->c_str()); 
					pause = false;
				} else {
					// Have a certain wait time ( half a second to be specific )
					next.tv_sec = itime();
					next.tv_nsec = wait;
					pthread_cond_timedwait( &condition, &condition_mutex, &next ); 
					msg("Thread::do_loop(): '%s' just awoke.\r\n", name->c_str()); 
				}
			}
			catch ( exception& e ) { 
				msg("Thread::do_loop(2): '%s'.\r\n", e.what()); 
			}
			
			//if( state == TS_READY ) state = TS_RUNNING;
			pthread_mutex_unlock( &condition_mutex );
			
			msg("Thread::do_loop(): Calling function for '%s'.\r\n", name->c_str()); 
			(*pfunction)(NULL);
			msg("Thread::do_loop(): Function for '%s' finished.\r\n", name->c_str());
			if(name->compare("initHelper") == 0) return;
			
			msg("Thread::do_loop(): Stop loop.\r\n", name->c_str());
			loop = false;
	}
	
}

you can take a look at the whole code here.

as you can see this is a more or less object oriented attempt. i think that maybe the pointer this->condition is different,when cond_wait uses it, and when cond_signal uses it. please help me out here, i am happy to provide every info and try every idea.
thanks for your attention,
sonicx
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Threaded Discussions for Webpages

Dear All, I run a website for a non-profit. Does anyone know where I can get free or cheap software to run threaded discussions for our website? Our website is obviously running off a unix platform. Thanks (4 Replies)
Discussion started by: evertk
4 Replies

2. Programming

Threaded 'find' utility

I need to modify my version of find in unix and get it to create and use two POSIX threads to carry out concurrent finding operations. How do i get about doing this>? If anyone could help me it would be much appreciated. Thanx Mariuca (1 Reply)
Discussion started by: mariuca
1 Replies

3. Programming

threaded merge sort help

I am working on a merge sort of two files of integers, and am fuzzy on some of the logic\syntax. I need two threads, each of which will open a file, read its contents into an array, and then sort the array using qsort. One thread will operate on file f1.dat(10000 numbers) and leave its sorted... (0 Replies)
Discussion started by: AusTex
0 Replies

4. AIX

multi threaded program is hanging

I have a Multithreaded program which is hanging on AIX. OS Version: AIX 5.2 and thread library version : 5.2.0.75 We Initiate the process with 50 threads..when we are disconnecting from the process it hangs.There is lots of other stuff involved here.I am just sending the piece of the problem with... (0 Replies)
Discussion started by: hikrishn
0 Replies

5. Programming

Conditional wait using pthread_cond_wait() details

Please tell me about internal functionality of pthread_cond_wait(). How it works. Whether it actually put the thread into sleep and do the context switch or use spin locking. (1 Reply)
Discussion started by: mansoorulhaq
1 Replies

6. Shell Programming and Scripting

In need of multi threaded perl assistance

I need to write a perl script to execute external programs and grab the output and return code. Each program should be killed if it has not completed within X seconds. Imagine that the script goes something like this : @commands = &get_commands(); foreach $cmd (@commands) { $pid =... (4 Replies)
Discussion started by: SandmanCL
4 Replies

7. UNIX for Advanced & Expert Users

Multi-threaded encryption @ Fedora 11

Hello, are any of the encryption programs capable of true multi-threading ? Friend of mine tells me that he's been running some testing on Fedora 11 and that the kernel doesn't support multi-threading at that level. I've been looking into TrueCrypt, encfs and both calm to support... (0 Replies)
Discussion started by: TehOne
0 Replies

8. Linux

Multi-threaded encryption @ Fedora 11

Hello, are any of the encryption programs capable of true multi-threading ? Friend of mine tells me that he's been running some testing on Fedora 11 and that the kernel doesn't support multi-threading at that level. I've been looking into TrueCrypt, encfs and both calm to support... (1 Reply)
Discussion started by: TehOne
1 Replies

9. Programming

multi-threaded memory leak

Hello All : I write a .c program to test the exactually resource the memory leak as follows: 1 #include <stdio.h> 2 #define NUM 100000 3 void *Thread_Run(void * arg){ 4 //TODO 5 //pthread_datch(pthread_self()); 6 int socket= (int)arg; 7 ... (1 Reply)
Discussion started by: aobai
1 Replies

10. Shell Programming and Scripting

Need threaded python script

I have a single threaded bash script that I am using to create secgroup rules in openstack. The process to add the rules is taking forever. Any of you python gurus know how to convert this bash script into a thread python script? Thanks in advanced. create-secgroup-rules.sh: #!/bin/bash cat ... (2 Replies)
Discussion started by: stovie1000
2 Replies
All times are GMT -4. The time now is 06:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy