这篇文章给大家带来线程控制的学习!!!
#include
typedef unsigned long int pthread_t;int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
创建一个线程,并且让它跑起来
#include
#include
#include
#include
using namespace std;// |---------------------------------------------------------------------|
// | #include |
// | int pthread_create(pthread_t *thread, const pthread_attr_t *attr, |
// | void *(*start_routine) (void *), void *arg); |
// |---------------------------------------------------------------------|void *CallPthread1(void *arg)
{printf("I am %s\n", (const char *)arg);// 指定线程退出状态值return (void *)0;
}void *CallPthread2(void *arg)
{printf("I am %s\n", (const char *)arg);// 指定线程退出状态值return (void *)0;
}int main()
{// 1、定义线程id,该id由OS填充pthread_t tid1;pthread_t tid2;// 创建线程,并且填充t1的id,arg参数给线程命名,并且让线程执行CallPthread代码if (pthread_create(&tid1, nullptr, CallPthread1, (void *)"thread t1") != 0){exit(EXIT_FAILURE);}if (pthread_create(&tid2, nullptr, CallPthread2, (void *)"thread t2") != 0){exit(EXIT_FAILURE);}return 0;
}
运行结果:
[lyh@192 Make_thread(1)]$ ./Thread_Creation
I am thread t1
I am thread t2
#include
#include
#include
#include
using namespace std;// |---------------------------------------------------------------------|
// | #include |
// | int pthread_create(pthread_t *thread, const pthread_attr_t *attr, |
// | void *(*start_routine) (void *), void *arg); |
// |---------------------------------------------------------------------|void *CallPthread1(void *arg)
{printf("I am %s, thread PID: %d\n", (const char *)arg, getpid());sleep(3);// 指定线程退出状态值return (void *)0;
}void *CallPthread2(void *arg)
{printf("I am %s, thread PID: %d\n", (const char *)arg, getpid());sleep(3);// 指定线程退出状态值return (void *)0;
}int main()
{// 1、定义线程id,该id由OS填充pthread_t tid1;pthread_t tid2;// 创建线程,并且填充t1的id,arg参数给线程命名,并且让线程执行CallPthread代码if (pthread_create(&tid1, nullptr, CallPthread1, (void *)"thread t1") != 0){exit(EXIT_FAILURE);}if (pthread_create(&tid2, nullptr, CallPthread2, (void *)"thread t2") != 0){exit(EXIT_FAILURE);}while (true){cout << "我是主线程, pid: : " << getpid() << endl;sleep(1);}return 0;
}

