linux编译文件

编译带线程的.c文件

// POSIX 线程
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
 void *thread_function(void *arg) {
  int i;
  for ( i=0; i<20; i++) {
    printf("Thread says hi!\n");
    sleep(1);
  }
  return NULL;
}
int main(void) {
  pthread_t mythread;
  
  if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {
    printf("error creating thread.");
    abort();
  }
  if ( pthread_join ( mythread, NULL ) ) {
    printf("error joining thread.");
    abort();
  }
  exit(0);
}

终端命令

gcc thread1.c -o thread1 -lpthread

终端运行

$ ./thread1

版权声明:本文为weixin_41602564原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。