posix thread小程序

2024-08-25 00:32
文章标签 程序 thread posix

本文主要是介绍posix thread小程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

posix thread在头文件pthread.h中,基本操作有:

(1)int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void (*start)(void *), void *arg),创建一个线程(线程id,线程属性参数,线程调用的函数start,线程传递参数),成功返回0,失败返回error错误标志。可以由头文件errno.h中的strerror接收错误类型。

(2)void pthread_exit(void *retval)终止当前线程,成功返回0,失败返回error。retval线程结束或取消时返回的指针

(3)int pthread_join(pthread_t thread, void **thread_return)挂起当前线程,等待thread执行完后再执行。thread_return线程取消或者结束时返回的指针。

(4)pthread_t pthread_self(void)返回当前线程。

(5)int pthread_equal(pthread_t thread1, pthread_t thread2)判断两个线程是否相同,如果相同返回非零值,否则返回0。

(6)int pthread_detach(ptread_t thread)从进程中分离出资源来给该线程,当线程结束时收回。调用成功返回0,否则返回非零值。

线程的同步机制:互斥量(mutex),连接/合并(join),条件变量(condition vavriables)

互斥量mutex,通过mutex_lock_count判断当前互斥量是不是应经锁住。

(1)pthread_mutex_t mymutex;定义一个互斥量。静态初始化PTHREAD_MUTEX_INITIALIZER。动态初始化pthread_mutex_init()。静态初始化时头文件pthread.h中定义的一个宏。只能适用于定义是初始化。

(2)int pthread_mutex_lock(pthread_mutex_t *mymutex)加锁,mutex_lock_count++,成功返回0,失败返回错误标志,并阻塞当前线程等待mymutex可用时再执行。

(3)int pthread_mutex_trylock(pthread_mutex_t *mymutex)增加了一个try。

(4)int pthread_mutex_unlock(pthread_mutex_t *mymutex)解锁,mutex_lock_count--,释放资源。


/*
完成一个有趣的游戏(类似抢板凳):主线程中创建线程1,然后线程1再创建2个线程2,3,线程2,3分别为一个计数器,
操作counter(初值为0),线程2每使用一次加3,线程3每用一次加5,如果加到被15整除,那么counter加8,
看哪个线程先加到9999,并计算自己使用了多少次计算器。先到者胜利,并打印出相应信息。
*/
#include <iostream>
#include <pthread.h>
using namespace std;
#define MAXN 999999
struct node{int cnt;int key;
};
node res1, res2;
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
int counter;
void *add2(void *arg) {while( 1 ) {pthread_mutex_lock(&mymutex);if( counter < MAXN ) {counter += 5;if( counter%5 == 0 ) counter += 8;res2.cnt++;if( counter >= MAXN )res2.key = 1;cout << "add2 -> " << counter << endl;}else {pthread_mutex_unlock(&mymutex);break;}pthread_mutex_unlock(&mymutex);}
}
void *add1(void *arg) {while( 1  ) {pthread_mutex_lock(&mymutex);if( counter < MAXN ) {counter += 3;res1.cnt++;if( counter >= MAXN )res1.key = 1;cout << "add1 -> " << counter << endl; }else {pthread_mutex_unlock(&mymutex);break;}pthread_mutex_unlock(&mymutex);}
}
void *start(void *arg) {int status;pthread_t thread_2;status = pthread_create(&thread_2, NULL, add1, NULL);cout << "thread_2 " << status << endl;if( status ) {cout << "thread_2 error" << endl;return NULL;}pthread_t thread_3;status = pthread_create(&thread_3, NULL, add2, NULL);cout << "thread_3" << status << endl;if( status ) {cout << "thread3_error" << endl;return NULL;	}	pthread_join(thread_2, NULL);pthread_join(thread_3, NULL);return NULL;
}
int main()
{res1.cnt = res1.key = 0;res2.cnt = res2.key = 0;counter = 0;int status;pthread_t thread_1;status = pthread_create(&thread_1,NULL, start,NULL);cout << "main " << status << endl;if( !status ) {pthread_join(thread_1,NULL);cout << "res1: cnt=" << res1.cnt << ", key=" << res1.key << endl;cout << "res2: cnt=" << res2.cnt << ", key=" << res2.key << endl;}elsecout << "thread_1 error" << endl;return 0;
}


