linux 线程属性 线程同步

线程属性:man pthread_attr_  (按两次Tab,会出现相关属性)

复制代码
 1 /*
 2     int pthread_attr_init(pthread_attr_t * attr);
 3         - 初始化线程属性变量
 4     int pthread_attr_destroy(pthread_attr_t * attr);
 5         - 释放线程属性的资源
 6     int pthread_attr_getdetachstate(const pthread_attr_t attr,int * detachstate);
 7         - 获取线程分离的状态属性
 8     int pthread_attr_setdetachstate(pthread_attr_t * attr, int detachstate);
 9         - 设置线程分离的状态属性
10 */
11 #include <stdio.h>
12 #include <pthread.h>
13 #include <unistd.h>
14 #include <string.h>
15 void * callback(void * arg)
16 {
17     printf("child thread id: %ld\n",pthread_self());
18     return NULL;
19 }
20 int main()
21 {
22     //创建一个线程属性变量
23     pthread_attr_t attr;
24     //初始化属性变量
25     pthread_attr_init(&attr);
26     //设置属性
27     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//表示设置线程分离
28     //创建一个子线程
29     pthread_t tid;
30     int ret = pthread_create(&tid, &attr, callback, NULL);//设置线程分离后,表明线程执行完毕后,释放资源
31     if( ret != 0)
32     {
33         char * errstr = strerror(ret);
34         printf("error1: %s\n",errstr);
35     }
36     //获取线程的栈的大小
37     size_t size;
38     pthread_attr_getstacksize(&attr,&size);
39     printf("thread stack size: %ld\n",size);
40     //输出主线程和子线程ID
41     printf("tid: %ld, main thread id:%ld\n",tid, pthread_self());
42     //释放线程属性资源
43     pthread_attr_destroy(&attr);//有初始化 就有释放
44     pthread_exit(NULL);
45     return 0;
46 }
复制代码

线程同步:若没进行线程同步,同一数据可能被多个线程访问,可能会出现 售出-1票售出两次 100票

复制代码
 1 /*
 2     使用多线程实现卖票案例
 3     有3个窗口,一共是100张票
 4 */
 5 #include <stdio.h>
 6 #include <pthread.h>
 7 #include <unistd.h>
 8 int tickets = 100;//全局变量, 所有的线程都共享这一份资源
 9 void * sellticket(void * arg)
10 {
11     //卖票
12     //int tickets = 100;//如果是局部变量 变为每个线程100张票 需要变为全局变量
13     while(tickets > 0)
14     {
15         //usleep(3000);//设置微秒间隔  1秒 = 1000毫秒  1毫秒 = 1000微秒  
16         usleep(6000);
17         printf("%ld 正在卖第 %d 张门票\n",pthread_self(),tickets);
18         tickets--;
19     }
20     return NULL;
21 }
22 int main()
23 {
24     //创建3个子线程
25     pthread_t tid1,tid2,tid3;
26     pthread_create(&tid1, NULL, sellticket, NULL);
27     pthread_create(&tid2, NULL, sellticket, NULL);
28     pthread_create(&tid3, NULL, sellticket, NULL);
29     //回收子线程的资源, 阻塞
30     pthread_join(tid1,NULL);
31     pthread_join(tid2,NULL);
32     pthread_join(tid3,NULL);
33     //设置线程分离    回收再分离没有什么效果
34     //pthread_detach(tid1);
35     //pthread_detach(tid2);
36     //pthread_detach(tid3);
37     pthread_exit(NULL);//退出主线程
38     return 0;
39 }
复制代码

  线程的主要优势在于,能够通过全局变量来共享信息。不过,这种便捷的共享是有代价的:必须确保多个线程不会同时修改同一变量,或者某一线程不会读取正在由其他线程修改的变量。

  临界区是指访问某一共享资源的代码片段,并且这段代码的执行应为原子操作,也就是同时访问同一共享资源的其他线程不应终端该片段的执行

  线程同步:即当有一个线程在对内存进行操作时,其他线程都不可以对这个内存地址进行操作,直到该线程完成操作,其他线程才能对该内存地址进行操作,而其他线程则处于等待状态。

posted on   廿陆  阅读(6)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示