歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C面向對象編程示例——線程調度

C面向對象編程示例——線程調度

日期:2017/3/1 10:05:15   编辑:Linux編程

/*
* 線程封裝演示-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();

switch(ch){
/*suspend*/
case 's':
case 'S':
thr_suspend(&thr);
break;
/*resume*/
case 'r':
case 'R':
thr_resume(&thr);
break;
/*cancel*/
case 'c':
case 'C':
thr_cancel(&thr);
goto EXIT_STEP;
/*exit*/
case 'e':
case 'E':
thr_exit(&thr);
goto EXIT_STEP;
/*exit*/
case 'q':
case 'Q':
exit(1);
};
}
EXIT_STEP:
thr_wait(&thr);
printf("---done---\r\n");
return 0;
}

Copyright © Linux教程網 All Rights Reserved