Hi,

How to set the name for thread in linux using c, c++.

What do you mean by setting name of the thread? Why you need a name?
In Posix you can refer to a thread by it's thread id.

pthread_create creates a thread and returns the handler of the thread which you can save and use.

#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0; t<NUM_THREADS; t++){
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

and also gettid returns you the current thread id.

Can you explain little bit more why you need to set a thread name, then i will be able to give you more proper solution

BTW its a C code solution which I just copied from cloud, you should write a proper cpp code to full fill the RAII idiom

Edited 5 Years Ago by alwaysLearning0: n/a

Thanks for replies,

I'm writing a C++ mulithread app. If any one of thread crashed need to find the exact thread with unique name. so that I can keep track of them during debugging. I difficult to find the exact thread with thread id.

can u suggest any other way to find which thread was crashed both windows and linux platform.

If a thread is crashed then the program is also crashed, compile your program with -g option so that when it crashes it will generate the core file.

And then you can debug core file to find out exactly where it crashed.

Use gdb to debug. Following link has some helpful commands to debug the threads.
http://www.delorie.com/gnu/docs/gdb/gdb_25.html

I am not a windows expert, so I hope somebody else can answer your question on windows.

Thanks,

I got the core file during crash but gdb failed to open the core file with error "core.5047 is not a core dump: File format not recognized".

My mulithread application was hung randomly. I don't know which thread and where it hung. How to get all running thread current status(current call stack).

OR any other way to find the application hung.

What is the core file size?
May be core file is getting discarded.
Try to set "ulimit -c unlimited" which will allow you to generate full core dump.
then use command: file coredumpfile, which will show you which program actually generated the core, just to make sure you are debugging the right core.

Then use the following command:
gdb -c coredumpfile ./YourProgram

then give command: "info threads" it will show you all the threads, usually the last thread is the one which crashed.
You can then debug individual thread by selecting the thread number you got in the last command
"thread 1" which will take you to that thread
or you can give command: "thread apply all bt" to see the back trace of all the threads.
or "thread apply threadno bt" to see the back trace of the last thread.

in the bt, you can see exactly where it crashed.

In the multithread programs, usually program crash because of invalid memory access, use share_pointer and locking as much as possible to control memory access.
There are some performance bottleneck for using those, but at least your program will not crash.

there is other tool too, valgrind is a very good tool to check the memory leaks and invalid read.

http://valgrind.org/docs/manual/mc-manual.html

you can paste here your core dump, I can have a look if you like.

Edited 5 Years Ago by alwaysLearning0: n/a

great thanks...

How to get all running thread current status(current call stack) in windows platform.

This article has been dead for over six months. Start a new discussion instead.