C面向对象编程示例

/*
 * 线程封装演示-C版本mythr.h
 * write by panzhh
 * 2011.12.24
 */
#ifndef MYTHR_H
#define MYTHR_H
#include <pthread.h>
typedef unsigned int bool;
#define true  1
#define false 0
typedef void (*RUN)(void *arg);
typedef struct __mythr
{
    RUN  run;        /*线程工作函数*/
    void *arg;        /*传给run的参数*/
    bool bthrun;    /*循环标志*/
    bool bthrSUSEnd;/*挂起标志*/

/*private item*/
    pthread_t tid;
    pthread_mutex_t mutex;
    pthread_cond_t cont;
}MYTHR, *PMYTHR;
int thr_start(PMYTHR pthr);    /*开启线程*/
int thr_wait(PMYTHR pthr);    /*等待线程*/
int thr_cancel(PMYTHR pthr);/*取消线程*/
void thr_exit(PMYTHR pthr);    /*退出线程*/
void thr_suspend(PMYTHR pthr);    /*挂起线程*/
void thr_resume(PMYTHR pthr);    /*恢复线程*/
#endif
/*
 * 线程封装演示-C版本mythr.c
 * write by panzhh
 * 2011.12.24
 */
#include <stdio.h>
#include <stdlib.h>
#include "mythr.h"
static void *routine(void *arg)
{
    PMYTHR pthr=(PMYTHR)arg;
    pthread_mutex_init(&pthr->mutex, NULL);
    pthread_cond_init (&pthr->cont, NULL);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    do{
        pthread_mutex_lock(&pthr->mutex);
        if(pthr->bthrsusend){
            if(0 != pthread_cond_wait (&pthr->cont, &pthr->mutex)){
                perror("work:pthread_cond_wait");
                return (void*)-1;
            }
        }
        pthread_mutex_unlock(&pthr->mutex);
        pthr->run(pthr->arg);
    }while(pthr->bthrun);
    pthread_cond_destroy(&pthr->cont);
    pthread_mutex_destroy(&pthr->mutex);
    return (void *)0;
}

int thr_start(PMYTHR pthr)
{
    if(NULL != pthr->run){
        return pthread_create(&pthr->tid, NULL, routine, (void *)pthr);
    }

return -1;
}

int thr_wait(PMYTHR pthr)
{
    if(NULL != pthr->run){
        return pthread_join(pthr->tid, NULL);
    }
    return -1;
}

int thr_cancel(PMYTHR pthr)
{
    if(NULL != pthr->run){
        pthread_cancel(pthr->tid);
        pthread_cond_destroy(&pthr->cont);
        pthread_mutex_destroy(&pthr->mutex);
        return 0;
    }
    return -1;
}

void thr_exit(PMYTHR pthr)
{
    if(NULL != pthr->run){
        pthread_mutex_lock(&pthr->mutex);
        if(pthr->bthrsusend){
            pthr->bthrsusend = false;
            pthread_cond_signal(&pthr->cont);
        }
        pthr->bthrun = false;
        pthread_mutex_unlock(&pthr->mutex);
    }
}

void thr_suspend(PMYTHR pthr)
{
    pthread_mutex_lock(&pthr->mutex);
    if(!pthr->bthrsusend){
        pthr->bthrsusend = true;
    }
    pthread_mutex_unlock(&pthr->mutex);
}

void thr_resume(PMYTHR pthr)
{
    pthread_mutex_lock(&pthr->mutex);
    if(pthr->bthrsusend){
        pthr->bthrsusend = false;
        pthread_cond_signal(&pthr->cont);
    }
    pthread_mutex_unlock (&pthr->mutex);
}
/*
 * 线程封装演示-C版本-测试代码
 * write by panzhh
 * 2011.12.24
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "mythr.h"

unsigned int k = 0;
void thrfunc(void *arg)
{
    printf("Thr---%s---%u\r\n", (char*)arg, k++);
    sleep(1);
}
MYTHR thr = {
    .run=thrfunc,        /*线程工作函数*/
    .arg="test",        /*传给run的参数*/
    .bthrun=true,        /*循环标志*/
    .bthrsusend=true,    /*挂起标志*/
};

int main(int argc, char **argv)
{
    thr_start(&thr);

//调度线程
    while(1){
        printf("Please Input:\r\n");
        char ch = getchar();

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/b67aaa12ff68be5624ac242ebde549cb.html