C++ 将字符串作为线程启动例程参数传递:C++;
指 我试图在C++中创建两个线程,并尝试将字符串作为参数传递给<代码>线程启动例程。根据定义,C++ 将字符串作为线程启动例程参数传递:C++;,c++,multithreading,C++,Multithreading,指 我试图在C++中创建两个线程,并尝试将字符串作为参数传递给线程启动例程。根据定义,线程启动例程参数只能是(void*)类型: int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg);
线程启动例程
参数只能是(void*)
类型:
int pthread_create(pthread_t * thread,
const pthread_attr_t * attr,
void * (*start_routine)(void *),
void *arg);
但我得到以下错误:
$ make
g++ -g -Wall Trial.cpp -o Trial
Trial.cpp: In function `int main()':
Trial.cpp:22: error: cannot convert `message1' from type `std::string' to type `void*'
Trial.cpp:23: error: cannot convert `message2' from type `std::string' to type `void*'
Makefile:2: recipe for target `Trial' failed
make: *** [Trial] Error 1
代码是
#include <iostream>
#include <pthread.h>
#include <string>
using namespace std;
void *print_message_function( void *ptr );
int main()
{
pthread_t thread1, thread2;
string message1 = "Thread 1";
string message2 = "Thread 2";
int iret1, iret2;
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
cout << "Thread 1 returns: " << iret1 << endl;
cout << "Thread 2 returns: " << iret2 << endl;
return 0;
}
void *print_message_function( void *ptr )
{
cout << endl << ptr << endl;
//return 0;
}
#包括
#包括
#包括
使用名称空间std;
void*打印消息功能(void*ptr);
int main()
{
pthread_t thread1,thread2;
字符串message1=“线程1”;
string message2=“线程2”;
int iret1,iret2;
iret1=pthread_create(&thread1,NULL,print_message_函数,(void*)message1);
iret2=pthread_create(&thread2,NULL,print_message_函数,(void*)message2);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
cout参数必须是指针,并且您尝试向其传递一个对象
您有两种选择,要么传递指向std::string
对象的指针,要么传递指向底层字符串的指针。我推荐第一种:
iret1 = pthread_create(&thread1, NULL, print_message_function, &message1);
然后您必须修改thread函数,否则它将打印指针,而不是指向的字符串:
void* print_message_function(void* ptr)
{
std::string str = *reinterpret_cast<std::string*>(ptr);
std::cout << str << std::endl;
return nullptr;
}
void*打印消息功能(void*ptr)
{
std::string str=*重新解释(ptr);
std::cout通过将所有参数包装到简单结构中,可以将任何参数传递给pthread\u create
方法。例如:
struct ThreadParams {
std::vector<int> ints;
std::string clientName;
// more params
};
struct ThreadParams{
std::向量整数;
std::字符串clientName;
//更多参数
};
您只需在调用CreateThread函数之前初始化此结构,然后传递指针:
ThreadParams * params = new ThreadParams();
params.setParameters();
pthread_create(..., params);
void* print_message_function(void* arg)
ThreadParams * params = reinterpret_cast<ThreadParams *>(arg);
// delete after usage;
delete params;
}
ThreadParams*params=新的ThreadParams();
setParameters();
pthread_create(…,params);
void*打印消息函数(void*arg)
ThreadParams*params=重新解释强制转换(arg);
//使用后删除;
删除参数;
}
调用pthread\u create时,需要获取字符串对象的地址(内存位置):
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*)&message1);
在打印时,您需要将内存void*转换回字符串指针(您无法将void*写入流,流将不知道如何处理它),取消引用:
cout << endl << *(string*)ptr << endl;
cout使用iret1=pthread\u创建(&thread1,NULL,print\u message\u函数,&message1);
我需要修改我的void*print\u message\u函数(void*ptr)
它显示了字符串的地址和当前代码。如何将其转换为字符串?顺便说一句,请注意,您需要保持“message1”字符串在线程的整个生命周期内有效。传递堆栈上的参数是否安全?@Yossarian由于message1是主线程的一部分,我认为它在主线程之前是有效的正在执行中…因此也为thord1……?@ @ Guravk用一个更……“C++”ISH版本更新了我的答案,使用C++ <代码>线程< /C>类。
cout << endl << *(string*)ptr << endl;