views:

95

answers:

2

This is the usage case:

Log(char* s); // prints out a log message

Now:

Log("hello world\n"); // called from Thread1

Desired output:

Thread1: hello world

Now:

Log("hello world\n"); // called from Thread2

Desired output:

Thread2: hello world

I can have a map that maps thread pids to strings. What I need however, is a function that when called form different threads gives me unique identification tags. Is this possible?

Thanks!

(Langauge: C/C++)

+3  A: 

You'll need to pass pthread_self() into your Log() function (or write a macro).

Dave Bacher
+1  A: 

One clean way to do it would be to create a thread attribute (using pthread_attr_*) for the thread name, set the attribute in each thread, then read it in the log function.

You could also use pthread_self in the log function.

Dietrich Epp