pthreads を使用して、C でコールバック メカニズムをエミュレートしようとしています。私が持っているコードは以下の通りです:
#include <stdio.h>
#include <pthread.h>
struct fopen_struct {
char *filename;
char *mode;
void *(*callback) (FILE *);
};
void *fopen_callback(FILE *);
void fopen_t(void *(*callback)(FILE *), const char *, const char *);
void *__fopen_t__(void *);
void fopen_t(void *(*callback)(FILE *), const char *filename, const char *mode) {
struct fopen_struct args;
args.filename = filename;
args.mode = mode;
args.callback = callback;
pthread_t thread;
pthread_create(&thread, NULL, &__fopen_t__, &args);
}
void *__fopen_t__(void *ptr) {
struct fopen_struct *args = (struct fopen_struct *)ptr;
FILE *result = fopen(args -> filename, args -> mode);
args -> callback(result);
}
int main() {
fopen_t(&fopen_callback, "test.txt", "r");
}
void *fopen_callback(FILE *stream) {
if (stream != NULL)
printf("Opened file successfully\n");
else
printf("Error\n");
}
これはコンパイルされますが、実行すると、画面にエラーやメッセージが表示されることなく完了します。私は何が欠けていますか?