从 Java 线程到 C++
threading from Java to C++
我有一个在 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
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- c++中的线程池
- 线程之间的布尔停止信号
- 为什么std::async使用同一个线程运行函数
- 用于矢量处理的多个线程
- C++为线程工作动态地分割例程
- 为什么我不能在 while 循环中创建线程?