28 POSIX Threads

1 What are Threads?

  1. 一个进程可以有多个线程
  2. 线程共享进程的资源,多进程是复制内存空间,彼此独立
  3. 线程被OS调度,意味着一个进程会使用超过100%的cpu(多核系统)

2 Hello POSIX Threads?

  1. POSIX became the standard interface for many system(C,Java,Python)

2.1 Creating a Thread

/* hello_pthread.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pthread.h>


void * hello_fun(void * args){

  printf("Hello World!\n");

  return NULL;
}

int main(int argc, char * argv[]){

  pthread_t thread;  //thread identifier

  //create a new thread have it run the function hello_fun
  pthread_create(&thread, NULL, hello_fun, NULL);

  //wait until the thread completes
  pthread_join(thread, NULL);

  return 0;
}
  1. pthread_create()创建线程,运行hello_fun,并用thread指针接收
  2. pthread_join()主线程阻塞,直到thread执行结束
  3. int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);

2.2 Passing Arguments to a Thread

/* hello_args_pthread.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pthread.h>


void * hello_arg(void * args){

  char * str = (char *) args;

  printf("%s", str);

  return NULL;
}

int main(int argc, char * argv[]){

  char hello[] = "Hello World!\n";

  pthread_t thread;  //thread identifier

  //create a new thread that runs hello_arg with argument hello
  pthread_create(&thread, NULL, hello_arg, hello);

  //wait until the thread completes
  pthread_join(thread, NULL);

  return 0;
}

2.3 Joining Threads

  1. thread的join很像process的wait
  2. 线程之间可以相互join
    3.当主线程退出时,其他所有线程自动销毁,所以不会产生僵尸和浪费资源
/* hello_pthread_bad.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pthread.h>


void * hello_fun(){

  printf("Hello World!\n");

  return NULL;
}

int main(int argc, char * argv[]){

  pthread_t thread;

  pthread_create(&thread, NULL, hello_fun, NULL);

  return 0;
}

2.4 Return values from threads

1.int pthread_join(pthread_t thread, void **retval);
2.类似于进程中的exit函数传递进程退出的状态,线程可以返回任何参数

2.5 Compiling POSIX threads

3 Threads and the OS

  1. 系统层面,pid和tid是相同的,既主线程和进程的id是相同的
  2. 线程内部tid和pid是不同的
/* hello_id_pthread.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>

#include <pthread.h>

//have to call syscall directly, no libc wrapper
pid_t gettid(){
  return (pid_t) syscall (SYS_gettid);
}

void * hello_fun(void * args){

  //retrieve the thread_id
  pthread_t thread = pthread_self();

  //print all identifying information
  printf("THREAD: TID:%d PID:%d PthreadID:%lu\n", gettid(), getpid(), thread);

  return NULL;
}

int main(int argc, char * argv[]){

  pthread_t thread;  //thread identifier


  //create a new thread have it run the function hello_fun
  pthread_create(&thread, NULL, hello_fun, NULL);

  //print all identifying information
  printf("MAIN: TID:%d PID:%d \n", gettid(), getpid());

  //wait until the thread completes
  pthread_join(thread, NULL);

  return 0;
}

3.2 Threads Running Like Processes

  1. C中每个线程可以占用一个cpu核

参考:
https://www.usna.edu/Users/cs/aviv/classes/ic221/s16/lec/28/lec.html#coderef-bad_ref

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值