Changing Name of a thread

tcak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 9 09:30:47 PST 2016


On Saturday, 9 January 2016 at 11:02:53 UTC, Keywan Ghadami wrote:
> Hello, i am trying to the set the name of thread with:
>
>     import core.thread;
>     auto thisThread = Thread.getThis();
>     thisThread.name = "kiwi";
>
> but GDB prints the name of the programm ("helloworld")
>
>     [Thread debugging using libthread_db enabled]
>     Using host libthread_db library 
> "/lib/x86_64-linux-gnu/libthread_db.so.1".
>     Edit source/app.d to start your project.
>     ^C
>     Program received signal SIGINT, Interrupt.
>     D main () at source/app.d:17
>     17		}
>     (gdb) info threads
>       Id   Target Id         Frame
>     * 1    Thread 0x7ffff7fd0800 (LWP 3232) "helloworld" D main 
> () at source/app.d:17
>
> Next thing i tried was calling pthread_setname_np (Linux) but 
> it seams that is not defined in phobos, so i tried to delcare 
> it by my self:
>
>     import core.sys.posix.pthread;
>     import std.string;
>     extern(C) int pthread_setname_np(pthread_t, const char*);
>     pthread_setname_np(pthread_self(), 
> toStringz("thread_name"));
>
> but this gives me
>    helloworld ~master: building configuration "application"...
>    dmd -c 
> -of.dub/build/application-debug-linux.posix-x86_64-dmd_2069-AAC406C4728034581B43AEC547EAA8E7/helloworld.o -debug -g -w -version=Have_helloworld -Isource/ source/app.d source/kiwi.d -vcolumns
>    Linking...
>    dmd 
> -of.dub/build/application-debug-linux.posix-x86_64-dmd_2069-AAC406C4728034581B43AEC547EAA8E7/helloworld .dub/build/application-debug-linux.posix-x86_64-dmd_2069-AAC406C4728034581B43AEC547EAA8E7/helloworld.o -L--no-as-needed -g
>    
> .dub/build/application-debug-linux.posix-x86_64-dmd_2069-AAC406C4728034581B43AEC547EAA8E7/helloworld.o: In function `_Dmain':
> /home/keywan/hello_world/source/app.d:13: undefined reference 
> to `_D3app4mainFZ18pthread_setname_npUmxPaZi'
>    collect2: error: ld returned 1 exit status
>    --- errorlevel 1
>    FAIL 
> .dub/build/application-debug-linux.posix-x86_64-dmd_2069-AAC406C4728034581B43AEC547EAA8E7/ helloworld executable
>    dmd failed with exit code 1.
>
> sorry for asking stupid questions but my background is more 
> PHP,Perl,Java...
> any help would be appreciated. (context i am trying to make 
> debugging easier by giving the threads names)

I tried your code with a little addition as follows:

[code]
import core.sys.posix.pthread;
import std.string;
import std.stdio;

extern(C) int pthread_setname_np(pthread_t, const char*);

void main(){
     pthread_setname_np(pthread_self(), toStringz("thread_name"));
     readln();
}
[/code]

Compiled it with "dmd blah.d", and then run "./blah".

Because there is "readln" there, I opened another terminal and
used "ps H -o 'pid tid cmd comm'".

In the output:

  PID   TID CMD                         COMMAND
5089  5089 ./blah                      thread_name

So, it works.


More information about the Digitalmars-d-learn mailing list