adinarayanak
asked on
How can get the thread ids
hi,
In my unix program I am creating a series of threads inwhich i am executing different functions on creation of new thread.I want to have the track of all these thread ids of the individual threads.I should able to stop any one of the thread and all the remaining threads should be running.
here i am sending my code what i have done. In that I want to stop first thread and the second thread shold be running as usual.Please help me
[ccode]
#include<pthread.h>
#include<errno.h>
#include<stdio.h>
void do_loop(int *data)
{
int i;
int j;
printf("I am in do_loop\n");
for (i = 0;i<10;i++)
{
for(j=0;j<500000;j++)
{
printf("%d got\n ",*data);
printf("\nMy thread id %d\n",pthread_self());
sleep(20);
}
}
//pthread_exit(NULL);
}
void do_loop1(int *data)
{
int i;
int j;
printf("I am in do_loop1\n");
for (i = 0;i<10;i++)
{
for(j=0;j<500000;j++)
{
printf("%d got\n ",*data); printf("\nMy thread id %d\n",pthread_self());
sleep(20);
}
}
}
int main(int argc,char *argv[])
{
// int thread_id;
pthread_t p_thread1;
pthread_t p_thread2;
int a=100;
int b=111;
int j;
pthread_create(&p_thread1, pthread_at tr_default ,do_loop,& a);
pthread_create(&p_thread2, pthread_at tr_default ,do_loop1, &b);
sleep(10);
for(j=0;j<500000;j++)
{
printf("I am in parent loop\n");
printf("count j = %d",j);
if(j==4)
pthread_cancel(pthread_sel f());
// here i want to cancel the specific thread
// pthread_cancel(p_thread1.p thread_id) ;
sleep(20);
}
return 0;
}
[/ccode]
In my unix program I am creating a series of threads inwhich i am executing different functions on creation of new thread.I want to have the track of all these thread ids of the individual threads.I should able to stop any one of the thread and all the remaining threads should be running.
here i am sending my code what i have done. In that I want to stop first thread and the second thread shold be running as usual.Please help me
[ccode]
#include<pthread.h>
#include<errno.h>
#include<stdio.h>
void do_loop(int *data)
{
int i;
int j;
printf("I am in do_loop\n");
for (i = 0;i<10;i++)
{
for(j=0;j<500000;j++)
{
printf("%d got\n ",*data);
printf("\nMy thread id %d\n",pthread_self());
sleep(20);
}
}
//pthread_exit(NULL);
}
void do_loop1(int *data)
{
int i;
int j;
printf("I am in do_loop1\n");
for (i = 0;i<10;i++)
{
for(j=0;j<500000;j++)
{
printf("%d got\n ",*data); printf("\nMy thread id %d\n",pthread_self());
sleep(20);
}
}
}
int main(int argc,char *argv[])
{
// int thread_id;
pthread_t p_thread1;
pthread_t p_thread2;
int a=100;
int b=111;
int j;
pthread_create(&p_thread1,
pthread_create(&p_thread2,
sleep(10);
for(j=0;j<500000;j++)
{
printf("I am in parent loop\n");
printf("count j = %d",j);
if(j==4)
pthread_cancel(pthread_sel
// here i want to cancel the specific thread
// pthread_cancel(p_thread1.p
sleep(20);
}
return 0;
}
[/ccode]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
if i modify the same program as
#include<pthread.h>
#include<errno.h>
#include<stdio.h>
void do_loop(int *data)
{
int i;
int j;
printf("I am in do_loop\n");
for (i = 0;i<10;i++)
{
for(j=0;j<500000;j++)
{
printf("%d got\n ",*data);
printf("\nMy thread id %d\n",pthread_self());
sleep(20);
}
}
}
int main(int argc,char *argv[])
{
pthread_t p_thread1;
pthread_t p_thread2;
int a=100;
int b=111;
int j;
pthread_create(&p_thread1, pthread_at tr_default ,do_loop,& a);
pthread_create(&p_thread2, pthread_at tr_default ,do_loop,& b);
sleep(10);
for(j=0;j<500000;j++)
{
printf("I am in parent loop\n");
printf("count j = %d",j);
if (j == 100);
{
// here you kill (cancel) the first thread you created from the
// parent thread
pthread_cancel(p_thread1);
sleep(20);
return 0;
}
}
return 0;
}
if i run this when the count j == 100 the program will be stopped.
I want one thread to be continuously executing and one thread to be terminated.
please do it
#include<pthread.h>
#include<errno.h>
#include<stdio.h>
void do_loop(int *data)
{
int i;
int j;
printf("I am in do_loop\n");
for (i = 0;i<10;i++)
{
for(j=0;j<500000;j++)
{
printf("%d got\n ",*data);
printf("\nMy thread id %d\n",pthread_self());
sleep(20);
}
}
}
int main(int argc,char *argv[])
{
pthread_t p_thread1;
pthread_t p_thread2;
int a=100;
int b=111;
int j;
pthread_create(&p_thread1,
pthread_create(&p_thread2,
sleep(10);
for(j=0;j<500000;j++)
{
printf("I am in parent loop\n");
printf("count j = %d",j);
if (j == 100);
{
// here you kill (cancel) the first thread you created from the
// parent thread
pthread_cancel(p_thread1);
sleep(20);
return 0;
}
}
return 0;
}
if i run this when the count j == 100 the program will be stopped.
I want one thread to be continuously executing and one thread to be terminated.
please do it
When the main thread finishes, the process die (and all threads within it).
All you gotta do is change the following lines
if (j == 100);
{
// here you kill (cancel) the first thread you created from the
// parent thread
pthread_cancel(p_thread1);
sleep(20);
return 0;
}
to these ones below
if (j == 100);
{
// here you kill (cancel) the first thread you created from the
// parent thread
pthread_cancel(p_thread1);
sleep(20);
//return 0;
}
All you gotta do is change the following lines
if (j == 100);
{
// here you kill (cancel) the first thread you created from the
// parent thread
pthread_cancel(p_thread1);
sleep(20);
return 0;
}
to these ones below
if (j == 100);
{
// here you kill (cancel) the first thread you created from the
// parent thread
pthread_cancel(p_thread1);
sleep(20);
//return 0;
}
ASKER
It is fine.Can I display any one of the childrens' thread_id in the main loop.
If so how to do it.
waiting for u'r reply.
regards,
AdinarayanaK.
If so how to do it.
waiting for u'r reply.
regards,
AdinarayanaK.
in the main thread, do the following after you created the 2 threads:
printf("thread1 id = %u\n", p_thread1);
printf("thread2 id = %u\n", p_thread2);
ASKER
But it is giving the same id for both the threads.
Why it is so.
awaiting u'r reply.
Why it is so.
awaiting u'r reply.
ASKER