pthread 笔记
1.创建线程
res = pthread_create(&a_thread, NULL, thread_function1, NULL);
if (res != 0)
{
perror("Thread creation failure");
}
2.等待线程结束
pthread_join
3.线程退出时,语句:
pthread_exit(NULL);
return NULL; 4. 互斥锁 使用前必须初始化!!!
pthread_mutex_init(&mutex, NULL);
pthread_mutex_destroy(&mutex);
4.
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#define sleep Sleep
#else
#include <unistd.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h> sem_t bin_sem;
void *thread_function1(void *arg)
{
printf("thread_function1--------------sem_wait\n");
sem_wait(&bin_sem);
printf("sem_wait OK\n");
while ()
{
}
pthread_exit(NULL);
return NULL;
} void *thread_function2(void *arg)
{
printf("thread_function2--------------sem_post\n");
sem_post(&bin_sem);
printf("sem_post\n");
while ()
{
}
return NULL;
} int main()
{
int res;
pthread_t a_thread;
void *thread_result; res = sem_init(&bin_sem, , );
if (res != )
{
perror("Semaphore initialization failed");
}
printf("sem_init\n");
res = pthread_create(&a_thread, NULL, thread_function1, NULL);
if (res != )
{
perror("Thread creation failure");
}
printf("thread_function1\n");
sleep ();
printf("sleep\n");
res = pthread_create(&a_thread, NULL, thread_function2, NULL);
if (res != )
{
perror("Thread creation failure");
}
while ()
{
}
}
5 sem:
sem_post 每执行一次 则加1
sem_wait( &st ) 等待直到st大于1 才向下执行, 每次执行st减去1;
6 条件锁
#include <pthread.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#define sleep(p) Sleep(p)
#else
#include <ustlib.h>
#define sleep(p) usleep((p)*1000)
#endif pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /*初始化互斥锁*/
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; //初始化条件变量 void *thread1(void *);
void *thread2(void *); int i = ;
int main(void)
{
pthread_t t_a;
pthread_t t_b;
pthread_create(&t_a, NULL, thread1, (void *)NULL);/*创建进程t_a*/
pthread_create(&t_b, NULL, thread2, (void *)NULL); /*创建进程t_b*/
pthread_join(t_b, NULL);/*等待进程t_b结束*/
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
getchar();
exit();
} void *thread1(void *junk)
{
for (i = ; i <= ; i++)
{
pthread_mutex_lock(&mutex);//
if (i % == )
pthread_cond_signal(&cond);/*条件改变,发送信号,通知t_b进程*/
else
printf("thead1:%d\n", i);
pthread_mutex_unlock(&mutex);//*解锁互斥量*/
printf("Up Unlock Mutex\n");
sleep();
}
return NULL; } void *thread2(void *junk)
{
while (i<)
{
pthread_mutex_lock(&mutex); if (i % != )
pthread_cond_wait(&cond, &mutex);/*等待*/
printf("thread2:%d\n", i);
pthread_mutex_unlock(&mutex);
printf("Down Ulock Mutex\n"); sleep();
}
return NULL;
}
pthread 笔记的更多相关文章
- Pthread 用法笔记
什么是线程? 从技术上讲,一个线程被定义为一个独立的指令流. 一个进程可以包含一个或多个线程. 线程操作包括线程创建,终止,同步(连接,阻塞),调度,数据管理和进程交互. 进程内的所有线程共享: 相同 ...
- PThread 学习笔记
POSIX 线程,也被称为Pthreads,是一个线程的POSIX标准: pthread.h int pthread_create(pthread_t * thread, pthread_attr_t ...
- storysnail的Linux串口编程笔记
storysnail的Linux串口编程笔记 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创代码根据Ge ...
- Mongodb Manual阅读笔记:CH4 管理
4 管理 Mongodb Manual阅读笔记:CH2 Mongodb CRUD 操作Mongodb Manual阅读笔记:CH3 数据模型(Data Models)Mongodb Manual阅读笔 ...
- What every programmer should know about memory 笔记
What every programmer should know about memory, Part 1(笔记) 每个程序员都应该了解的内存知识[第一部分] 2.商用硬件现状 现在硬件的 ...
- contiki-main.c 中的process系列函数学习笔记 <contiki学习笔记之六>
说明:本文依然依赖于 contiki/platform/native/contiki-main.c 文件. ---------------------------------------------- ...
- linux网络编程学习笔记之五 -----并发机制与线程�
进程线程分配方式 简述下常见的进程和线程分配方式:(好吧,我仅仅是举几个样例作为笔记...并发的水太深了,不敢妄谈...) 1.进程线程预分配 简言之,当I/O开销大于计算开销且并发量较大时,为了节省 ...
- Boost使用笔记(Smart_ptr)
我是Word写的,复制过来实在懒得在排版了,有兴趣的朋友可以去我的百度文库看,谢谢 http://wenku.baidu.com/view/34e485e2f61fb7360b4c653e.html ...
- 笔记整理--Linux多线程
Unix高级环境编程系列笔记 (2013/11/17 14:26:38) Unix高级环境编程系列笔记 出处信息 通过这篇文字,您将能够解答如下问题: 如何来标识一个线程? 如何创建一个新线程? 如何 ...
随机推荐
- Rocketmq异步发送消息
package com.bfxy.rocketmq.quickstart; import java.util.List; import org.apache.rocketmq.client.excep ...
- python安装appium模块
(base) localhost:~ ligaijiang$ pip3 install Appium-Python-Client Collecting Appium-Python-Client Dow ...
- Golang 空指针nil的方法和数据成员
golang中,有一个特殊的指针值nil. 如何使用nil没有方法和成员变量呢? 下面来看下具体例子. 程序中,定义结构体类型Plane, 将Plane类型的指针作为函数的参数,然后传入nil作为实参 ...
- SAN LAN MAN WAN的区别
主要是范围不同 SAN: System Area NetworkLAN: Local Area NetworkMAN: Metropolitan Area NetworkWAN: Wide Area ...
- UIView和UIWindow的使用
1.创建窗口: func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [N ...
- Linux显存占用无进程清理方法(附批量清理命令)
在跑TensorFlow.pytorch之类的需要CUDA的程序时,强行Kill掉进程后发现显存仍然占用,这时候可以使用如下命令查看到top或者ps中看不到的进程,之后再kill掉: fuser -v ...
- Spring boot Gradle项目搭建
Spring boot Gradle项目搭建 使用IDEA创建Gradle工程 操作大致为:File->new->Project->Gradle(在左侧选项栏中) 创 ...
- python-Web-django-路由保护
from django.shortcuts import redirect,HttpResponse from app01.models import * import re def ddff(mod ...
- PHP SQL注入
开发者容易遗漏的输入点: HTTP头 X-Forwarded-For 获取用户ip User-Agent 获取浏览器 Referer 获取之 ...
- Transformer详解
0 简述 Transformer改进了RNN最被人诟病的训练慢的缺点,利用self-attention机制实现快速并行. 并且Transformer可以增加到非常深的深度,充分发掘DNN模型的特性,提 ...