基本使用

  1. #include <string>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include "testThread.h"
  5. #include "mylog.h"
  6. using namespace std;
  7. //自定义一个函数,异步任务使用
  8. void *customPThreadMethod(void *value) {
  9. int tempValue = *static_cast<int *>(value); //接收从外部传入的值
  10. LOG("accept value from out method is %d", tempValue);
  11. for (int i = 0; i < 10; ++i) {
  12. sleep(1);
  13. LOG("customPThreadMethod:%d", i + 1);
  14. }
  15. return 0;
  16. }
  17. void main_testThread() {
  18. //线程基本使用
  19. // int pthread_create(pthread_t *__pthread_ptr, pthread_attr_t const *__attr,
  20. // void *(*__start_routine)(void *), void *);
  21. pthread_t pThreadID; //函数指针
  22. int value = 1000;
  23. pthread_create(&pThreadID, 0, customPThreadMethod, &value);
  24. pthread_join(pThreadID, 0); //会等待线程执行完毕之后,才会执行后面的代码
  25. LOG("after thread");
  26. }

使用升级

分离线程非分离线程 非分离线程,要等到耗时任务执行全部完成以后,先执行异步任务,才会执行join后面关联的代码逻辑

分离线程,各玩各的,老死不相往来,所以就造成了,不管异步任务是否执行完毕,该结束就结束

  1. #include <string>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include "testThread.h"
  5. #include "mylog.h"
  6. using namespace std;
  7. //自定义一个函数,异步任务使用
  8. void *customPThreadMethod(void *value) {
  9. int tempValue = *static_cast<int *>(value);
  10. // int tempValue = *((int *)value);
  11. LOG("accept value from out method is %d", tempValue);
  12. for (int i = 0; i < 10; ++i) {
  13. sleep(1);
  14. LOG("customPThreadMethod:%d", i + 1);
  15. }
  16. return 0; //必须返回一个值,不然程序会闪退
  17. }
  18. void main_testThread() {
  19. pthread_t pThreadID; //线程ID,允许有野指针
  20. pthread_attr_t pThreadAttr; //线程属性,不允许有野指针 有坑
  21. //初始化:线程属性
  22. pthread_attr_init(&pThreadAttr);
  23. //开启分离线程,pthread_join就失效了
  24. // pthread_attr_setdetachstate(&pThreadAttr, PTHREAD_CREATE_DETACHED);
  25. //开启非分离线程和pthread_join关联起来了
  26. pthread_attr_setdetachstate(&pThreadAttr, PTHREAD_CREATE_JOINABLE);
  27. int value = 1000;
  28. pthread_create(&pThreadID, &pThreadAttr, customPThreadMethod, &value);
  29. //会等待线程执行完毕之后,才会执行后面的代码
  30. pthread_join(pThreadID, 0);
  31. //线程属性,进行回收
  32. pthread_attr_destroy(&pThreadAttr);
  33. LOG("after thread");
  34. }

线程安全互斥锁

  1. #include <string>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include <queue>
  5. #include "mylog.h"
  6. #include "testUpdateThread.h"
  7. using namespace std;
  8. queue<int> saveAllData;
  9. pthread_mutex_t mutex; //互斥锁
  10. //自定义一个函数,异步任务使用
  11. void *customPThreadUpdateMethod(void *value) {
  12. //线程加锁
  13. pthread_mutex_lock(&mutex);
  14. int tempValue = *static_cast<int *>(value);
  15. // int tempValue = *((int *)value);
  16. LOG("accept value from out method is %d", tempValue);
  17. if (!saveAllData.empty()) {
  18. LOG("获取队列的数据:%d\n", saveAllData.front());
  19. saveAllData.pop(); // 把数据弹出去,删除的意思
  20. } else {
  21. LOG("队列中没有数据了\n");
  22. }
  23. // 解除锁定,是为了让其他多线程,可以进来的操作,这就是解锁的目的
  24. pthread_mutex_unlock(&mutex);
  25. return 0; //必须返回一个值,不然程序会闪退
  26. }
  27. void main_testUpdateThread(){
  28. pthread_mutex_init(&mutex, nullptr);
  29. for (int i = 1000; i < 10010; ++i) {
  30. saveAllData.push(i);
  31. }
  32. pthread_t pThreadID[10];
  33. for (int i = 0; i < 10; ++i) {
  34. pthread_create(&pThreadID[i], 0, customPThreadUpdateMethod, &i);
  35. pthread_join(pThreadID[i], 0);
  36. }
  37. LOG("函数已经执行到了这里");
  38. //回收互斥锁
  39. pthread_mutex_destroy(&mutex);
  40. }