参考http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html#SCHEDULING
我试图在C ++中创建两个线程并尝试将字符串作为参数传递给Thread Start Routine
。根据定义,Thread Start Routine
参数只能是(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;
}
有没有办法将string
作为(void *)
参数传递?或者只有C style strings
可以用作多线程参数 - 如链接中的参考代码所示。
答案 0 :(得分:6)
参数需要是指针,并且您尝试将对象传递给它。
您有两个选择,要么将指针传递给std::string
对象,要么将指针传递给基础字符串。我推荐第一个:
iret1 = pthread_create(&thread1, NULL, print_message_function, &message1);
然后你必须修改线程函数,否则它将打印指针而不是它指向的字符串:
void* print_message_function(void* ptr)
{
std::string str = *reinterpret_cast<std::string*>(ptr);
std::cout << str << std::endl;
return nullptr;
}
除非要求使用POSIX线程,否则我宁愿推荐C++ standard library中的线程功能:
#include <iostream>
#include <string>
#include <thread>
void print_message_function(const std::string& msg);
int main()
{
std::string message1 = "Thread 1";
std::string message2 = "Thread 2";
std::thread thread1(print_message_function, std::cref(message1));
std::thread thread2(print_message_function, std::cref(message2));
thread1.join();
thread2.join();
}
void print_message_function(const std:string& msg)
{
std::cout << msg << std::endl;
}
答案 1 :(得分:2)
您可以通过将所有参数包装到简单结构中来将任何参数传递给pthread_create
方法。例如:
struct ThreadParams {
std::vector<int> ints;
std::string clientName;
// more params
};
你需要做的就是在调用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;
}
答案 2 :(得分:2)
调用pthread_create时需要获取字符串对象的地址(内存位置):
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*)&message1);
当你打印它时你需要将你的内存void *转换回字符串指针(你不能写一个void *到一个流,流不会知道如何处理它),取消引用它:
cout << endl << *(string*)ptr << endl;
这应该可行,但为了完全正确,你应该看一下在向*和从*转换时使用reinterpret_cast。