2009-09-11 6 views
5

हे सभी लोग निम्नलिखित कोड पर विचार करते हुए (g++ -lpthread thread_test.cpp के साथ संकलित) मुझे कैसे पता चलेगा कि मैं "thread_function" के अंदर से कौन सा नंबर थ्रेड हूं? और अगर आपके पास कोई अन्य सुझाव है तो मुझे बताएं।कक्षा में

धन्यवाद!

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; 
} 

उत्तर

3

आप pthread_self() फ़ंक्शन का उपयोग कर सकते हैं।

+0

ठीक है, मैं अब देखता हूं ... क्या मुझे कुछ प्रकार के थ्रेड विशिष्ट डेटा को स्टोर करने के लिए pthread_key_create जैसे कुछ का उपयोग करना चाहिए ताकि यह पहचान सके कि यह धागा 1 या 2 है या नहीं? मैं यह पता लगाने की कोशिश कर रहा हूं कि क्या pthread_key_create वास्तव में मैं चाहता हूं ... –

+0

आप थ्रेड विशिष्ट चर का उपयोग कर सकते हैं लेकिन मुझे नहीं लगता कि यह आवश्यक है। जब भी आप वर्तमान थ्रेड नंबर का उपयोग करना चाहते हैं तो बस pthread_self() को कॉल करें। – danadam

+0

आप एक संख्या के रूप में 'pthread_t' का उपयोग कैसे करते हैं? –

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; 
} 
+1

आप उस तरह से pthread_t की तुलना नहीं कर सकते हैं। आपको * pthread_equal का उपयोग करना होगा। –

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 पॉइंटर के बजाय थ्रेड को पास करें।

संबंधित मुद्दे