pthread create utilizzarlo dentro una classe
Von: mitic (mit@libero.it) [Profil]
Datum: 29.02.2008 11:04
Message-ID: <fq8lbn$2juj$1@newsreader2.mclink.it>
Newsgroup: it.comp.os.linux.development
Datum: 29.02.2008 11:04
Message-ID: <fq8lbn$2juj$1@newsreader2.mclink.it>
Newsgroup: it.comp.os.linux.development
vorrei creare una classe che ha un metodo run e nel costruttore di questa
classe far partire un thread collegato a questo metodo.
utilizzando pthread create è possibile?
qualcuno mi può postare parte del codice
ho trovato solo questo esempio che usa le classi,
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <strings.h>
#include <pthread.h>
#include <iostream.h>
//This C++ class is an abstract representation of a pair of integer numbers.
class pairint{
public:
pairint(int, int);
~pairint() {};
void add();
void printpair();
private:
int _x;
int _y;
};
pairint::pairint(int x, int y) {
_x = x;
_y = y;
};
void pairint::add() {
cout << "sum of pair is "<< _x+_y << "\n";
};
void pairint::printpair() {
cout << "value of x is " << _x << "\n";
cout << "value of y is " << _y << "\n";
};
extern "C" void *ThreadStartup(void *);
int main(int argc, char **argv)
{
pairint *t=NULL;
pthread_t thread;
int rc;
t = new pairint(5,10);
rc = pthread_create(&thread, NULL, ThreadStartup, (void *)t);
if (rc) {
cout << "Failed to create a thread" << "\n";
exit(EXIT_FAILURE);
}
printf("Waiting for thread to complete\n");
rc = pthread_join(thread, NULL);
if (rc) {
cout << "Failed to joint a thread" << "\n";
exit(EXIT_FAILURE);
}
cout << "Tadah" << "\n";
exit(EXIT_SUCCESS);
}
// This function is a helper function. It has normal C linkage, and is
// the base for newly created pairint objects. It runs the
// add method on the pairint object passed to it (as a void *).
// After this method completes normally (i.e returns),
// we delete the object.
void *ThreadStartup(void *_tgtObject) {
pairint *tgtObject = (pairint *)_tgtObject;
tgtObject->add();
delete tgtObject;
return (NULL);
}
[ Auf dieses Posting antworten ]Antworten
- mitic (29.02.2008 11:10)
