LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 01-12-2009, 05:26 AM   #1
Jurrian
Member
 
Registered: Sep 2008
Posts: 58

Rep: Reputation: 15
thread c


Hi all,

I have an application that has a simple command lined interface, which gets command and executes them. Since I try to create a media application, I use the gstreamer framework and I want my interface back after I execute a program.

First I created the interface, the gstreamer framework and combined them. Everything worked perfectly, except that I only get my interface back after a complete run of a file. Here is what I do in short:

Code:
while(quit != 0){
if(strcmp(command, "play")==0)
		{
			scanf("%s", file);
			printf("inputfile: %s\n", file);
			pthread_create( &thread1, NULL, playfile(file), NULL);
                        pthread_join( thread1, NULL);
}
}
The file is played, but after that I get back in my shell, so somehow my thread broke out the while loop...

if I instead use:

Code:
while(quit != 0){
if(strcmp(command, "play")==0)
		{
			scanf("%s", file);
			printf("inputfile: %s\n", file);
			playfile(file);
}
}
I do get back on while loop.

Anyone has a suggestion to help me out?

Thanx in advance,

Jurrian
 
Old 01-12-2009, 11:25 AM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
"pthread_join (one_thread)" means "Wait until the thread one_thread is finished. Another thing I'm not sure that's clear for you is that when a process ends, it kills all its thread.
So the design of your program will depend on what you want. If you really want to start the play and directly come back to the shell, I think you'd better make simple program (without thread nor fork) and run it with a "&" at the end of your command line, thus the shell will start it as a background job. You can do that into your program with the "fork" function, I don't know if it's really the same thing, but it really look the same, but then your force the "background job" policy, I think it's better that the program user have the choice to start it with or without "&".
 
Old 01-13-2009, 12:20 AM   #3
Jurrian
Member
 
Registered: Sep 2008
Posts: 58

Original Poster
Rep: Reputation: 15
I think you misunderstand me a little. My program has an own command line interface, and a & behind the start of my program will give me back the terminal command line.

Anyone has another suggestion?
 
Old 01-13-2009, 12:37 AM   #4
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
Quote:
Originally Posted by Jurrian View Post
I think you misunderstand me a little
to me it seems not.

what ever the reply in the second post, it is quite ok.
 
Old 01-13-2009, 12:40 AM   #5
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
Quote:
Originally Posted by Jurrian View Post
pthread_join( thread1, NULL);
try comment this line and execute the code. check if it is doing what u want.
 
Old 01-13-2009, 06:38 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Try to write it out like this:
Code:
1. start program
2. start thread          --> 1. top of loop
3. wait for thread (join)    2. do stuff, maybe break loop
4. exit program              3. go to 1
Modify that with what you expect to happen (and maybe also what you think is happening) so we have a better idea of what's going on.
ta0kira
 
Old 01-14-2009, 12:29 AM   #7
Jurrian
Member
 
Registered: Sep 2008
Posts: 58

Original Poster
Rep: Reputation: 15
This is what I want it to do:
Code:
1. start program
2. start loop
3a. start thread          --> 1. top of loop
3b execute other command
4 if other command is not exit, goto 1
5 exit program
I took out the wait to join, and put that on the end, so my program will first terminate the thread and then finish.
 
Old 01-14-2009, 05:14 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Do you terminate with pthread_cancel? If so, that doesn't work well with pthread_join except under certain conditions (I'll elaborate if that's the case.) Also, what does the thread do? You didn't specify. Does the main thread continually execute a command in 3b because of looping to 1, or is that jumping to the 1 in the thread? Are you just trying to wait until the command exits? By command, do you mean a new process, or the execution of a function?
ta0kira
 
Old 01-15-2009, 12:23 AM   #9
Jurrian
Member
 
Registered: Sep 2008
Posts: 58

Original Poster
Rep: Reputation: 15
Code:
1. start program
2. start loop
3a. start thread and start playing a media file  --> 1. top of loop
3b execute other command (like pause, mute, help, exit)
4 if other command is not exit, goto 1
5 exit program
After 3a, the program needs to go straight to 2, else a mute command or pause command will be pretty useless.

I am not using pthread_cancel, nor using p_thread_exit().

I found a mistake myself, the call to p_thread_create should be:

Code:
pthread_create( &thread1, NULL, (void *) playfile, file);
 
Old 01-15-2009, 02:30 AM   #10
Jurrian
Member
 
Registered: Sep 2008
Posts: 58

Original Poster
Rep: Reputation: 15
fixed it, thanx all for help
 
  


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
python thread safety: printing from thread to redirected stdout - safe? BrianK Programming 2 10-11-2010 11:28 AM
the return value of getpid() called from main thread and new thread r identical !!! cryincold Programming 3 02-29-2008 01:37 AM
[thread control suggestion] add a "solved" button that the thread starter can click atom LQ Suggestions & Feedback 3 03-24-2005 11:55 AM
Main thread sending notification to child thread rajesh_b Programming 1 09-22-2004 09:15 AM
configure qt thread issue (just compiled qt w/ -thread option) cleff Linux - Software 8 05-07-2004 11:11 PM

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

All times are GMT -5. The time now is 12:15 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