How Can I use pthread_create ?


 
Thread Tools Search this Thread
Top Forums Programming How Can I use pthread_create ?
# 1  
Old 01-11-2006
Question How Can I use pthread_create ?

Hi.

I use C++ and I wishes to create a thread with the pthread_create function, my question is, how can I do this if I wish that the function will be a member of the class ??

I know from windows programming that I can declare a static function like this

static unsigned int __stdcall funcName(void*);

how can I do something similar in unix ??

Thanks.
# 2  
Old 01-12-2006
hey check this out

http://www.geocities.com/hemantborole/
clik on "knowledge bank", click on "c++" under "My Programs". in the file list you will get a program named pthread1.C. That is probably what you are looking for.
# 3  
Old 01-13-2006
Question Writing a HTTP Client/Server program.

Thanks alot.

That example teaches how to work with thread in C, I need an example of how to use it in C++ OOP, so that the function that the thread runs it a member function of the class.

Thanks again.
# 4  
Old 01-13-2006
oops sorry, I hadnt uploaded the correct program, on the same page look for thread.C

hope this helps
# 5  
Old 03-11-2008
MySQL hay try this cpp code to thread member functions!!!!!

as u know threads are called by simple calling a static function.

but member functions are restricted to the scope of an object as they access on variables within an object specific environment.

so one of the best way to do this is a JAVA style generic threading architecture- i hav provided a simple code here---njoy!!!!!

~~~~~~~~~~~~~~~~~~~~~

Code:
#include<iostream>
#include<pthread.h>
#include<sys/types.h>

using namespace std;

// this class is used to make generic calls to the run function
// in any object which have been inherited from object class

class Object
{
    public:
      virtual void run(){}
};

// now we will create an executer non member function
//which can call the run function of any Object class instance

void* executer(void* param)
{
   Object *obj=(Object*)param;
   obj->run();
   // this will definitely call the run function associated with the object obj
   // from this general function--the rest i guess u can figure out!
}

//now we will create the threading framework

class thread : public Object
{
  pthread_t thid;
  int ret;
  public:
   void start();
   void join();
};

// this member function will call the run function of the derived class
void thread::start()
{
   ret=pthread_create(&thid,NULL,executer,(void*)this);
}
// this is to join the object threads
void thread::join()
{
   pthread_join(thid,NULL);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// thats all thats required for the basic one
// now we use the above code...
// if we have to call a member function of a class
// we inherit it from the class thread and call the derived calss object //start function
// the run function will get executed as a thread!!!! just as java does it
// note that only the run function is threadable here
// further modifications is up to u - this is only the base
// by the way i hav not provided nything for synchronisation-give it where
//required, alogo to pass arg to threads, calling functions other than the //run() etc....
//  as an example of the above frame work we shall see a small example
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class multicount : public thread
{
   char *name;
  public:
    multicount(char n[])
    {
      name=n;
    }
    void run();
};
void multicount::run()
{
   for(int i=0;i<10000;i++)
   {
     cout<<"\n"<<name<<" : "<<i;
   }
}

int main()
{
  // we first create 3 instances(objects) of multicount a,b,c
  multicount a("thread1"),b("thread2"),c("thread3");
  // then we call the a.start() of object a to create a thread for doing  
  // a.run() and similarly for others
  a.start();
  b.start();
  c.start();
  // finally we wait for all threads to join
  a.join();
  b.join();
  c.join();

  //creating such frameworks greatly reduces programming difficulty with 
  // OOP
  // see how eazy the thread operations became with the simple 
  //architecture

  return 0;
}


Last edited by Franklin52; 12-27-2008 at 07:30 AM.. Reason: adding code tags
# 6  
Old 03-11-2008
hay try this cpp code to thread member functions!!!!!-with the correct code

hope this answers your question
# 7  
Old 12-23-2008
that's good. thanks for sharing the way you code, tellarunbose...Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

fork vs pthread_create

Suppose I have a simple program main() with a global varibale int x=0. int x = 0; main() { print("%d\n",x); } I want to create two threads/process which must access this variable x in sync. Which one will be better threads( pthread_create ) or process( fork )? If I go with fork() then... (1 Reply)
Discussion started by: rupeshkp728
1 Replies

2. UNIX for Dummies Questions & Answers

Pthread_create problem

Hi, I'm trying to do my homework assignment but I am having trouble using the pthread_create fucntion. Here is my code________________ //Alicia Johnson //sum_pid program //creates n number of threads. These threads create a random number //then adds the number to a global array. Then... (1 Reply)
Discussion started by: ajohns38
1 Replies

3. Programming

pthread_create

The prototype for pthread_create function is like this:- int pthread_create(pthread_t *thread,pthread_attr_t *attr,void *(*start routine),void *arg); Q.1 .Why the return type of the start_routine must be void*?? Q.2. Why should we pass arg by converting into void * only ?? Thank You (3 Replies)
Discussion started by: sunil_abhay
3 Replies

4. Programming

undefined reference to `pthread_create'

Hi guys. H was learning posix threads in C with anjuta IDE. it gives me undefined reference to `pthread_create' I know i should compile it like: gcc -lpthread main.c how should i import this configuration in anjuta so i can compile inside it? (2 Replies)
Discussion started by: majid.merkava
2 Replies

5. Programming

undefined reference to pthread_create

I try to compile a sample c code in fedora eclipse 3.2 as managed makefile using pthread library,it shows some error on pthread functions.Error is of undefined reference to pthread.Anybody guide me to solve this problem. Thanking you (1 Reply)
Discussion started by: sujith4u87
1 Replies

6. Solaris

pthread_create failed upon execution

Im trying to run an application i compiled (iperf) and i get an error telling me that it cant create the pthread. when i ran the ./configuration one of the things it checked was for pthreads which came back ok. Im not really sure where to even start to resolve this. i have been unable to find... (5 Replies)
Discussion started by: jrich523
5 Replies

7. Programming

Pthread_create issue

Hello My problem goes like this: I have used Pthread_create, and I have tryed to create 2 proccess but nothing happens! It does not even matter what the function im trying to create do. It is if im trying to activate an empty function. This is my code. Any help will be highly appreciated.... (1 Reply)
Discussion started by: Hellboy
1 Replies

8. Programming

unresolve pthread_create etc

how to do with that? after cc -o xxxx xxxx.c ld: Unresolved: _pthread_create _pthread_deteach _pthread_exit Thanks (3 Replies)
Discussion started by: zhshqzyc
3 Replies

9. Programming

pthread_create problem

Here is simple code for multithreading in POSIX: void* simplethread(void* arg) { printf("Hello World\n"); } int main(void) { pthread_t id; pthread_create(&id, NULL, simplethread, NULL); return 0; } Whether the new thread will run or not depends on the OS. Tricky ... (5 Replies)
Discussion started by: _rocky
5 Replies
Login or Register to Ask a Question