C++, giving thread a "name"


 
Thread Tools Search this Thread
Top Forums Programming C++, giving thread a "name"
# 1  
Old 08-20-2011
C++, giving thread a "name"

Hey guys, I'm doing a mutli-threading in a maze which I'm able to do it already. Now all thats left is to name the thread which is created.
The problem is I don't really know how.
Let's say my first thread is call "POOH".
After the threads die.
It will create another new thread called "TIGGER".
I've stored the names into an array, now I kinda confused with naming the threads.
Any help will be appreciated thx everyone!!


This is my thread function
Code:
int callThread()
{
    //create 2 thread objects
    pthread_t t1, t2;
    //create 2 string msg
    string tN1 = "A new life have been born!!!";
    string tN2 = "A second life have been born!!!";
    //create 2 thread msg
    string * tM1 = &tN1;
    string * tM2 = &tN2;
    //create thread 1
    if(pthread_create(&t1, NULL, thread_func, tM1) != 0)
    {
        return EXIT_FAILURE;
    }
    //if not EXIT_FAILURE
    else
    {
        cout << tN1 << endl;
    }
    //create thread 2
    if(pthread_create(&t2, NULL, thread_func, tM2) != 0)
    {
        return EXIT_FAILURE;
    }
    //if not EXIT_FAILURE
    else
    {
        cout << tN2 << endl;
    }
    //Block thread 1 until thread 2 stop
    if(pthread_join(t1, NULL) != 0)
    {
        return EXIT_FAILURE;
    }
    //Block thread 2 until thread 1 stop
    if(pthread_join(t2, NULL) != 0)
    {
        return EXIT_FAILURE;
    }
    
    return EXIT_SUCCESS;
}

My array
Code:
    const std::string THREAD_NAMES []    = {"POOH", "TIGGER", "ROO", "GOLPHER", "KANGA", "LUMPY", "OWL", "RABBIT", "PIGLET",
                                           "POOH0", "TIGGER0", "ROO0", "GOLPHER0", "KANGA0", "LUMPY0", "OWL0", "RABBIT0", "PIGLET0",
                                           "POOH1", "TIGGER1", "ROO1", "GOLPHER1", "KANGA1", "LUMPY1", "OWL1", "RABBIT1", "PIGLET1",
                                           "POOH2", "TIGGER2", "ROO2", "GOLPHER2", "KANGA2", "LUMPY2", "OWL2", "RABBIT2", "PIGLET2",
                                           "POOH3", "TIGGER3", "ROO3", "GOLPHER3", "KANGA3", "LUMPY3", "OWL3", "RABBIT3", "PIGLET3",
                                           "POOH4", "TIGGER4", "ROO4", "GOLPHER4", "KANGA4", "LUMPY4", "OWL4", "RABBIT4", "PIGLET4",
                                           "POOH5", "TIGGER5", "ROO5", "GOLPHER5", "KANGA5", "LUMPY5", "OWL5", "RABBIT5", "PIGLET5"
                                          };

# 2  
Old 08-21-2011
The threads don't know the order in which they're created, but they do have a unique thread ID. If you want them to know what string they are, either save their ID's somewhere by the string so they can look them up, or pass them a number so they know what ID they are. The latter would be simpler and better since you could get it all arranged properly before you create the thread and not after, eliminating race conditions.

Why not just do const char *THREAD_NAMES[]={ ... } and save yourself all the pointless C++ overhead?
# 3  
Old 08-26-2011
You can use
prctl(PR_SET_NAME,"Thread name",0,0,0)

But this only works on Linux 2.6.9 or higher versions
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. HP-UX

HP-UX: Shell Script giving " 0^J30: Syntax error"

Hi All, We are getting a very unique error while running a shell script on HP-UX box. Can somebody help in this regards? The shell script is working fine on linux/solaris box. Error: ++++++++++++++++++++++++ $/test.sh ./test.sh: 0^J30: Syntax error $ ++++++++++++++++++++++++ TIA.... (16 Replies)
Discussion started by: vai_sh
16 Replies

4. Shell Programming and Scripting

Bash Script giving "Command Not found"

Hello Geeks, Greetings...I have the following script: #!/usr/bin/bash #Script to generate number of active PDP context & calculate PDP activation #failurefrom EPG-M #Script written by Gbenga Adigun #September 12, 2013 username="xxxxxx" password="xxxxxxxxx" HOSTS=( ggsn01... (6 Replies)
Discussion started by: infinitydon
6 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Dummies Questions & Answers

Giving a name to a Terminal in "Xfce" desktop environment

Hi, I work noramly with 3/4/5 Terminals (not xterms) open. In the different Terminal I set different tools (software). Unfortunately I loose easily the overview of all these Terminals. Which setup is in which Terminal? It would be nice to have a possibility to name Termianls. By the way I work... (4 Replies)
Discussion started by: hooge789
4 Replies

7. Shell Programming and Scripting

In ksh shell command - Print "-ABC" is giving error

Hi Guys, while executing the following command : print "-ABC" is giving following error : ksh: print: bad option(s) I cannot use echo for some other reasons, so any other option ? (2 Replies)
Discussion started by: sagarjani
2 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. Shell Programming and Scripting

Giving "read" from standard input a timeout.

I want to prompt a user for input but I want it to timeout after a specified time if no response is given. I tried the sleep command but this does not work. I am using ksh. Thanks. (10 Replies)
Discussion started by: rello
10 Replies
Login or Register to Ask a Question