文章目录
一 线程创建
1. 线程创建
增加链接选项: g++ -lpthread 或者 g++ -pthread (根据编译器版本而定)
#include <iostream>
using namespace std;
extern "C"
{
/* int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
*/
#include <unistd.h>
#include <pthread.h>
}
/*说明两个函数
pthread_self ,获取当前线程ID
pthread_equal,比较两个线程ID是否相等(因为有的平台pthread_t 类型可能是结构体
所以单独封装了一个比较函数
*/
void printids(char const* host)
{
cout << host << ": Pid = " << getpid() << " Thread Id = "
<< pthread_self() << endl;
}
void* thread_func(void *arg)
{
printids("new thread");
cout << "thread_func arg = " << *(int *)arg << endl;
}
int main(void)
{
/*main thread*/
printids("main thread");
/*thread create*/
pthread_t thread;
int arg = 20;
pthread_create(&thread,NULL,thread_func,&arg);
/*wait for new thread exit, then main thread exit*/
sleep(20);
return 0;
}
2. 线程终止
2.1 线程主动终止
- 线程函数 return
- 调用 pthread_exit
#include <iostream>
using namespace std;
extern "C"
{
#include <unistd.h>
#include <pthread.h>
}
static int s_val1 =100;
static int s_val2 = 99;
void printids(char const* host)
{
cout << host << ": Pid = " << getpid() << " Thread Id = "
<< pthread_self() << endl;
}
void* thread_func_return(void *arg)
{
printids("return from thread");
return &s_val1;
}
void* thread_func_pexit(void *arg)
{
printids("pthread_exit from thread");
pthread_exit(&s_val2);
}
int main(void)
{
/*main thread*/
printids("main thread");
/*thread create*/
pthread_t thread1;
pthread_t thread2;
int arg = 20;
pthread_create(&thread1,NULL,thread_func_return,&arg);
pthread_create(&thread2,NULL,thread_func_pexit,&arg);
/*wait for new thread exit, then main thread exit*/
void* pret;
pthread_join(thread1,&pret);
cout << "thread1 return val = " << *(int*)pret << endl;
pthread_join(thread2,&pret);
cout << "thread2 exit val = " << *(int*)pret << endl;
return 0;
}
2.2 让对等线程终止
-
存在对等关系的线程A可以让线程B终止,通过函数 pthread_cancel
-
线程可以设置属性,是否可以被对等线程终止,通过函数pthread_setcancelstate
fool线程结束,bar线程继续
#include <iostream>
using namespace std;
extern "C"
{
#include <unistd.h>
#include <pthread.h>
}
void* fool(void *)
{
int oldcancelstate = 0;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldcancelstate);
while(1)
{
cout << "fool thread" << endl;
sleep(1);
}
}
void* bar(void *)
{
int oldcancelstate = 0;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&oldcancelstate);
while(1)
{
cout << "bar thread" << endl;
sleep(1);
}
}
int main(void)
{
pthread_t thread1;
pthread_t thread2;
pthread_create(&thread2,NULL,bar,NULL);
pthread_create(&thread1,NULL,fool,NULL);
sleep(2);
pthread_cancel(thread1);
pthread_cancel(thread2);
while(1)
{
sleep(1);
}
return 0;
}
2.3 进程结束
Linux exit函数会终止所有与进程相关的线程
二 线程汇合/分离
1. 线程汇合(等待线程终止)
线程等待指定的线程退出,释放线程资源,并获取线程退出指针;
#include <iostream>
using namespace std;
extern "C"
{
#include <unistd.h>
#include <pthread.h>
}
static int fool_ret = 99;
void* fool(void *arg)
{
int oldcancelstate = 0;
cout << "fool thread func" << endl;
return &fool_ret;
}
int main(void)
{
pthread_t thread1;
pthread_create(&thread1,NULL,fool,NULL);
void* retval = NULL;
if( 0 == pthread_join(thread1,&retval))
{
cout << "Fool thread exited with retval " << *(int*)retval
<< endl;
}
return 0;
}
2. 线程分离
设置为分离状态的线程,线程结束时自动释放资源,无须使用pthread_join
#include <iostream>
using namespace std;
extern "C"
{
#include <unistd.h>
#include <pthread.h>
}
static int fool_ret = 99;
void* fool(void *arg)
{
pthread_detach(pthread_self());
cout << "detached fool thread" << endl;
return NULL;
}
int main(void)
{
pthread_t thread1;
pthread_create(&thread1,NULL,fool,NULL);
sleep(10);
return 0;
}
3. 被detach的线程,再使用pthread_join 会发生什么?
结果是:pthread_join函数返回失败
#include <iostream>
using namespace std;
extern "C"
{
#include <unistd.h>
#include <pthread.h>
}
static int fool_ret = 99;
void* fool(void *arg)
{
pthread_detach(pthread_self());
cout << "detached fool thread" << endl;
while(1)
{
sleep(10);
}
return NULL;
}
int main(void)
{
pthread_t thread1;
pthread_create(&thread1,NULL,fool,NULL);
sleep(1);
if(0 == pthread_join(thread1,NULL))
{
}
else
{
cout << "join failed" << endl;
}
while(1)
{
sleep(1);
}
return 0;
}
线程管理精要
883

被折叠的 条评论
为什么被折叠?