这篇关于posix thread小程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1104048

相关文章

python编写朋克风格的天气查询程序

《python编写朋克风格的天气查询程序》这篇文章主要为大家详细介绍了一个基于Python的桌面应用程序,使用了tkinter库来创建图形用户界面并通过requests库调用Open-MeteoAPI... 目录工具介绍工具使用说明python脚本内容如何运行脚本工具介绍这个天气查询工具是一个基于 Pyt

Ubuntu设置程序开机自启动的操作步骤

《Ubuntu设置程序开机自启动的操作步骤》在部署程序到边缘端时,我们总希望可以通电即启动我们写好的程序,本篇博客用以记录如何在ubuntu开机执行某条命令或者某个可执行程序,需要的朋友可以参考下... 目录1、概述2、图形界面设置3、设置为Systemd服务1、概述测试环境:Ubuntu22.04 带图

Python程序打包exe,单文件和多文件方式

《Python程序打包exe,单文件和多文件方式》:本文主要介绍Python程序打包exe,单文件和多文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python 脚本打成exe文件安装Pyinstaller准备一个ico图标打包方式一(适用于文件较少的程

Python程序的文件头部声明小结

《Python程序的文件头部声明小结》在Python文件的顶部声明编码通常是必须的,尤其是在处理非ASCII字符时,下面就来介绍一下两种头部文件声明,具有一定的参考价值,感兴趣的可以了解一下... 目录一、# coding=utf-8二、#!/usr/bin/env python三、运行Python程序四、

无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案

《无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案》:本文主要介绍了无法启动此程序,详细内容请阅读本文,希望能对你有所帮助... 在计算机使用过程中,我们经常会遇到一些错误提示,其中之一就是"api-ms-win-core-path-l1-1-0.dll丢失

SpringBoot后端实现小程序微信登录功能实现

《SpringBoot后端实现小程序微信登录功能实现》微信小程序登录是开发者通过微信提供的身份验证机制,获取用户唯一标识(openid)和会话密钥(session_key)的过程,这篇文章给大家介绍S... 目录SpringBoot实现微信小程序登录简介SpringBoot后端实现微信登录SpringBoo

uniapp小程序中实现无缝衔接滚动效果代码示例

《uniapp小程序中实现无缝衔接滚动效果代码示例》:本文主要介绍uniapp小程序中实现无缝衔接滚动效果的相关资料,该方法可以实现滚动内容中字的不同的颜色更改,并且可以根据需要进行艺术化更改和自... 组件滚动通知只能实现简单的滚动效果,不能实现滚动内容中的字进行不同颜色的更改,下面实现一个无缝衔接的滚动

Java使用WebView实现桌面程序的技术指南

《Java使用WebView实现桌面程序的技术指南》在现代软件开发中,许多应用需要在桌面程序中嵌入Web页面,例如,你可能需要在Java桌面应用中嵌入一部分Web前端,或者加载一个HTML5界面以增强... 目录1、简述2、WebView 特点3、搭建 WebView 示例3.1 添加 JavaFX 依赖3

防止SpringBoot程序崩溃的几种方式汇总

《防止SpringBoot程序崩溃的几种方式汇总》本文总结了8种防止SpringBoot程序崩溃的方法,包括全局异常处理、try-catch、断路器、资源限制、监控、优雅停机、健康检查和数据库连接池配... 目录1. 全局异常处理2. 使用 try-catch 捕获异常3. 使用断路器4. 设置最大内存和线

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.