Thread

本文介绍了多线程程序的基本概念,定义了线程为进程内的控制序列,并详细说明了使用pthread库进行线程操作的方法。此外,还解释了如何通过定义_REENTRANT宏来启用多线程支持,并给出了创建新线程的示例。

Multiple strands of execution in a single program are called threads.

A more precise definition is that a thread is a sequence of control within a process.

There is a whole set of library calls associated with threads, most of whose names start with pthread. To use these library calls, you must define the macro _REENTRANT , include the file pthread.h , and link with the threads library using -lpthread .

In multithreaded programs, you tell the compiler that you need this feature by defining the _REENTRANT macro before any #include lines in your program. This does three things, and does them so elegantly that usually you don't even need to know what was done:

  • some functions get prototypes for a re-entrant safe equivalant. These are normally the same function name, but with _r appended so that, for example, gethostbyname is changed to gethostbyname_r .
  • some stdio.h functions that are normally implemented as macros become proper re-entrant safe functions.
  • the variable errno , from errno.h , is changed to call a function, which can determine the real errno value in a multithread safe way.

Create a new thread, much like fork creates a new process.

#include <pthread.h>

int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);

上一篇: Process 下一篇: Signal
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值