3

参考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可用作多线程参数 - 如链接中的参考代码中所示。

4

3 回答 3

7

参数需要是一个指针,并且您尝试将一个对象传递给它。

你有两个选择,要么传递一个指向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++ 标准库中的线程功能:

#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;
}
于 2013-05-14T11:03:11.453 回答
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;
}
于 2013-05-14T11:05:20.543 回答
2

调用 pthread_create 时需要获取字符串对象的地址(内存位置):

iret1 = pthread_create( &thread1, NULL, print_message_function, (void*)&message1);

并且您需要在打印时将内存 void* 转换回字符串指针(您不能将 void* 写入流,流将不知道如何处理它),取消引用它:

cout << endl <<  *(string*)ptr << endl;

这应该可行,但要完全正确,您可能应该考虑在与 void* 之间进行转换时使用 reinterpret_cast。

于 2013-05-14T11:07:07.973 回答