Solved

How can get the thread ids

Posted on 2000-02-25
7
1,668 Views
Last Modified: 2013-12-26
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_attr_default,do_loop,&a);
      
       pthread_create(&p_thread2,pthread_attr_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_self());

                         // here i want to cancel the specific thread

      //      pthread_cancel(p_thread1.pthread_id);
            sleep(20);
      }


      return 0;

}

[/ccode]

0
Comment
Question by:adinarayanak
  • 4
  • 3
7 Comments
 

Author Comment

by:adinarayanak
ID: 2558710
if  I put the sleep(20) before  second printf in any one of the do_loops then I am getting two different ids.but if i execute the prigram as it is given in the code,I am getting the same thread id for both.Why it is happening.
0
 
LVL 2

Accepted Solution

by:
rdelfino earned 10 total points
ID: 2559803

1st - pthread_cancel() is enough to cancel (kill) a thread wich has its cancel
        state as PTHREAD_CANCEL_ENABLE (default state)

2nd - If you cancel the main thread, your process wil die (and all threads
         within it)

3rd - pthread_self() returns the current thread ID

4th - the program below creates 2 threads besides the main thread, and
        cancels the first thread created


-------------------8<----------------

#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);
      }

  }

}


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[])
{

  pthread_t p_thread1;
  pthread_t p_thread2;
  int a=100;
  int b=111;
  int j;

                        pthread_create(&p_thread1,pthread_attr_default,do_loop,&a);

                        pthread_create(&p_thread2,pthread_attr_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 == 100);
    {
       // here you kill (cancel) the first thread you created from the
       // parent thread
       pthread_cancel(p_thread1);
       
       sleep(20);
       return 0;
    }

  }
  return 0;
}
0
 

Author Comment

by:adinarayanak
ID: 2564477
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_attr_default,do_loop,&a);

                                         pthread_create(&p_thread2,pthread_attr_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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:rdelfino
ID: 2564819
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;
  }
0
 

Author Comment

by:adinarayanak
ID: 2564965
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.
0
 
LVL 2

Expert Comment

by:rdelfino
ID: 2564997

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);

0
 

Author Comment

by:adinarayanak
ID: 2565211
But it is giving the same id for both the threads.

Why it is so.

awaiting u'r reply.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 87
xyBalance chalenge 58 88
Separate files macro - Errors on larger data set 18 56
Line meaning 9 77
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now