SORU
20 Temmuz 2009, PAZARTESİ


pthread sınıfından fonksiyon

Hadi böyle bir sınıf var diyelim

class c { 
    // ...
    void *print(void *){ cout << "Hello"; }
}

Ve sonra c vektörü var

vector<c> classes; pthread_t t1;
classes.push_back(c());
classes.push_back(c());

Şimdi, c.print(); bir iş parçacığı oluşturmak istiyorum

Ve beni takip sorunu aşağıda veriyor: pthread_create(&t1, NULL, &c[0].print, NULL);

Hata Çıkışı: ‘void* (tree_item::)(void) ’ ‘*void ()(void)’ argümanı ‘3’ ‘pthread_create(pthread_t*, int için pthread_attr_t*, ( . void* ^em>)(void), void*)’

CEVAP
20 Temmuz 2009, PAZARTESİ


C sınıfının üye fonksiyonları this gizli bir parametre geçirilen çünkü senin yazdığın şekilde yapamazsın. pthread_create() hiçbir fikri ne değeri this kullanın, çok çalışırsan vakit derleyici tarafından döküm yöntemi için bir işlev işaretçisi türü uygun bir segmetnation hatası. Statik sınıf yöntemi this parametre yok), ya da düz sıradan bir fonksiyonu kullanmak için sınıf bootstrap için:

class C
{
public:
    void *hello(void)
    {
        std::cout << "Hello, world!" << std::endl;
        return 0;
    }

    static void *hello_helper(void *context)
    {
        return ((C *)context)->hello();
    }
};
...
C c;
pthread_t t;
pthread_create(&t, NULL, &C::hello_helper, &c);

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • MovieZoneET

    MovieZoneET

    22 Aralık 2009
  • Pocketnow

    Pocketnow

    14 EKİM 2007
  • TV nEW

    TV nEW

    25 AĞUSTOS 2012