5

大家好,考虑到以下代码(用 编译g++ -lpthread thread_test.cpp),我如何从“thread_function”中知道我所在的线程数?如果您有任何其他建议,请告诉我。

谢谢!

线程测试.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;
}
4

3 回答 3

3

您可以使用 pthread_self() 函数。

于 2009-09-11T22:34:26.393 回答
1

正确答案很大程度上取决于您为什么需要这些信息。如果两个线程在做不同的事情,为什么它们具有相同的启动功能?

一个简单的解决方法是:

#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, int which );
      static void* thread_function_1( void *ptr );
      static void* thread_function_2( 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_1, this );
   int ret2 = pthread_create( &m_thread2, NULL, &A::thread_function_2, this );
}

void A::run() {
   while ( 1 ) { 
      printf( "parent incrementing...\n" );
      m_global++;
      sleep( 2 );
   }   
}

void* A::thread_function_1( void *ptr ) { thread_function(ptr, 1); }
void* A::thread_function_2( void *ptr ) { thread_function(ptr, 2); }

void* A::thread_function( void *ptr, int which ) { 
   printf( "I'm thread %d\n", which );

   while ( 1 ) { 
      printf("thread global: %d\n", m_global );
      sleep( 1 );
   }   
}

int main() {
   A a;
   a.run();

   return 0;
}

如果您有超过 2 个线程,则可以使用另一种方法。创建一个结构来保存线程所需的所有信息,包括this指针和它是哪个线程。分配该类型的结构,用线程需要的所有内容填充它,并通过pthread_create函数而不是this指针将其传递给线程。

于 2011-08-27T16:59:43.013 回答
1

好吧,我想我可以做到这一点,但我不确定将 pthread_t 变量设为静态是否是最好的做法。意见?

#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 );
      static pthread_t m_thread1, m_thread2;

      static int m_global;
};

int A::m_global = 0;
pthread_t A::m_thread1 = 0;
pthread_t A::m_thread2 = 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 ) { 
    int thread_num = 0;
    if ( pthread_self() == m_thread1 ) {
        thread_num = 1;
    } else {
        thread_num = 2;
    }

    printf( "I'm thread %d\n", thread_num );

    while ( 1 ) { 
        printf("thread %d global: %d\n", thread_num, m_global );
        sleep( 1 );
    }   
}

int main() {
   A a;
   a.run();

   return 0;
}
于 2009-09-12T02:52:10.563 回答