gpt4 book ai didi

c - 将线程输出保存在变量中

转载 作者:行者123 更新时间:2023-11-30 16:09:18 25 4
gpt4 key购买 nike

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

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);

就像我如何将线程的输出保存在变量或其他东西中。我将感谢任何帮助。

最佳答案

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

参见this发帖,第一个答案。

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

关于c - 将线程输出保存在变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59176017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com