0

To return an int from a posix thread you would Do

void* myfunc(void *args)
{
int *result = (int *) malloc(size of(int));

...
*result= 3;
return result;
}

void main()
{
int *received;
...
pthread_join(thread_var,&received);
std::cout<< "thread int val: "<<*received;
}

How about if it were a paired vector as such

vector<pair<int,string>>;

  • 3
    Well, you will start by actually using `std::thread` in C++ code. Is there a particular reason why you are using POSIX threads in C++? – Sam Varshavchik Nov 13 '22 at 13:20
  • 3
    You should almost *never* use `malloc` in C++. In fact, every time you need to use a C-style cast (like `(int *)` in front of the `malloc` call) you should take that as a sign that you're doing something wrong. – Some programmer dude Nov 13 '22 at 13:22
  • 4
    And whatever resource you're using to learn C++, it seems to teach you more C than C++. Please try to invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn C++ properly. – Some programmer dude Nov 13 '22 at 13:23

0 Answers0