-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcallback_demo3_pthread.c
More file actions
68 lines (49 loc) · 1.63 KB
/
callback_demo3_pthread.c
File metadata and controls
68 lines (49 loc) · 1.63 KB
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
#include<stdio.h>
//#include <pthread.h>
#include <stdlib.h>
typedef void (* def_func)(int n);
typedef struct param_s{
int a;
def_func call_back;
} param_t;
// 线程回调函数
void *thread_func(void *param)
{
param_t *p = (param_t *)param;
int b = 100; // 需要在此处得到的资源
printf("thread_func..\n");
// 此处 底层在耗时操作,得到相关资源, 然后才能调用用户提供的程序
sleep(5);
// 执行回调函数
p->call_back(p->a);
p->call_back(b); // 让用户调用我们本地的资源
printf("a = %d\n", p->a); // 我们调用用户的资源
}
// 注册函数 -- .dll
void Register_func(def_func func, int n)
{
pthread_t thrd;
param_t *param = (param_t *)malloc(sizeof(param_t));
param->a = n;
param->call_back = func;
pthread_create(&thrd, NULL, thread_func, (void *)param);
pthread_detach(thrd);
// pthread_join(thrd, NULL); // 会阻塞在这里
}
// 回调函数 --用户去写
// 尽管用户写了该函数,
// 却不是自己去调用,而是通过指针由底层的 注册函数去调用 称为回调函数
void func(int b){
printf("This is func.\t b = %d\n", b);
}
int main(int argc, const char *argv[])
{
int c = 10;
def_func real = func;
// 把用户写的函数注册上去
Register_func(real, c);
// 注册之后, 自己想干嘛干嘛, 5s时间到后,自然会操作func 函数, 即实现异步操作
printf("This is client func after Register_func\n");
pause(); // 查看异步操作
return 0;
}