0

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");
}

これはコンパイルされますが、実行すると、画面にエラーやメッセージが表示されることなく完了します。私は何が欠けていますか?

4

2 に答える 2

3

の完了前にスレッドmainが終了してい__fopen_t__ます。したがって、そのスレッド(fopen_t) using を切り離して他の便利なことを行うか、 usingpthread_detachの完了を待ちます。__fopen_t__pthread_join

を使用するpthread_joinと、次のfopen_tようになります。

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);
    pthread_join( thread, NULL );   // Waiting till the new thread completes
}

詳細については、man ページpthread_detachおよびpthread_joinを参照してください。


R..のコメントに従ってより論理的にするために、動的割り当てを使用したコードを以下に示します。

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

struct fopen_struct {
    char *filename;
    char *mode;
    void *(*callback) (FILE *);
};

void *fopen_callback(FILE *);
pthread_t* fopen_t(void *(*callback)(FILE *), const char *, const char *);
void *__fopen_t__(void *);

// returns pthread_t* to be freed by caller
pthread_t* fopen_t(void *(*callback)(FILE *), const char *filename, const char *mode)
{

    struct fopen_struct *args = calloc( 1, sizeof(  struct fopen_struct ) );
    args->filename = filename;
    args->mode = mode;
    args->callback = callback;

    pthread_t *thread = calloc( 1, sizeof( pthread_t ) );   // Need error checks
    pthread_create( thread, NULL, &__fopen_t__, args);
    //pthread_join( thread, NULL ); // `thread` is returned to caller

    return thread;
}

// takes `struct fopen_struct*` as argument and will be freed
void *__fopen_t__(void *ptr) {
    struct fopen_struct *args = (struct fopen_struct *)ptr;
    FILE *result = fopen(args -> filename, args -> mode);
    args -> callback(result);

    free( args ); args = NULL;
    return NULL;
}

int main() {
    pthread_t *th_id = NULL;

    th_id = fopen_t(&fopen_callback, "test.txt", "r");      // Need error checks
    pthread_join( *th_id, NULL );  // Wait till the __fopen_t__ thread finishes
    free( th_id ); th_id = NULL;

    return 0;
}
于 2013-06-19T08:17:48.520 に答える