Hey everyone, considering the following code (compiled with g++ -lpthread thread_test.cpp
) how can I know what number thread I am in from inside "thread_function"? And let me know if you have any other suggestions.
Thanks!
thread_test.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
class A {
public:
A();
void run();
private:
static void* thread_function( void *ptr );
pthread_t m_thread1, m_thread2;
static int m_global;
};
int A::m_global = 0;
A::A() {
int ret1 = pthread_create( &m_thread1, NULL, &A::thread_function, this );
int ret2 = pthread_create( &m_thread2, NULL, &A::thread_function, this );
}
void A::run() {
while ( 1 ) {
printf( "parent incrementing...\n" );
m_global++;
sleep( 2 );
}
}
void* A::thread_function( void *ptr ) {
printf( "I'm thread ?\n" );
while ( 1 ) {
printf("thread global: %d\n", m_global );
sleep( 1 );
}
}
int main() {
A a;
a.run();
return 0;
}
You can use pthread_self() function.
Well i figured out I could do this, but I'm not sure if making the pthread_t variables static is the best thing to do. Opinions?
The correct answer depends heavily on why you need this information. If the two threads are doing different things, why do they have the same start function?
One simple fix is this:
If you have more than 2 threads, you can use another approach. Create a structure that holds all the information the thread needs, including the
this
pointer and which thread it is. Allocate a structure of that type, stuff it with everything the thread needs and pass that to the thread through thepthread_create
function instead of just thethis
pointer.