[LC++]Posix threads

Laszlo Boszormenyi boszorme at mazsola.iit.uni-miskolc.hu
Tue Aug 28 22:54:05 UTC 2001


Hi!

I am new to threads, and trying to stop one with pthread_exit(); There
seems to be a trouble, as the program seems to be finished at that point
and does not execute 'delete PFirstThread'. How can I get man pages for
programming with pthreads under Debian?

Thanks, Laszlo
-------------- next part --------------
#include <iostream.h>
#include <pthread.h>
#include <syslog.h>
#include <unistd.h>

class FirstThread {
	public:
		pthread_t StoreThread;
		FirstThread(void);
		~FirstThread(void);
		friend void* Logging(void*);
};

FirstThread::FirstThread(void) {
	cout << "FTc" << endl;
}

FirstThread::~FirstThread(void) {
	cout << "FTd" << endl;
}

void* Logging(void* ptr) {
	cout << "FTl" << endl;
	for(int i=0;i<20;i++) {
		syslog(LOG_WARNING,"Thread logging!");
		sleep(1);
	}
//	exit(0);
	return 0;
}

int main(void) {

	daemon(0,1);

	FirstThread* PFirstThread=new FirstThread();
	openlog("Test for Ago",LOG_CONS,LOG_DAEMON);

	pthread_create(&PFirstThread->StoreThread,NULL,Logging,PFirstThread);

	for(int i=0;i<10;i++) {
		syslog(LOG_ERR,"Main logging!");
		sleep(2);
	}
	cout << "suxx" << endl;
	pthread_exit(&PFirstThread->StoreThread);
	cout << "suxx" << endl;

	syslog(LOG_ERR,"Logger ends.");
	sleep(1);
	closelog();

	delete PFirstThread;
	return 0;
}


More information about the tuxCPProgramming mailing list