小编M B*_*M B的帖子

POSIX 线程,将多个参数传递给具有结构的函数

所以我花了最后几个小时试图谷歌并找出我的代码有什么问题,但我无法弄清楚。

我是一名学生,我刚刚开始学习线程等,所以这对我来说都是全新的,我不是很有经验。

谷歌(和这里)上的答案通常是对代码中一个特定问题的答案,我不知道如何真正让这件事起作用。

这是我的代码的一个非常简化的版本:

http://pastebin.com/wst8Yw8z

#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>

using namespace std;

struct Data{
    string a;
    string b;
};

void* thread_func( void *param ){

    struct Data *input = (struct Data*)param;

    string data1 = input->a;
    string data2 = input->b;

    cout << "You said: " << data1 << " " << data2 << endl;

    return NULL;
}

int main( int argc, char *argv[] )
{
    pthread_t child;
    string arg, arg2;

    struct Data *input;

    cout << "Input …
Run Code Online (Sandbox Code Playgroud)

c++ concurrency multithreading posix pthreads

1
推荐指数
1
解决办法
1060
查看次数

标签 统计

c++ ×1

concurrency ×1

multithreading ×1

posix ×1

pthreads ×1