//pool.h
1 #ifndef POOL_H
#define POOL_H
#include <pthread.h> class pool
{
public:
pool();
~pool(); void func();
static void *schedule(void *ptr);
int schedule_thread();
/*
private:
pthread_t pid;
void *params;
*/
}; #endif // POOL_H
//pool.cpp
1 #include <iostream>
#include <stdio.h>
#include <pthread.h>
#include "pool.h"
using namespace std;
pool::pool()
{
cout << "pool()" << endl;
} pool::~pool()
{
cout << "~pool()" << endl;
} void pool::func(){
cout << "pool::func" << endl;
} //void *pool::schedule(void *ptr){
void *pool::schedule(void *ptr){
cout << "enter pool::schedule" << endl;
//unjoinable属性可以在pthread_create时指定,或在线程创建后在线程中pthread_detach自己,
int res = pthread_detach(pthread_self());
if(res != ){
printf("please check /proc/%ld/maps\n", pthread_self());
}
pool *p = (pool *)ptr;
p->func();
cout << "leave schedule" << endl;
return ;
} int pool::schedule_thread(){
cout << "enter pool::schedule_thread" << endl;
pthread_t pid;
//if(pthread_create(&pid , NULL ,schedule, (void *)this) != 0){
if(pthread_create(&pid , NULL ,schedule, (void *)this) != ){
cerr << "schedule thread create fail" << endl;
return -;
}
cout << "leave pool::schedule_thread" << endl;
return ;
}
//main.cpp
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pool.h"
int main(void)
{
pool *pl = new pool();
// pl.func();
pool::schedule(pl);
//pl.schedule_thread();
delete pl;
return ;
}

pthread_detach pthread_create实例-LMLPHP

05-15 08:53