1
votes

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

3

3 Answers

3
votes

A pthread thread function has to conform to function definition specified by library:

int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)

so, your function list<int> inordertraversal(node* n) does not conform to void* ()(void)

you will have to change your function signature to a function which conforms to above signature and then call that recursively.

void* inorder(void)
{
    pthread_create(&threads[count++],NULL,wrapper,(void *)n->left);

}

Have list<int> l; as a class member, so that you dont need to pass it around with every function call.

0
votes

You're misinterpreting what pthread_create does. It just creates the thread and returns the status of the thread creation (did it work or fail). It doesn't handle at all what the function running in the thread returns. In fact the thread function probably shouldn't be returning the result at all. It should probably be updating some common variable accessible to all threads (at which point mutexes or some other mutual exclusion mechanism are going to be needed).

0
votes

pthread_create needs a function pointer which should have type void* ()(void*). Your function has return type list .