简体   繁体   English

类中的 linux C++ 线程

[英]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?我与 main 和 IncWrapper 方法中的 pthread_create 不同,我可以更改它吗?

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:实际上不是在 ctor 中,因为有时您(或任何使用您的代码的人)可能会忘记在 ctor 中创建的线程并开始编码,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM