从 Java 线程到 C++

threading from Java to C++

本文关键字:C++ 线程 Java      更新时间:2023-10-16

我有一个在 JAVA 上实现可运行的 Kevin 类和这个基于 Kevin 实例创建线程对象并运行它的 KevinThreads 类。

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

我现在正在尝试将此逻辑复制到 pthread C++。到目前为止,我所做的是我有一个凯文课

#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: Kevinn" << value << "Seconds since epoch:" << time(0) << "nThread id:" << pthread_self() << endl;
}

以及一个有点类似于凯文线程的 main.cc

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

但是,它没有编译并给了我"在 main:14,15 无效地使用无效表达式"。我是C++语法的新手,这个错误甚至意味着什么?我尝试将 (void *)0 更改为 NULL,我很确定 kevin1 的返回类型。Speak() 不是导致此问题的问题。知道吗?

你的凯文1.Speak 是一个函数 - pthread_create想要函数的内存地址所以而不是传递实际的函数凯文。Speak(1) 传递函数的内存地址&kevin.speak

至于将值传递给线程遵循此格式

long t;
t = 1; //value to pass
pthread_create( &thread, NULL, function, (void*)t);

下面是一个完整的示例,其中包含为您的类添加的函数

#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: Kevinn" << 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;
}

成员函数不能直接用作回调函数,它应该是一个函数地址,而不是函数调用的返回值

不能包含 CC 文件#include "Kevin.cc"

试试这段代码

#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: Kevinn" << *((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;
}

编译命令 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