C++ 如何将一个值从一个线程传递给main
How to pass a value from a thread to main in c++
我是 OS 的初学者。我遇到的问题是我想从线程接收一个值到主进程中。垃圾值打印在 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);
}
尝试:
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
您有两种方法可以做到这一点:
将指向整数的指针作为参数传递给线程,并让线程修改参数以保存您想要的值。
让线程在堆上分配一个值并 return 一个指向该内存的指针。以后再清理吧。
#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));
}
我是 OS 的初学者。我遇到的问题是我想从线程接收一个值到主进程中。垃圾值打印在 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);
}
尝试:
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
您有两种方法可以做到这一点:
将指向整数的指针作为参数传递给线程,并让线程修改参数以保存您想要的值。
让线程在堆上分配一个值并 return 一个指向该内存的指针。以后再清理吧。
#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)); }