评论

收藏

[C++] c线程传递多个参数,使用结构体

编程语言 编程语言 发布于:2021-08-07 13:09 | 阅读数:296 | 评论:0

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include "header-demo.h"
void display(void *couple);
typedef struct Persons {
  int age;
  char *husband;
  char *wife;
} Couple;
int main() {
  pthread_t pthread;
  Couple *couple = (Couple *)alloca(sizeof(Couple));
  couple->age = 10;
  couple->wife = "zhu";
  couple->husband = "guan";
//  strcpy(couple->husband, "guan");
//  strcpy(couple->wife, "zhu");
  pthread_create(&pthread, NULL, display, (void *)couple);
  pthread_join(&pthread, NULL);
  return 0;
}
void display(void *couple) {
  Couple *couple1 = (Couple *)couple;
  printf("age:%d, name1: %s, name2:%s\n", couple1->age, couple1->husband, couple1->wife);
}


关注下面的标签,发现更多相似文章