ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215
Rep:
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.
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.
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215
Original Poster
Rep:
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?
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?
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215
Original Poster
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.