DEV调试C++项目实例

//calc.cpp
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
 #include <error.h>
#include <iostream>
#include <sys/time.h>
#include <time.h>
 #include <memory.h>
#include <math.h>
#include <errno.h>

#include "emstcp.h"


int emsTcp::calcNumInit()
{
    int i,sum=0;
    for(i=0;i<=100;i++)
    {
        sum = sum+i;
    }
    return sum;
 }
//ems.cpp
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include"ems.h"

void cEms::emsrun()
{
    std::cout<<"esmrun is running!"<<std::endl;
}

void cEms::print()
{
    printf("emstcp get ems success!\n");
}

//ems.h
#ifndef _EMS_H
#define _EMS_H

class cEms
{
    public:
        cEms(){};
        ~cEms(){};
        void emsrun();

        void print();//其他类中调用时声明函数类型为public 

    private://私有类成员函数及变量声明 

};


#endif

//emstcp.cpp
#include <stdio.h>
#include <stdlib.h>
#include<signal.h>
#include<iostream>
#include<pthread.h>
#include<windows.h>
#include"emstcp.h"

//#include"sqlite3.h"
/***window 下避免在头文件下声明互斥锁,否则运行时会出现read or write内存错误***/
    pthread_mutex_t *mutex;
    pthread_mutex_t *mutex1;    

/***故障错误存日志***/
void emsTcp::setLog(char *strerr)
{
    const char *filename="emslog";


}

 void *emsTcp:: warning_work(void *arg)
{
    int *p=(int *)arg;

    std::cout<<"warning_work thread is run"<<" p="<<p[0]<<std::endl;
   }
void *emsTcp:: warning_flag(void *arg)
{        
    int *p=(int *)arg;
     emsTcp *ems_tcp =(emsTcp *)p[1];//声明一个类,这个类的属性指向自己的类 

    int sum;

    sum = ems_tcp->calcNumInit(); 

    std::cout<<"sum="<<sum<<endl;

    std::cout<<"warning_flag thread is run"<<" p="<<p[0]<<std::endl;
 }

/***devTrEq is running***/
void *emsTcp::devTrEq(void *arg)
{
    int *p = (int *)arg;
    cEms *ems = (cEms *)p[0];
    emsTcp *ems_tcp = (emsTcp *)p[1];

    ems->print();
    Sleep(1000);

    std::cout<<"devTrEq thread is running"<<endl;

}


/***tcprun is running***/
void emsTcp::tcprun(cEms *ems)
{    
    int ret;
     std::cout<<"tcprun"<<std::endl;
     unsigned int i=1;
     arg_tr[0] =(int)ems;
     arg_tr[1]=(int)this;
     char log[128]; 
     emsTcp *ems_tcp = (emsTcp *)arg_tr[1];


     pthread_t thread_id1,thread_id2,thread_id3;

     if((pthread_mutex_init(mutex,NULL))||(pthread_mutex_init(mutex1,NULL)))
     printf("pthread_mutex_init error!\n");

    pthread_create(&thread_id1,NULL,warning_flag,(void *)&arg_tr[0]);
    if(ret)
    {
         sprintf(log,"pthread_create thread1 start error\n",NULL);
        setLog(log); 
        perror("pthread_create thread1 error\n");
    }
     pthread_create(&thread_id2,NULL,warning_work,(void *)&arg_tr[0]);//传递函数的属性保持一致,都是void类型 
     pthread_create(&thread_id3,NULL,devTrEq,(void *)&arg_tr[0]);


    pthread_join(thread_id1,NULL);
     pthread_join(thread_id2,NULL);
     pthread_join(thread_id3,NULL);

     pthread_mutex_destroy(mutex);
    pthread_mutex_destroy(mutex1);
      /*原型:int  pthread_create((pthread_t  *thread,  pthread_attr_t  *attr,  void  *(*start_routine)(void  *),  void  *arg)
    用法:#include  <pthread.h>
    功能:创建线程(实际上就是确定调用该线程函数的入口点),在线程创建以后,就开始运行相关的线程函数。
    说明:thread:线程标识符;
    attr:线程属性设置;
    start_routine:线程函数的起始地址;
    arg:传递给start_routine的参数;
    返回值:成功,返回0;出错,返回-1
    */

     /*互斥锁初始化
   函数原型:int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_init()函数是以动态方式创建互斥锁的,参数attr指定了新建互斥锁的属性。如果参数attr为空,则使用默认的互斥锁属性,默认属性为快速互斥锁 。互斥锁的属性在创建锁的时候指定,在LinuxThreads实现中仅有一个锁类型属性,不同的锁类型在试图对一个已经被锁定的互斥锁加锁时表现不同。
    pthread_mutexattr_init()函数成功完成之后会返回零,其他任何返回值都表示出现了错误。
    函数成功执行后,互斥锁被初始化为未锁住态。
    */
}
//emstcp.h
#ifndef _EMSTCP_H
#define _EMSTCP_H
#include"ems.h"

using namespace std;
class emsTcp
{
    public:
        void tcprun(cEms *ems);
        int calcNumInit();
        void setLog(char *strerr); 

        emsTcp(){};
        ~emsTcp(){};
    private:


        int arg_tr[2];
        static void * warning_work(void *ptr);
        static void * warning_flag(void *ptr);
        static void * devTrEq(void *ptr);


};

#endif

//main.cpp
#include<stdio.h>
#include<stdlib.h>
#include"emstcp.h"


emsTcp *ems_tcp = NULL;
cEms *ems = NULL;

int main()
{
    printf("hello world!\n");


    ems = new cEms;
    ems->emsrun();

    ems_tcp = new emsTcp;
    ems_tcp->tcprun(ems);

    while(1);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值