|
Home > Archive > Unix Programming > April 2004 > Thread
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Doug Ly 2004-04-23, 12:34 pm |
| Hi,
I like to have a thread in my main program to display a value continuously.
I use the function
void* display(void* arg)
{
while (1) {
cout << value;
}
}
as the argument to pass to the thread
and I declare
pthread_t tid;
int arg;
pthread_create(&tid, NULL, display,(void *) arg);
pthread_join(tid, (void **)NULL);
in my main();
But the thread just keep running forever. Is it supposed to be terminated
whenever the main() returns?
Thanks
| |
| Fletcher Glenn 2004-04-23, 2:34 pm |
|
Doug Ly wrote:
> Hi,
> I like to have a thread in my main program to display a value continuously.
> I use the function
> void* display(void* arg)
> {
> while (1) {
> cout << value;
> }
> }
>
> as the argument to pass to the thread
> and I declare
>
> pthread_t tid;
> int arg;
> pthread_create(&tid, NULL, display,(void *) arg);
>
> pthread_join(tid, (void **)NULL);
>
> in my main();
>
> But the thread just keep running forever. Is it supposed to be terminated
> whenever the main() returns?
> Thanks
>
>
>
>
Your thread will fill up your stdout buffer with a gazillion messages
generated at full processor speed. Even after main returns, your system
will be busy emptying the backlog of messages. Try putting is some
delay between outputs.
--
Fletcher Glenn
| |
| DeMarcus 2004-04-23, 2:34 pm |
|
Doug Ly wrote:
> Hi,
> I like to have a thread in my main program to display a value continuously.
> I use the function
> void* display(void* arg)
> {
> while (1) {
> cout << value;
> }
> }
>
> as the argument to pass to the thread
> and I declare
>
> pthread_t tid;
> int arg;
> pthread_create(&tid, NULL, display,(void *) arg);
>
> pthread_join(tid, (void **)NULL);
>
> in my main();
>
> But the thread just keep running forever. Is it supposed to be terminated
> whenever the main() returns?
> Thanks
>
Your main() doesn't return. It blocks on pthread_join() until the thread
finish, which it never will.
By the way. It feels scary to pass an int like that. I would rather
concider (void*)&arg
| |
| Doug Ly 2004-04-23, 2:34 pm |
| So what is the correct way to have a thread running on the main() background
and terminate itself when main() exit?
I just want to display the value of a variable continuously.
Thanks
| |
| DeMarcus 2004-04-23, 4:33 pm |
|
Doug Ly wrote:
> So what is the correct way to have a thread running on the main() background
> and terminate itself when main() exit?
> I just want to display the value of a variable continuously.
> Thanks
>
>
The thread will terminate when the main thread terminates. If you want
to do a clean termination of the thread you could do something like
this.
void* myThread( void* arg )
{
while( 1 )
{
cout << value;
if( terminate )
break;
}
}
bool terminate = false;
int main( void )
{
pthread_create( ... );
terminate = true;
pthread_join( ... );
return 0;
}
| |
| bob holder 2004-04-23, 6:35 pm |
|
"Doug Ly" <dmly@sbcglobal.net> wrote in message
news:bldic.207$Nj.30@newssvr24.news.prodigy.com...
> So what is the correct way to have a thread running on the main()
background
> and terminate itself when main() exit?
> I just want to display the value of a variable continuously.
> Thanks
>
If you remove the pthread_join as the other poster suggested your thread
will "evaporate" when the main exits.
bob
| |
| The King of Pots and Pans 2004-04-24, 12:34 am |
| On Fri, 23 Apr 2004 at 16:12 GMT, Doug Ly spoke:
> But the thread just keep running forever. Is it supposed to be
> terminated whenever the main() returns?
This is what I do. You need the mutex stuff especially if running on
an SMP system (i.e. multiple CPU's).
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
pthread_mutex_t killLock;
int killEvent = 0;
void *mythread(void *data)
{
while(1)
{
// do someting continuously here
// check for kill event
pthread_mutex_lock(&killLock);
if(killEvent)
break;
pthread_mutex_unlock(&killLock);
}
pthread_mutex_unlock(&killLock);
return (void *)0;
}
int main()
{
pthread_t tid;
pthread_mutex_init(&killLock, NULL);
pthread_create(&tid, NULL, &mythread, NULL);
// go off and do stuff (simulate by sleeping)
sleep(5);
// when we are done and want our thread to die do this:
pthread_mutex_lock(&killLock);
killEvent = 1;
pthread_mutex_unlock(&killLock);
// wait for thread to get signal and die
pthread_join(tid, NULL);
// cleanup
pthread_mutex_destroy(&killLock);
return EXIT_SUCCESS;
}
--
The King of Pots and Pans
|
|
|
|
|