开发者

linux C++ thread in class

Hi i want to do the class with method which will start in separate thread after creating class. That how i do that:

class Devemu {
int VarInc;

void Increm() {
    for(;;) {
        if (VarInc > 532) VarInc = 0;
        else VarInc++;
  开发者_如何学运维  }
}

public:
static void* IncWrapper(void* thisPtr) {
    ((Devemu*) thisPtr)->Increm();
    return NULL;
}
Devemu() {
    VarInc = 0;
}
int Var() {
    return VarInc;
}
};
int main(int argc, char** argv) {

Devemu* em = new Devemu();
pthread_t thread_id;
pthread_create(&thread_id, NULL, &Devemu::IncWrapper, NULL);


for(int i = 0 ;i < 50; i++) {
printf("%d\n", em->Var());
}
return (EXIT_SUCCESS);
}

I unlike that pthread_create in main and IncWrapper method can i change that?


Yes, you can put it in the constructor if you like :

class Devemu {
int VarInc;
pthread_t thread_id;

void Increm() {
    for(;;) {
        if (VarInc > 532) VarInc = 0;
        else VarInc++;
    }
}

public:
static void* IncWrapper(void* thisPtr) {
    ((Devemu*) thisPtr)->Increm();
    return NULL;
}
Devemu() {
    VarInc = 0;

    pthread_create(&thread_id, NULL, &Devemu::IncWrapper, NULL);
}
int Var() {
    return VarInc;
}
};


I suppose it's better to put the thread creation in the separate member-function like that:

class Devemu {

    ...

    void run()
    {
        pthread_create(&thread_id, NULL, &Devemu::IncWrapper, NULL);
    }

    ...
};

Not actually in ctor, because sometimes you (or anyone who uses your code) can forget that thread created in the ctor and start coding like:

Devemu m;
...
Devemu m1;
...

creating unnecessary threads just like instances of the class.


If you want to get working source code you need make next changes:

--- so0.cpp     2019-11-04 11:26:11.101984795 +0000
+++ so1.cpp     2019-11-04 11:26:57.108501816 +0000
@@ -1,3 +1,7 @@
+#include "stdio.h"
+#include <pthread.h>
+#include <cstdlib>
+
 class Devemu {
 int VarInc;

@@ -24,7 +28,7 @@

 Devemu* em = new Devemu();
 pthread_t thread_id;
-pthread_create(&thread_id, NULL, &Devemu::IncWrapper, NULL);
+pthread_create(&thread_id, NULL, &Devemu::IncWrapper, em);

My variant for resolving your problem is :

#include "stdio.h"
#include <pthread.h>
#include <cstdlib>
#include <unistd.h>

class Devemu {
private:
    int VarInc;

    pthread_attr_t attr; /* отрибуты потока */
    pthread_t thread_id;

    void Increm();
public:
    static void* IncWrapper (void* thisPtr);
    Devemu();
    ~Devemu();
    int Var();
};

void Devemu::Increm() {
    while (true) {
        if (VarInc > 532) {  VarInc = 0; } else { VarInc++; }
    }
}

void* Devemu::IncWrapper(void* thisPtr) {
    ((Devemu*) thisPtr)->Increm();
    return NULL;
}

Devemu::~Devemu() {
    pthread_cancel (thread_id);
}

Devemu::Devemu() {
    VarInc = 0;
/// get default value of arrts
    pthread_attr_init(&attr);
/// start thread
    pthread_create(&thread_id, &attr, &Devemu::IncWrapper, this);

}

int Devemu::Var() {
    return VarInc;
}

int main(int argc, char** argv) {
    Devemu* em = new Devemu();

    for(int i = 0 ; i < 100; i++) {
        printf("%d\n", em->Var());
        usleep (10);
    }
    delete em;
    usleep (1000);
    return (EXIT_SUCCESS);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