线程可以说是轻型的进程
多线程共享进程的地址空间和资源使得线程的上下文切换优于进程
由于线程共享资源就会有抢占资源的情况主要的手段有 互斥锁 ,条件变量 ,信号量等等
简单回顾下C++ 多线程
#include <pthread.h>
pthread_create (thread,attr, start_routine, arg)
创建线程函数
#include <iostream>
#include <cstdlib>
#include <pthread.h>
using namespacestd;
#defineNUM_THREADS 5
void *PrintHello(void*threadid)
{
long tid;
tid = (long)threadid;
cout << "Hello World! Thread ID," << tid << endl;
pthread_exit(NULL);
}
int main ()
{
pthread_t threads[NUM_THREADS];
int rc;
int i;
for( i=0; i < NUM_THREADS; i++ ){
cout << "main() : creatingthread, " << i << endl;
rc = pthread_create(&threads[i], NULL,
PrintHello, (void *)i);
if (rc){
cout << "Error:unabl