c/c++函数指针

本文通过一个具体的C++函数指针示例,展示了如何在多线程环境中使用函数指针来实现灵活的线程行为控制。介绍了函数指针的定义、初始化及应用,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2014-03-29 星期六 10:31:48

c/c++函数指针demo

1、funcptr.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//funcptr demo
 
class CFuncPtr  //:public CThread
{
public:
  static CFuncPtr* createNew(int chn=0, int type=0);
   
public:
    static void *(*funcentry[8])(void *para);   //demo,没继承CThread,就这样了
    static void *ThreadEntry_00(void *para);
    static void *ThreadEntry_01(void *para);
    static void *ThreadEntry_10(void *para);
    static void *ThreadEntry_11(void *para);
    static void *ThreadEntry_20(void *para);
    static void *ThreadEntry_21(void *para);
    static void *ThreadEntry_30(void *para);
    static void *ThreadEntry_31(void *para);
    static void assignfunc();
 
protected:
    CFuncPtr(int chn,int type);
    ~CFuncPtr();
 
protected:
    void ThreadProc();
 
private:
    int m_chn;
    int m_type;
    int m_cnt;
    pthread_t m_pid;   
};
 
typedef  int (CFuncPtr::*EntryFuncPtr)(void *para);
2、funcptr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream> 
#include <iterator>
#include <vector>
#include <algorithm>
#include <utility> 
#include <string> 
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
#include <pthread.h>
 
#include "funcptr.hh"
 
#define TRY_EVALUATE_POINTER(pointer, value) do{\
    if(pointer)\
    {\
        *(pointer) = value;\
    }\
}while(0)
 
typedef void *(*ThreadEntryPtrType)(void *);
 
int CreateNormalThread(ThreadEntryPtrType entry, void *para, pthread_t *pid)
{
    pthread_t ThreadId;
    pthread_attr_t attr;
 
    pthread_attr_init(&attr);  
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    if(pthread_create(&ThreadId, &attr, entry, para) == 0)
    {
        pthread_attr_destroy(&attr);
        TRY_EVALUATE_POINTER(pid, ThreadId);
         
        return 0;
    }
 
    pthread_attr_destroy(&attr);
 
    return -1;
}
 
void mSleep(unsigned int  MilliSecond)
{
    struct timeval time;
    time.tv_sec = MilliSecond / 1000;           //seconds
    time.tv_usec = MilliSecond * 1000 % 1000000;//microsecond
    select(0, NULL, NULL, NULL, &time);
}
 
#define ENTYBODY()  {\
    CFuncPtr *pthis = (CFuncPtr *)para;\
    \
    if(!pthis)\
    {\
        exit(1);\
    }\
    pthis->ThreadProc();\
    return NULL;\
}
 
void *CFuncPtr::ThreadEntry_01(void *para)
{
    printf("line %d, %s, entry\n", __LINE__, __func__);
    ENTYBODY();
}
void *CFuncPtr::ThreadEntry_10(void *para)
{
    printf("line %d, %s, entry\n", __LINE__, __func__);
    ENTYBODY();
}
void *CFuncPtr::ThreadEntry_11(void *para)
{
    printf("line %d, %s, entry\n", __LINE__, __func__);
    ENTYBODY();
}
void *CFuncPtr::ThreadEntry_20(void *para)
{
    printf("line %d, %s, entry\n", __LINE__, __func__);
    ENTYBODY();
}
void *CFuncPtr::ThreadEntry_21(void *para)
{
    printf("line %d, %s, entry\n", __LINE__, __func__);
    ENTYBODY();
}
void *CFuncPtr::ThreadEntry_30(void *para)
{
    printf("line %d, %s, entry\n", __LINE__, __func__);
    ENTYBODY();
}
void *CFuncPtr::ThreadEntry_31(void *para)
{
    printf("line %d, %s, entry\n", __LINE__, __func__);
    ENTYBODY();
}
 
 
//CFuncPtr
 
//static void *(*funcentry[8])(void *para); //demo,没继承CThread,就这样了
 
void* (*CFuncPtr::funcentry[8])(void *para);
 
void CFuncPtr::assignfunc()
{
    CFuncPtr::funcentry[0] = CFuncPtr::ThreadEntry_00;
    CFuncPtr::funcentry[1] = CFuncPtr::ThreadEntry_01;
    CFuncPtr::funcentry[2] = CFuncPtr::ThreadEntry_10;
    CFuncPtr::funcentry[3] = CFuncPtr::ThreadEntry_11;
    CFuncPtr::funcentry[4] = CFuncPtr::ThreadEntry_20;
    CFuncPtr::funcentry[5] = CFuncPtr::ThreadEntry_21;
    CFuncPtr::funcentry[6] = CFuncPtr::ThreadEntry_30;
    CFuncPtr::funcentry[7] = CFuncPtr::ThreadEntry_31;
}
 
//仅demo
CFuncPtr::CFuncPtr(int chn, int type)
    : m_chn(chn)
    , m_type(type)
{
    EntryFuncPtr pEntryFunc;
 
    int num = m_chn*2 + m_type;
    CreateNormalThread(CFuncPtr::funcentry[num], (void *)this, &m_pid);
    printf("line %d, %s, num(%d)\n", __LINE__, __func__, num);
 
}
CFuncPtr::~CFuncPtr()
{
 
}
 
void *CFuncPtr::ThreadEntry_00(void *para)
{
    printf("line %d, %s, entry\n", __LINE__, __func__);
    CFuncPtr *pthis = (CFuncPtr *)para;
     
    if(!pthis)
    {
        exit(1);
    }
 
    pthis->ThreadProc();
    return NULL;
}
 
void CFuncPtr::ThreadProc()
{  
    printf("line %d, %s, entry\n", __LINE__, __func__);
    while(1)
    {
        mSleep(20);
        if(++m_cnt == 100)
        {  
            m_cnt = 0;
            printf("line %d, %s, running, chn(%d), type(%d)\n",
                __LINE__, __func__, m_chn, m_type);
        }
    }
}
 
CFuncPtr* CFuncPtr::createNew(int chn, int type)
{
    return new CFuncPtr(chn, type);
}
 
//g++ funcptr.cpp -lpthread
int main (int argc,char *argv[])
    //char* estr = "Hello,ni hao!";
    //std::cout<<"The count of CExample: "<<estr<<std::endl;
    CFuncPtr::assignfunc();
     
    CFuncPtr *pCFuncPtr[4][2];
    int chn,type;
    for(chn=0; chn<2; ++chn)
    {
        for(type=0; type<2; ++type)
        {
            pCFuncPtr[chn][type] = CFuncPtr::createNew(chn, type);
        }
    }
 
    while(1)
    {
        mSleep(200);
    };
     
    return 0;
}


相信通过这个demo,大家对函数指针应该有了比较清晰的认知。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值