信号量

子线程被阻塞,主线程可以激活,这就是线程的同步问题。
信号量就可以实现线程同步

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <semaphore.h>
  6. char buf[1024] = {0};
  7. sem_t sem;
  8. int flag = 0;
  9. void *func(void *arg)
  10. {
  11. sem_wait(&sem);
  12. while(!flag)
  13. {
  14. printf("count: %d\n", strlen(buf));
  15. memset(buf, 0, sizeof(buf));
  16. sem_wait(&sem);
  17. }
  18. pthread_exit(NULL);
  19. }
  20. int main(int argc, char **argv)
  21. {
  22. int ret = -1;
  23. pthread_t th = -1;
  24. sem_init(&sem, 0, 0);
  25. ret = pthread_create(&th, NULL, func, NULL);
  26. if(ret != 0)
  27. {
  28. printf("pthread_create failed");
  29. exit(-1);
  30. }
  31. printf("please input: \n");
  32. while (scanf("%s", buf))
  33. {
  34. if(!strncmp(buf, "end", 3))
  35. {
  36. printf("process end\n");
  37. flag = 1;
  38. sem_post(&sem);
  39. break;
  40. }
  41. sem_post(&sem);
  42. }
  43. printf("wait for recycle thread\n");
  44. ret = pthread_join(th, NULL);
  45. if(ret != 0)
  46. {
  47. printf("pthread_create failed");
  48. exit(-1);
  49. }
  50. printf("finish recycle thread\n");
  51. sem_destroy(&sem);
  52. return 0;
  53. }

互斥锁

互斥锁又叫做互斥量mutex
互斥锁: 对共享数据进行锁定,保证同一时刻只能有一个线程去操作。
注意: 互斥锁是多个线程一起去抢,抢到锁的线程先执行,没有抢到锁的线程需要等待,等互斥锁使用完释放后,其它等待的线程再去抢这个锁。
相关函数:
pthread_mutex_init 功能:初始化一个互斥锁
pthread_mutex_destroy 功能:销毁一个互斥锁
pthread_mutex_lock 功能:加锁
pthread_mutex_unlock 功能:尝试加锁

互斥锁和信号量的关系:可以认为互斥锁是一种特殊的信号量

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. char buf[1024] = {0};
  6. pthread_mutex_t mutex;
  7. int flag = 0;
  8. void *func(void *arg)
  9. {
  10. sleep(1);
  11. while(!flag)
  12. {
  13. pthread_mutex_lock(&mutex);
  14. printf("count: %d\n", strlen(buf));
  15. memset(buf, 0, sizeof(buf));
  16. pthread_mutex_unlock(&mutex);
  17. sleep(1);
  18. }
  19. pthread_exit(NULL);
  20. }
  21. int main(int argc, char **argv)
  22. {
  23. int ret = -1;
  24. pthread_t th = -1;
  25. pthread_mutex_init (&mutex, NULL);
  26. ret = pthread_create(&th, NULL, func, NULL);
  27. if(ret != 0)
  28. {
  29. printf("pthread_create failed");
  30. exit(-1);
  31. }
  32. printf("please input: \n");
  33. while (1)
  34. {
  35. pthread_mutex_lock(&mutex);
  36. scanf("%s", buf);
  37. pthread_mutex_unlock(&mutex);
  38. if(!strncmp(buf, "end", 3))
  39. {
  40. printf("process end\n");
  41. flag = 1;
  42. break;
  43. }
  44. sleep(1);
  45. }
  46. printf("wait for recycle thread\n");
  47. ret = pthread_join(th, NULL);
  48. if(ret != 0)
  49. {
  50. printf("pthread_join failed");
  51. exit(-1);
  52. }
  53. printf("finish recycle thread\n");
  54. pthread_mutex_destroy(&mutex);
  55. return 0;
  56. }

条件变量

相关函数:
pthread_cond_init
pthread_cond_destory
pthread_cond_wait
pthread_cond_signal/pthread_cond_broadcast

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. char buf[1024] = {0};
  6. pthread_mutex_t mutex;
  7. pthread_cond_t cond;
  8. int flag = 0;
  9. void *func(void *arg)
  10. {
  11. while(!flag)
  12. {
  13. pthread_mutex_lock(&mutex);
  14. pthread_cond_wait(&cond, &mutex);
  15. printf("count: %d\n", strlen(buf));
  16. memset(buf, 0, sizeof(buf));
  17. pthread_mutex_unlock(&mutex);
  18. //sleep(1);
  19. }
  20. pthread_exit(NULL);
  21. }
  22. int main(int argc, char **argv)
  23. {
  24. int ret = -1;
  25. pthread_t th = -1;
  26. pthread_mutex_init(&mutex, NULL);
  27. pthread_cond_init(&cond, NULL);
  28. ret = pthread_create(&th, NULL, func, NULL);
  29. if(ret != 0)
  30. {
  31. printf("pthread_create failed");
  32. exit(-1);
  33. }
  34. printf("please input: \n");
  35. while (1)
  36. {
  37. //pthread_mutex_lock(&mutex);
  38. scanf("%s", buf);
  39. pthread_cond_signal(&cond);
  40. pthread_mutex_unlock(&mutex);
  41. if(!strncmp(buf, "end", 3))
  42. {
  43. printf("process end\n");
  44. flag = 1;
  45. break;
  46. }
  47. //sleep(1);
  48. }
  49. printf("wait for recycle thread\n");
  50. ret = pthread_join(th, NULL);
  51. if(ret != 0)
  52. {
  53. printf("pthread_create failed");
  54. exit(-1);
  55. }
  56. printf("finish recycle thread\n");
  57. pthread_mutex_destroy(&mutex);
  58. pthread_cond_destroy(&cond);
  59. return 0;
  60. }