LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2008, 09:12 AM   #1
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Rep: Reputation: 16
Thread question


Hi I'm new to thread programming so please be gentle

I have a main function which has a while(1) loop which always runs. I also want to create another thread which also has a while(1) loop which will run constantly like so:

Code:
int main (int argc, char **argv)
{
        pthread_t thread1;     
	
	char *message1 = "This is Thread 1";
	pthread_create( &thread,
                NULL,print_message_function,(void*) message1);
	
	pthread_join( thread1, NULL);
	
	while(1)
	{
		printf("This is the MAIN Thread\n");
                //so some stuff	
	}
}
void *print_message_function( void *ptr )
{     
	while(1)
	{
		char *message;     
		message = (char *) ptr;     
		printf("%s \n", message);
	}
}
The problem I'm having is that the thread which I start is always runing and control is never passed back to the main loop The "This is the MAIN Thread" never gets printed. I thought the two loops would share time equally. What do I have to do in order to get this to happen.
 
Old 06-20-2008, 09:22 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

The problem is the line:

pthread_join( thread1, NULL);

It means it will wait at this line until the other thread (thread1) returns. And since the function print_message_function never returns, it will wait forever (or till you terminate the application). Try removing the pthread_join.
 
Old 06-20-2008, 09:41 AM   #3
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Original Poster
Rep: Reputation: 16
Yes this now works, thank you
Another question, in my main loop I detect if certain keys are pressed on the keyboard but it does not detect them straight away if it is busy processing the thread1. How can I design my program so that the main loop executes once only then the thread1 executes once only then back to main and so on?

Thanks
 
Old 06-20-2008, 09:53 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hmm. I don't really understand the last part. The point of threads is to execute many things at the same time. To me it seems you don't want threads after all? If so, why not skip all the thread stuff, and call print_message_function in the main loop?
 
Old 06-20-2008, 10:04 AM   #5
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Original Poster
Rep: Reputation: 16
Ok the code outlined above is a simplified version of what I want to do, the thread which I start will use directfb to draw various stuff to screen and the main() while loop is actually a dbus loop which is constantly waiting for dbus events.

So in one thread I am constantly drawing to screen and the other (main) thread is listening for dbus events. The dbus events will determine what is drawn to screen.

I hope you get what I am trying to achieve and I appreciate any help u can offer.

Thanks
 
Old 06-20-2008, 10:43 AM   #6
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by HarryBoy View Post
How can I design my program so that the main loop executes once only then the thread1 executes once only then back to main and so on?
Use a pthread_mutex_t. Read the man pages for pthread_mutex_lock.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about join a thread George2 Programming 4 08-22-2005 07:58 AM
perl thread question kevman Programming 2 08-04-2005 12:44 AM
Question About Thread sibtay Programming 1 08-26-2004 02:59 PM
A Java Thread question megaspaz Programming 0 08-02-2003 01:33 PM
Question about thread icons moses LQ Suggestions & Feedback 8 11-30-2002 12:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration