I am trying out multithreading of inorder traversal of binary trees. I am getting errors due to pthread_create.
#include <iostream>
#include <pthread.h>
#include <list>
using namespace std;
static pthread_t threads[9];
static int count=0;
struct node
{
int value=0;
node *left=NULL;
node *right=NULL;
};
list<int> inordertraversal(node* n)
{
list<int> l1,l2;
if(n->left!=NULL)
list<int> l1=pthread_create(&threads[count++],NULL,inordertraversal,n->left);
if(n->right!=NULL)
list<int> l2=pthread_create(&threads[count++],NULL,inordertraversal,n->right);
list<int> l;
l.insert(l.begin(),l1.begin(),l1.end());
l.push_back(n->value);
l.insert(l.end()--,l2.begin(),l2.end());
return l;
}
struct node* newNode(int data)
{
node* node;
node->value=data;
node->left=NULL;
node->right=NULL;
return node;
}
int main()
{
struct node *root=newNode(7);
root->left=newNode(9);
root->right=newNode(5);
root->left->left=newNode(13);
root->left->right=newNode(17);
root->right->left=newNode(56);
root->right->right=newNode(21);
root->left->left->left=newNode(45);
root->right->left->right=newNode(45);
root->left->left->right=newNode(67);
list<int> l=inordertraversal(root);
for(list<int>::iterator it=l.begin();it!=l.end();it++)
{
cout<<*it<<" ";
}
}
I would like to return list elements from the function passed on to the thread using pthread_create. The error is as follows:-
/usr/include/pthread.h|244|error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)’ [-fpermissive]|
/home/dinu94/dummyspace/interview_prep/ThreadedBinaryTree/main.cpp|25|error: conversion from ‘int’ to non-scalar type ‘std::list’ requested|
/home/dinu94/dummyspace/interview_prep/ThreadedBinaryTree/main.cpp|28|error: invalid conversion from ‘std::list ()(node)’ to ‘void* ()(void)’ [-fpermissive]|
/usr/include/pthread.h|244|error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)’ [-fpermissive]|
/home/dinu94/dummyspace/interview_prep/ThreadedBinaryTree/main.cpp|28|error: conversion from ‘int’ to non-scalar type ‘std::list’ requested
I am not sure how to proceed.
edit: what are the alternative ways of returning value if pthread_create is the wrong way to do?
Thanks