Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java到C++;_Java_C++_Pthreads_Mutex_Semaphore - Fatal编程技术网

从Java到C++;

从Java到C++;,java,c++,pthreads,mutex,semaphore,Java,C++,Pthreads,Mutex,Semaphore,我有一个Kevin类,它在JAVA上实现runnable,还有一个KevinThreads类,它基于Kevin实例创建线程对象并运行它 public class KevinThreads{ public static void main(String[] args){ if(args.length!=2) { System.err.println("Usage: KevinThreads.java (1st param) (2

我有一个Kevin类,它在JAVA上实现runnable,还有一个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();
        }

    }

}
公共类KevinThreads{
公共静态void main(字符串[]args){
如果(参数长度!=2)
{
System.err.println(“用法:KevinThreads.java(第一个参数)(第二个参数)”;
System.err.println(“第一个参数:指定要执行的线程数”);
System.err.println(“第二个参数:指定执行状态。1=互斥,2=非互斥”);
返回;
}
int number_of_threads=Integer.parseInt(args[0]);
int thread_mode=Integer.parseInt(args[1]);

对于(inti=0;ikevin1.Speak是一个函数-pthread_create需要函数的内存地址 因此,与其传递实际函数kevin.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: 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;
}
#包括
#包括
#包括
使用名称空间std;
凯文班
{
公众:
凯文(){

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

您不能包含cc文件
#包含“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: 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;
}

那么,我怎么指定凯文的说法呢?凯文。凯文给我这个建议:ISO C++禁止取一个绑定成员的地址来形成一个指向成员函数的指针。比如说‘&:凯文:SpaKe'和凯文。(类)或者你正在做&kevin1.Speak(类的实例)?(后一种是正确的方法)我正在使用&kevin1.Speak()。&kevin1.Speak也不起作用。我修改了答案并添加了代码-我在这里测试了它,效果很好,我很确定我们可以做#包括“Kevin.cc”@麦克文文,但不是命令,代码中存在很多问题,我尝试修复它,希望代码能帮助java,我们把类文件分开保存,为什么C++不推荐?推荐它是什么,应该是。CPP不是。cc@McKevin,问题是,当您使用main.cc编译kevin.cc时(g++-o main.cc kevin.cc-lpthread)触发多定义错误
#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;
}
#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;
}
new instance of Kevin is createdName: Kevin
1  Seconds since epoch:
Thread id:3083348896
Name: Kevin
2  Seconds since epoch:
Thread id:3074960288