Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将线程输出保存在变量中_C_Linux_Multithreading_Sockets_Pthreads - Fatal编程技术网

C 将线程输出保存在变量中

C 将线程输出保存在变量中,c,linux,multithreading,sockets,pthreads,C,Linux,Multithreading,Sockets,Pthreads,我有一个简单的客户端/服务器应用程序。我正在使用线程。 这是我的线程示例 void * thread2() { val1 = 6*a*a; val2 = a*a*a; printf("va1 = %f \n", va1); printf("val2 = %f \n", val2); } 这就是我在服务器中调用它的方式 // LEVEL 3 if(level == 3){ i

我有一个简单的客户端/服务器应用程序。我正在使用线程。 这是我的线程示例

void * thread2()

    {   
        val1 = 6*a*a;
        val2 = a*a*a;

            printf("va1 = %f \n", va1);
            printf("val2 = %f \n", val2);
    }
这就是我在服务器中调用它的方式

// LEVEL 3
    if(level == 3){
    int status;
    pthread_t tid1,tid2,tid3,tid4;


    pthread_create(&tid1,NULL,thread1,NULL);
    pthread_join(tid1,NULL);
    pthread_create(&tid2,NULL,thread2,NULL);
    pthread_join(tid2,NULL);
    pthread_create(&tid3,NULL,thread3,NULL);
    pthread_join(tid3,NULL);
    pthread_create(&tid4,NULL,thread4,NULL);
    pthread_join(tid4,NULL);

    level = 4;
}
问题是,所有内容都是在服务器上打印的,而不是在聊天中打印的,我不知道如何在
send()函数中使用它

这是我的
send()
函数的示例

s = send(newSocket, msg2, sizeof(msg2), 0);

比如如何将线程的输出保存在变量或其他东西中。我将非常感谢您的帮助。

您可以将参数传递给正在调用的线程。如果需要传递多个参数,请使用一个包含所有参数的结构。如果通过引用传递,则可以在线程完成后返回结果

见帖子,第一个答案


在您要传递的结构中,您应该包含一个boolexecuted,这样您就可以检查主函数中的线程是否已完全执行,因此您不会提取尚未设置的值。

如果我理解正确,您实际上希望保存线程的输出,将其打印在“chat”上而不是中如您所说的“服务器”。尝试简单地修改
stdout
,使其成为您正在谈论的聊天。使用
dup
系列检查重定向