Basic Threading

Mar 14, 2012 at 9:49am
I have a basic program that takes some video files, creates a playlist, and then opens the playlist in Totem (video player).

Now, this works just fine but my program itself will not end until the Totem program is closed, which isn't exactly what I want. I want my program to do its things (create playlist and launch it) and then it should close down right after the launching of Totem.

As far as I understand, I need to create a new thread and launch totem in that thread, allowing my main thread to complete and end.

Is this true? If so, how do I go about it? My relevant code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  .........
  //save the playlist to a file
  playlistFile.open("/tmp/shows-playlist.pls");
  playlistFile << playlist;
  playlistFile.close();

  //launch totem!
  system("totem /tmp/shows-playlist.pls");
  
  return 0;
}
Mar 14, 2012 at 10:12am
If your using windows, you go with the _beginthreadex(); function.
http://msdn.microsoft.com/en-us/library/kdzttdcb(v=vs.80).aspx

Some thinglike this (written on the fly)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void startTotem(void* pData)
{
     system("totem /tmp/shows-playlist.pls");
    _endthread();
}


int main(void)
{
    .........
  //save the playlist to a file
  playlistFile.open("/tmp/shows-playlist.pls");
  playlistFile << playlist;
  playlistFile.close();

  _beginthreadex(NULL,0,startTotem,NULL,0,NULL);
  return 0;
}



but im not sure if you realy need to start the program via a new thread..
Mar 14, 2012 at 10:26am
As far as I understand, I need to create a new thread and launch totem in that thread, allowing my main thread to complete and end.
Certainly not. If your main() ends the thread will end as well.

You can start another program independently with CreateProcess(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx

Or ShellExecute(): http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx
Mar 14, 2012 at 11:56am
Should have mentioned it before but I am programming for Linux.

coder777: it doesn't seam like my main thread is ending though after the system() call. I have a class that outputs a message that it cleaned up after the program.

The end of the program should read out "ssh connection cleaned up" but this does not come up until after I close Totem (the program launched by the system call) so it shows that the main() does not end until the system call is finished.

How can I get around this in a Linux way?
Mar 14, 2012 at 12:04pm
Under linux it's even easier using exec(): http://linux.die.net/man/3/exec

seems that system indeed wait for the process to return.
Mar 14, 2012 at 12:34pm
It seams that execv() is the way to go (as far as I can tell) but I can't seam to figure out how to pass the command using it.

Can someone give me a quick example?
Mar 14, 2012 at 1:17pm
first argument should be the full path to the program, second argument is an array of cstring arguments (much like the argv element of a C/C++ app that takes arguments)
Mar 14, 2012 at 3:06pm
I am not sure how to create that cstring array. I have tried:

1
2
const char *args[] = {"/tmp/shows-playlist.pls", (char *) 0 };
execv("totem", args);

but this gives me a compilation error:
create-playlist.cpp: In function ‘int main()’:
create-playlist.cpp:26:23: error: invalid conversion from ‘const char**’ to ‘char* const*’ [-fpermissive]
/usr/include/unistd.h:560:12: error: initializing argument 2 of ‘int execv(const char*, char* const*)’ [-fpermissive]
Mar 14, 2012 at 3:11pm
execl is more convenient:

execl("totem", "totem", "/tmp/shows-playlist.pls", NULL); // Note that the first argument is supposed to be the filename
Mar 14, 2012 at 4:21pm
Thanks, it seams to be working now!
Topic archived. No new replies allowed.