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.

smali
  • 4,687
  • 7
  • 38
  • 60
  • yes may be duplicate but here i am having problem while calling other non static function in the thread function. – smali Jun 04 '14 at 05:24
  • Wait... a... second... did you say C++11? – Jan Hudec Jun 04 '14 at 08: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. – Jan Hudec Jun 04 '14 at 08:08

5 Answers5

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);
}
Community
  • 1
  • 1
quantdev
  • 23,517
  • 5
  • 55
  • 88
  • Ya that i have tried but getting some errors can you sen me a link which is having all the doc – smali Jun 04 '14 at 05:37
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).

Community
  • 1
  • 1
Ilya
  • 4,583
  • 4
  • 26
  • 51
  • 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 – Matt Coubrough Jun 04 '14 at 05:21
  • 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. – Ilya Jun 04 '14 at 05:26
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);
}
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • 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. – smali Jun 04 '14 at 05:29
  • 1
    @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`. – Jim Mischel Jun 04 '14 at 05:36
  • Inappropriate for C++11... (-1). – Jan Hudec Jun 04 '14 at 08:04
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.

Anil8753
  • 2,663
  • 4
  • 29
  • 40
  • yes but my problem is diff. – smali Jun 04 '14 at 06:14
  • Do you want to have a class member function instead of a static function as a thread proc. – Anil8753 Jun 04 '14 at 06:16
  • 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. – smali Jun 04 '14 at 06:20
  • 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; } – Anil8753 Jun 04 '14 at 06:42
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;
}
Anil8753
  • 2,663
  • 4
  • 29
  • 40