线程库NPTL提供了pthread_ self函数,可以获得线程自身的ID:
#include
typedef unsigned long int pthread_t;pthread_t pthread_self(void);
#include
#include
#include
#include
using namespace std;void *CallPthread1(void *arg)
{// 获取线程idpthread_t id = pthread_self();printf("I am %s, pthread PID: %u\n", (const char *)arg, id);// 指定线程退出状态值return (void *)0;
}void *CallPthread2(void *arg)
{// 获取线程idpthread_t id = pthread_self();printf("I am %s, pthread PID: %u\n", (const char *)arg, id);// 指定线程退出状态值return (void *)0;
}int main()
{// 1、定义线程id,该id由OS填充pthread_t tid1;pthread_t tid2;// 创建线程,并且填充t1的id,arg参数给线程命名,并且让线程执行CallPthread代码if (pthread_create(&tid1, nullptr, CallPthread1, (void *)"thread t1") != 0){exit(EXIT_FAILURE);}if (pthread_create(&tid2, nullptr, CallPthread2, (void *)"thread t2") != 0){exit(EXIT_FAILURE);}return 0;
}
运行结果:
[lyh@192 Make_thread(1)]$ ./Thread_Creation
I am thread t1, pthread PID: 3796985600
I am thread t2, pthread PID: 3788592896
如果需要只终止某个线程而不终止整个进程
pthread_exit函数:
#include
typedef unsigned long int pthread_t;void pthread_exit(void *value_ptr);
#include
#include
#include
#include
using namespace std;void *CallPthread1(void *arg)
{// 获取线程idpthread_t id = pthread_self();printf("I am %s, pthread PID: %u\n", (const char *)arg, id);// 线程终止,线程退出后的值设为空pthread_exit(nullptr);
}void *CallPthread2(void *arg)
{// 获取线程idpthread_t id = pthread_self();printf("I am %s, pthread PID: %u\n", (const char *)arg, id);pthread_exit(nullptr);
}int main()
{// 1、定义线程id,该id由OS填充pthread_t tid1;pthread_t tid2;// 创建线程,并且填充t1的id,arg参数给线程命名,并且让线程执行CallPthread代码if (pthread_create(&tid1, nullptr, CallPthread1, (void *)"thread t1") != 0){exit(EXIT_FAILURE);}if (pthread_create(&tid2, nullptr, CallPthread2, (void *)"thread t2") != 0){exit(EXIT_FAILURE);}return 0;
}
pthread_cancel函数:
#include
typedef unsigned long int pthread_t;int pthread_cancel(pthread_t thread);
#include
#include
#include
#include
using namespace std;void *CallPthread1(void *arg)
{// 获取线程idpthread_t id = pthread_self();while (true){printf("I am %s, pthread PID: %u\n", (const char *)arg, id);sleep(1);}pthread_exit(nullptr);
}void *CallPthread2(void *arg)
{// 获取线程idpthread_t id = pthread_self();while (true){printf("I am %s, pthread PID: %u\n\n", (const char *)arg, id);sleep(1);}pthread_exit(nullptr);
}int main()
{// 1、定义线程id,该id由OS填充pthread_t tid1;pthread_t tid2;// 创建线程,并且填充t1的id,arg参数给线程命名,并且让线程执行CallPthread代码if (pthread_create(&tid1, nullptr, CallPthread1, (void *)"thread t1") != 0){exit(EXIT_FAILURE);}if (pthread_create(&tid2, nullptr, CallPthread2, (void *)"thread t2") != 0){exit(EXIT_FAILURE);}// 二秒后退出ID为tid1的线程sleep(2);pthread_cancel(tid1);return 0;
}
运行结果:

#include
typedef unsigned long int pthread_t;int pthread_join(pthread_t thread, void **value_ptr);
#include
#include
#include
#include
using namespace std;void *CallPthread1(void *arg)
{// 获取线程idpthread_t id = pthread_self();printf("I am %s, pthread PID: %u\n", (const char *)arg, id);// p是一个线程状态返回值,传给pthread_exit参数const char* p = "线程退出成功1! ! !";pthread_exit((void*)p);
}void *CallPthread2(void *arg)
{// 获取线程idpthread_t id = pthread_self();printf("I am %s, pthread PID: %u\n", (const char *)arg, id);const char* p = "线程退出成功2! ! !";pthread_exit((void*)p);
}int main()
{// 1、定义线程id,该id由OS填充pthread_t tid1;pthread_t tid2;// 创建线程,并且填充t1的id,arg参数给线程命名,并且让线程执行CallPthread代码if (pthread_create(&tid1, nullptr, CallPthread1, (void *)"thread t1") != 0){exit(EXIT_FAILURE);}if (pthread_create(&tid2, nullptr, CallPthread2, (void *)"thread t2") != 0){exit(EXIT_FAILURE);}void* RV1;void* RV2;// 等待线程退出,并且获取线程退出时的返回值pthread_join(tid1, &RV1);pthread_join(tid2, &RV2);cout << (const char*)RV1 << endl;cout << (const char*)RV2 << endl;return 0;
}
运行结果:
[lyh@192 Make_thread(1)]$ make
g++ -o Thread_Creation Thread_Creation.cpp -std=c++11 -lpthread[lyh@192 Make_thread(1)]$ ./Thread_Creation
I am thread t1, pthread PID: 2257434368
I am thread t2, pthread PID: 2249041664
线程退出成功1! ! !
线程退出成功2! ! !
调用该函数的线程将挂起等待,直到id为thread的线程终止
thread线程以不同的方法终止,通pthread_join得到的终止状态是不同的

#include
typedef unsigned long int pthread_t;int pthread_detach(pthread_t thread);
上一篇:【stl容器--实践操作】