简体   繁体   English

从Java到C ++的线程

[英]threading from Java to C++

I have this Kevin class that implements runnable on JAVA and this KevinThreads class that create thread objects based on the Kevin instances and run it. 我有这个在JAVA上实现可运行的Kevin类,还有这个KevinThreads类,它基于Kevin实例创建线程对象并运行它。

public class KevinThreads{



    public static void main(String[] args){
        if(args.length!=2)
        {
            System.err.println("Usage: KevinThreads.java (1st param) (2nd param)");
            System.err.println("1st param: specifies the number of threads to be executed");
            System.err.println("2nd param: specifies the state of execution. 1=MUTEX,2=NON-MUTEX");
            return;
        }
        int number_of_threads = Integer.parseInt(args[0]);
        int thread_mode = Integer.parseInt(args[1]);
        for(int i=0;i<number_of_threads;i++){
        new Thread(new Kevin(i+1,thread_mode)).start();
        }

    }

}

I am now trying to replicate this logic to C++ with pthread. 我现在正尝试使用pthread将这种逻辑复制到C ++。 What I've done so far is I have a Kevin class 到目前为止,我要做的是参加凯文课程

#include <iostream>
#include <time.h>
#include <pthread.h>
using namespace std;

class Kevin
{
public:
    Kevin();
    void Speak(int value);
};

Kevin::Kevin()
{
    cout << "new instance of Kevin is created";
}

void Kevin::Speak(int value)
{
    cerr << "Name: Kevin\n" << value << "Seconds since epoch:" << time(0) << "\nThread id:" << pthread_self() << endl;
}

and a main.cc that somewhat similar to KevinThreads 和main.cc有点类似于KevinThreads

#include <iostream>
#include <pthread.h>
#include "Kevin.cc"
using namespace std;

int main(int argc, char *argv[])
{
    Kevin kevin1;
    int status;
    // creating thread objects
    pthread_t thrd_1;
    pthread_t thrd_2;
    // create thread
    pthread_create(&thrd_1,NULL,kevin1.Speak(1),(void *)0); //14
    pthread_create(&thrd_2,NULL,kevin1.Speak(2),(void *)0); //15
    pthread_join(thrd_1, (void **)&status);
    pthread_join(thrd_2, (void **)&status);
    system("PAUSE");
    return EXIT_SUCCESS;
}

however, it does not compile and gave me "Invalid use of void expression at main:14,15". 但是,它没有编译,并给了我“ main:14,15无效使用void表达式”。 I'm new to the C++ syntax, what does that error even mean? 我是C ++语法的新手,该错误甚至表示什么? I've tried changing (void *)0 to NULL, and I'm pretty sure the return type of kevin1.Speak() is not the problem that caused this. 我尝试将(void *)0更改为NULL,并且我很确定kevin1.Speak()的返回类型不是导致此问题的原因。 Any idea? 任何想法?

Your kevin1.Speak is a function - pthread_create wants the memory address of the function so instead of passing the actual function kevin.Speak(1) pass the memory address of the function &kevin.speak 您的kevin1.Speak是一个函数-pthread_create想要该函数的内存地址,因此与其传递实际的功能kevin.Speak(1)而不是传递函数&kevin.speak的内存地址

as for passing a value to the thread follow this format 至于将值传递给线程,请遵循以下格式

long t;

t = 1; //value to pass

pthread_create( &thread, NULL, function, (void*)t);

here is a full example with added functions for your class 这是为您的班级增加功能的完整示例

#include <iostream>
#include <time.h>
#include <pthread.h>
using namespace std;


class Kevin
{
public:
    Kevin(){
    cout << "new instance of Kevin is created";
}
    void Speak(int value){
    cerr << "Name: Kevin\n" << value << "Seconds since epoch:" << time(0) << "\nThread id:" << pthread_self() << endl;
}
};
Kevin kevin1;

void *kevinSpeaker1(void*)
{
    kevin1.Speak(2);
}
void *kevinSpeaker2(void*)
{
    kevin1.Speak(1);
}

int main(int argc, char *argv[])
{

    int status;
    // creating thread objects
    pthread_t thrd_1;
    pthread_t thrd_2;
    // create thread
    pthread_create(&thrd_1,NULL,&kevinSpeaker1,(void *)0); //14
    pthread_create(&thrd_2,NULL,&kevinSpeaker2,(void *)0); //15
    pthread_join(thrd_1, (void **)&status);
    pthread_join(thrd_2, (void **)&status);
    system("PAUSE");
    return EXIT_SUCCESS;
}

member function can not be directly used as callback function, and it should be a funciton address ,not the return value of a function call 成员函数不能直接用作回调函数,它应该是一个函数地址,而不是函数调用的返回值

you can not include a cc file #include "Kevin.cc" 您不能包含抄送文件#include "Kevin.cc"

try this piece of code 试试这段代码

#include <iostream>
#include <time.h>
#include <pthread.h>
using namespace std;

class Kevin
{
  public:
        Kevin();
        static void* Speak(void* value);
};

Kevin::Kevin()
{
      cout << "new instance of Kevin is created";
}

void* Kevin::Speak(void* value)
{
      cout << "Name: Kevin\n" << *((int*)value) << "  Seconds since epoch:" << "\nThread id:" << pthread_self() << endl;
}

int main(int argc, char *argv[])
{
      Kevin kevin1;
      int status;
      pthread_t thrd_1;
      pthread_t thrd_2;
      int i = 1;
      int j = 2;
      pthread_create(&thrd_1,NULL,Kevin::Speak,(void *)&i); //14
      pthread_create(&thrd_2,NULL,Kevin::Speak,(void *)&j); //15
      pthread_join(thrd_1, (void **)&status);
      pthread_join(thrd_2, (void **)&status);
      system("PAUSE");
      return EXIT_SUCCESS;
}

compile command g++ -o main main.cc -lpthread it outputs: 编译命令g ++ -o main main.cc -lpthread它输出:

new instance of Kevin is createdName: Kevin
1  Seconds since epoch:
Thread id:3083348896
Name: Kevin
2  Seconds since epoch:
Thread id:3074960288

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

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