69,381
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
//回调函数
void OutputSth(const char *str)
{
printf("%s\n", str);
}
//线程函数
void *ThreadRun(void (*func)())
{
const char str[20] = "hello callback.";
int i = 0;
for(; i < 10; ++i)
{
func(str);
sleep(1);
}
return NULL;
}
int main(void)
{
pthread_t thd;
pthread_create(&thd, NULL, ThreadRun, OutputSth);
pthread_join(thd, NULL);
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
struct param {
void (*func)(const char *);
};
//回调函数
void OutputSth(const char *str)
{
printf("%s\n", str);
//return NULL;
}
//线程函数
void *ThreadRun(void *arg)
{
const char str[20] = "hello callback.";
struct param *p = (struct param *)arg;
int i = 0;
for(; i < 10; ++i)
{
p->func(str);
sleep(1);
}
return NULL;
}
int main(void)
{
pthread_t thd;
struct param arg;
arg.func = OutputSth;
pthread_create(&thd, NULL, ThreadRun, (void *)&arg);
pthread_join(thd, NULL);
return 0;
}