#include <PThreadRun.h> #include <iostream> using namespace std; class Test : public PThreadRun { void run() { for (int i=0;i<5;i++) { cout<<i<<" ";cout.flush(); sleep(1); } } }; int main() { Test test; int nrThreads=10; pthread_t pthread1[nrThreads]; // start threads for (int i=0;i<nrThreads;i++) pthread1[i]=test.start(); sleep(7); // join threads for (int i=0;i<nrThreads;i++) test.join(pthread1[i]); return 1; }
As the return type is also a void* we have to keep the pointers target in scope after returning, otherwise the target will be deallocated. Therefore, in this example, I choose to return a pointer to the local static variables 'ret'.
#include <PThread.h> #include <iostream> using namespace std; class Test: public PThread { public: Test() { pthread_mutex_init(&mutex,NULL);} ~Test() { pthread_mutex_destroy(&mutex);} void setString(string str) { s=str;} friend void *testThread1(void *t); friend void *testThread2(void *t); private: string s; pthread_mutex_t mutex; }; void *testThread1(void *t) { Test *me=(Test *)t; pthread_mutex_lock( &(me->mutex) ); for (int i=0;i<3;i++) { cout<<i<<":"<<me->s<<" ";cout.flush();sleep(1);} cout<<endl; pthread_mutex_unlock( &(me->mutex) ); static int ret=1; return &ret; } void *testThread2(void *t) { Test *me=(Test *)t; pthread_mutex_lock( &(me->mutex) ); for (int i=0;i<3;i++) { cout<<i<<":"<<me->s<<" ";cout.flush();sleep(1);} cout<<endl; pthread_mutex_unlock( &(me->mutex) ); static int ret=2; return &ret; } int main() { Test t; t.setString("SomeText"); cout<<"testThread1 starting"<<endl; cout<<"testThread2 starting"<<endl; pthread_t pthread1=t.start(testThread1); pthread_t pthread2=t.start(testThread2); // threads are now running void *ret1=t.join(pthread1); void *ret2=t.join(pthread2); cout<<"testThread1 joined with return value: "<<*((int *)ret1)<<endl; cout<<"testThread2 joined with return value: "<<*((int *)ret2)<<endl; }
1.4.2