【问题标题】:How to pass a value from a thread to main in c++如何将值从线程传递到C++中的主线程
【发布时间】:2021-08-15 03:38:39
【问题描述】:

我是操作系统的完整初学者。我遇到的问题是我想从线程接收一个值到主进程。一个垃圾值被打印在 main 中。请详细说明,以便我知道我的错误。

这是我的代码:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include<string.h>
#include <sys/wait.h>
#include<pthread.h>
#include <sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<time.h>
#include <fstream>
#include<vector>

using namespace std;

void *thread_1(void *arg);

int main(int argc, char* argv[])
{
    void *temp;
    pthread_t t1;
    pthread_create(&t1, NULL,thread_1,NULL);
    pthread_join(t1,&temp);
    int *num = (int*)temp;
    cout<<"Value in main: "<<*num<<endl;
}

void *thread_1(void *arg)
{
    int number = 5;
    int* value = &number;
    cout<<"Value in thread: "<<*value<<endl;
    pthread_exit((void*)value);
}

【问题讨论】:

  • 可以更改我的代码并在此处分享吗?我试过但没有帮助....同样的问题:(
  • 您正在发出一个指向堆栈上项目的指针,该项目在指针被取消引用时将不存在。停止这样做,最好完全停止使用 join()。

标签: c++ linux multithreading operating-system


【解决方案1】:

您有两种方法可以做到这一点:

  1. 将指向整数的指针作为参数传递给线程,并让线程修改参数以保存您想要的值。

  2. 让线程在堆上分配一个值并返回一个指向该内存的指针。以后再清理吧。

    #include <iostream>
    #include <pthread.h>
    
    void *thread_1(void *arg);
    void *thread_2(void *arg);
    
    int main(int argc, char* argv[])
    {
        int value = 0;
        pthread_t t1;
        pthread_create(&t1, NULL,thread_1, &value);
        pthread_join(t1, NULL);
    
        std::cout<<"Value in main: "<<value<<std::endl;
    
    
        int* value2 = NULL;
        pthread_t t2;
        pthread_create(&t2, NULL,thread_2, NULL);
        pthread_join(t2, reinterpret_cast<void**>(&value2));
    
        if (value2) {
            std::cout<<"Value in main: "<<*value2<<std::endl;
            delete value2;
        }
    }
    
    void *thread_1(void *arg)
    {
        int* value = static_cast<int*>(arg);
        *value = 5;
    
        std::cout<<"Value in thread: "<<*value<<std::endl;
        pthread_exit(NULL);
    }
    
    void *thread_2(void *arg)
    {
        int* value = new int(10);
        std::cout<<"Value in thread: "<<*value<<std::endl;
        pthread_exit(static_cast<void*>(value));
    }
    

【讨论】:

    【解决方案2】:

    试试:

    void *thread_1(void *arg)
    {
        
        int value = *((int *)arg);
        cout<<"Value in thread: "<<value<<endl;
        int new_val = 345;
        pthread_exit(&new_val );
    }
    
    int main(int argc, char* argv[])
    {
        int *temp=15;
        void *return_val;
        pthread_t t1;
        pthread_create(&t1, NULL,thread_1,(void *)temp);
        pthread_join(t1,&return_val);
    
        
        cout<<"Value in main: "<<*temp<<endl;
        cout<<"Value from loop: "<<*(int*)return_val<<endl;
    }
    

    更多信息:http://www.cse.cuhk.edu.hk/~ericlo/teaching/os/lab/9-PThread/Pass.html

    【讨论】:

    • 我想从一个线程接收一个值到main。您正在将值从 main 传递到线程中......
    • @Ibrahim 我已经进行了更改。
    • 那不会编译。你应该避免 C 风格的转换,它会隐藏错误。使用 C++ 转换来显示错误。
    猜你喜欢
    • 2018-01-30
    • 2014-08-04
    • 1970-01-01
    • 2021-12-17
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    相关资源
    最近更新 更多