C++:クラスにスレッドを作成する

990 ワード


#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

class Test
{
public:

    Test():sum(0){}

    // static  void * insert_pth(void*);
    friend  void * insert_pth(void*);
    void lanch1();

private:
    int sum;
};

// void * Test::insert_pth(void* __this)
void* insert_pth(void* __this)
{
    Test * _this =(Test *)__this;
    sleep(2);
    _this->sum+=1;
    printf("%d insert.....
",_this->sum); } void Test::lanch1() { pthread_t pth; pthread_create(&pth,NULL,insert_pth,(void*)this); } int main() { Test t; t.lanch1(); pthread_t pid; pthread_create(&pid, NULL, insert_pth, (void*)&t); // pthread_create(&pid, NULL, Test::insert_pth, (void*)&t); pthread_join(pid, NULL); sleep(5); return 0; }