1
'pthread_create (thread, attr, start_routine, arg)'

Can i call a non static function creating threads which is having more than one arguments, since pthread_create() will only take one argument and that is of void type.

I am using threads in my class which is having many functions which perform heavy task so i want to execute each function on their own threads but i am not able to do that since Pthrad_create only takes one argument and also the function of type static, so how can i solve this issue.

Thanks.

3
  • yes may be duplicate but here i am having problem while calling other non static function in the thread function. Commented Jun 4, 2014 at 5:24
  • Wait... a... second... did you say C++11? Commented Jun 4, 2014 at 8:04
  • You have exactly the same problem as described in the suggested duplicate. If not you are either describing it wrong or missing the simple fact that it's trivial to wrap a method call in a function. Since you tagged C++11, your solution should be different though. Commented Jun 4, 2014 at 8:08

5 Answers 5

3

To complete other answers about pthread :

Since you tagged C++11, avoid pthread altogether, and use std::thread :

std::thread t(func1,a,b,c,d);

It's portable, and easier to maintain.

EDIT:

As you asked, you can find the std::thread documentation here.

This SO post discussed passing multiple arguments too :

#include <thread>

void func1(int a, int b, ObjA c, ObjB d){
    //...
}

int main(int argc, char* argv[])
{
    std::thread t(func1,a,b,c,d);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ya that i have tried but getting some errors can you sen me a link which is having all the doc
2

You could put all of those parameters into a structure and pass a pointer to that structure to a stub function. The stub function then calls the real function.

struct foo
{
    int param1;
    int param2;
    // etc.
};

struct foo myFoo;
myFoo.param1 = 1;
myFoo.param2 = 42;

pthread_create(thread, attr, stub_function, &myFoo);


void stub_function(void* params)
{
    foo myFoo = (foo*)params;
    real_function(myFoo->param1, myFoo->param2);
}

3 Comments

Thanks but i am not able to call other non static functions directly. how can i solve that if i create the object and call it works but gives segmentation fault.
@ali786: Possibly the problem is that you're passing the address of a stack-allocated structure. Try making it a class and allocating with new. Just be sure to delete it after the call to real_function.
Inappropriate for C++11... (-1).
2

Yes, you can. For example, you can transfer pointer to struct containing many arguments.

See: Multiple arguments to function called by pthread_create()? (and first answer).

2 Comments

Best to just flag as a duplicate and add a comment rather than answer the question if your answer is simply a link to another answer
Ok, I agree with this rule. But it is not duplication, because author of that question had different problem. Also I've explained how to solve the problem (the link is only addition to my answer). But thank you for your comment! I'll try to avoid such short answers with links.
2

Use structure to pass multiple arguments. Here you can pass any data including class object pointers. In static thread proc you can do type casting to get Class object pointers again and here you are free to use your class object pointer as per your requirement.

4 Comments

yes but my problem is diff.
Do you want to have a class member function instead of a static function as a thread proc.
yes, you are correct but i am trying to call other non static function in this static where i am getting error, if i create an object and call the function it is working but giving segmentation fault.
You can not call a class non static function from a static method. you need object or valid object pointer to call a function. You must forgot to assign memory to class object pointer. try below code: class Student { public: Student() { m_Age = 25; } int m_Age; }; typedef struct { Student pStudent; } DATA; static void fnStatic(void *pData) { DATA *data = (DATA)pData; cout << data->pStudent->m_Age; } int _tmain(int argc, _TCHAR* argv[]) { Student *pStudentPtr = new Student(); DATA *data = new DATA(); data->pStudent = pStudentPtr; fnStatic(data); return 0; }
1
class Student
{
public:
    Student() { m_Age = 25; }
    int m_Age;
};

typedef struct
{
    Student *pStudent;
    // You can add other data members also.
} DATA;

static void fnStatic(void *pData)
{
    DATA *data = (DATA*)pData;
    cout << data->pStudent->m_Age << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
    Student *pStudentPtr = new Student();

    DATA *data = new DATA();
    data->pStudent = pStudentPtr;

    fnStatic(data);
    return 0;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.