pthread

c多线程不加锁demo

江枫思渺然 提交于 2019-12-13 01:10:32
// // Created by gxf on 2019/12/13. // #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shareInt = 0; void increase_num(void); int main() { int ret; pthread_t thread1, thread2, thread3; ret = pthread_create(&thread1, NULL, increase_num, NULL); ret = pthread_create(&thread2, NULL, increase_num, NULL); ret = pthread_create(&thread3, NULL, increase_num, NULL); pthread_join(thread1, NULL); pthread_join(thread2, NULL); pthread_join(thread3, NULL); printf("sharedInt:%d\n", shareInt); return 0; } void increase_num(void) { long i, tmp; for (i=0; i <= 100000; i++) { tmp = shareInt; tmp =

pthread_create线程终止问题

北慕城南 提交于 2019-12-11 15:39:32
一直以为,程序创建线程,线程运行结束会自动清空资源 最近在一个项目中用到了线程,除去业务逻辑,我把他简化出来是下面这样 //pthread.c 错误demo示例 #include <stdio.h> #include <pthread.h> static int testcount = 0; static void *test_thread_handler() { testcount++; printf("%d\n",testcount); return 0; } int main( int argc, char *argv[] ) { int ret = 0; pthread_t test_tid; while(1) { usleep(10000); ret = pthread_create(&test_tid, NULL, test_thread_handler,NULL); if(ret != 0) { printf("Create handler error :%d!\t testcount:%d\n", ret, testcount); return -1; } } return 0; } 备注:pthread库不是Linux系统默认的库,连接时需要使用静态库libpthread.a,所以在线程函数在编译时,需要连接库函数 gcc pthread.c -o pthread

生产者与消费者模型

我的梦境 提交于 2019-12-10 05:42:05
生产者与消费者模型 生产者与消费者模型是经典的同步与互斥模型。 支持忙闲不均 支持并发 解耦合 有两类进程分别为 消费者进程 和 生产者进程 ,对同一个临界资源进行访问,生产者不断生产产品,并将产品作为资源加入缓冲区,而消费者不断的消费缓冲区中的资源,利用信号量实现两类进程的同步与互斥。 其中生产者与生产者之间具有互斥关系; 消费者与消费者之间也具有互斥关系; 而生产者与消费者之间具有同步与互斥关系。 即一个场所,两类角色,三类关系。 利用C++封装一个线程安全的队列,实现生产者与消费者模型: # include <iostream> # include <queue> # include <stdio.h> # include <unistd.h> # include <pthread.h> # define MAX_QUE 10 class BlockQueue { private : std :: queue < int > _queue ; // 动态增长的队列 int _capacity ; // 队列最大容量 pthread_cond_t _cond_productor ; // 生产者等待队列 pthread_cond_t _cond_consumer ; // 消费者等待队列 pthread_mutex_t _mutex ; // 实现互斥 public :

unix环境高级编程-undefined reference to &apos;pthread_create&apos;

亡梦爱人 提交于 2019-12-10 02:30:01
笔者一直在学习unix环境高级编程。第十一章为线程编程。第一个程序就是打印线程ID。程序如下: [cpp] view plain copy #include "apue.h" #include <pthread.h> pthread_t ntid; void printids( const char * s) { pid_t pid; pthread_t tid; pid=getpid(); //得到当前进程id tid=pthread_self(); //得到当前线程id printf( "%s pid %u tid %u (0x%x)\n" ,s,(unsigned int )pid,(unsigned int ) tid,(unsigned int )tid); } void * thr_fn( void * arg) { printids( "new thread: " ); return (( void *) 0); } int main( void ) { int err; err=pthread_create(&ntid,NULL,thr_fn,NULL); if (err!=0) err_quit( "can't create thread:%s\n" ,strerror(err)); printids( "main thread :" ); sleep(1);

进程通信和同步(转)

ぃ、小莉子 提交于 2019-12-09 21:24:53
概念 竞争条件 多个进程读写某些共享数据,而最后的结果取决于进程运行的精确时许,称为竞争条件。 忙等待的互斥 几种实现互斥的方案: 屏蔽中断 1在单处理器系统中,最简单的方法是使每个进程在刚刚进入临界区后立即屏蔽所有中断,包括时钟中断。CPU 只有在发生中断的时候才会进行进程切换,这样在中断被屏蔽后 CPU 将不会被切换到其他进程。 锁变量 严格轮换法 while (TRUE) { while (turn != 0) critical_region(); turn = 1; noncritical_region(); } while (TRUE) { while (turn != 1) critical_region(); turn = 0; noncritical_region(); } 忙等待检查变量。使用忙等待的锁称为自旋锁。 Peterson 解法 #define FALSE 0 #define TRUE 1 #define N 2 /* number of processes */ int turn; /* whose turn is it? */ int interested[N]; /* all values initially 0 (FALSE) */ void enter_region(int process); /* process is 0 or 1 */ {

线程

点点圈 提交于 2019-12-09 13:18:24
什么是线程 线程是在进程内部运行的一个执行流 linux下没有真正意义上的线程,是用进程模拟的 优点 创建一个新线程的代价要比创建一个新进程的代价小 与进程间切换比,线程切换操作系统做的工作少 线程占用的资源少 能充分利用多处理器的可并行数量 在等待慢速I/O结束的同时,可以执行其他计算任务 计算密集型应用,为了能在多个处理器系统上运行,将计算分解到多个线程中实现 I/O密集型应用,可以同时等待不同I/O的操作 缺点 性能损失 健壮性低 缺乏访问控制 编程难度高 进程VS线程 1. 进程是资源分配的基本单位 2. 线程是调度的基本单位 3. 线程共享进程资源,但是拥有独立栈结构,独立硬件上下文(因为线程是会被调度的) 4. 文件描述符共享 进程创建 1 #include < iostream > 2 #include < string > 3 #include < unistd . h > 4 #include < sys / types . h > 5 #include < pthread . h > 7 using namespace std ; 8 9 void * thread_routine ( void * arg ) 10 { 11 string str = ( char * ) arg ; 12 while ( 1 ) 13 { 14 cout << str <<

Android Binder线程

流过昼夜 提交于 2019-12-06 20:35:50
在android系统中,通过binder进行IPC时,服务端总是会起一些Binder线程来响应客户端的请求。如下面的这个设备上,system_process进程中就可以看到许多名为"Binder_X"的线程: 那这些Binder线程又是如何创建,如何管理的呢?而这些Binder线程本身又有些什么样的特点呢?在android的java app进程被创建起来时,它就会去建立一个线程池,来专门处理那些binder IPC事务。在frameworks/base/cmds/app_process/app_main.cpp中我们可以看到下面的这两个方法: virtual void onStarted() { sp<ProcessState> proc = ProcessState::self(); ALOGV("App process: starting thread pool.\n"); proc->startThreadPool(); AndroidRuntime* ar = AndroidRuntime::getRuntime(); ar->callMain(mClassName, mClass, mArgC, mArgV); IPCThreadState::self()->stopProcess(); } virtual void onZygoteInit() { // Re

golang思考之运行速度之channel

浪尽此生 提交于 2019-12-06 14:55:15
golang channel测速程序: package main import ( "runtime" ) var PC_DATA_SIZE int var chEnd chan int func producer(ch chan byte) { for i := 0; i < PC_DATA_SIZE; i++ { ch <- 0 } chEnd <- 0 } func consumer(ch chan byte) { for i := 0; i < PC_DATA_SIZE; i++ { <-ch } chEnd <- 0 } func main() { runtime.GOMAXPROCS(runtime.NumCPU()) PC_DATA_SIZE = 10000000 chEnd = make(chan int) ch := make(chan byte, 1024) go producer(ch) go consumer(ch) <-chEnd <-chEnd } C语言有锁并发队列: #include <pthread.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #define PC_DATA_SIZE 10000000 typedef struct Queue{ uint8_t

linuxC线程pthread的相关理解

て烟熏妆下的殇ゞ 提交于 2019-12-06 13:12:24
1. 线程概念  Linux里的线程,也叫轻量级进程(light weight process),本质上是进程。  Linux早期是没有线程的,后来的线程是借助进程实现的。线程、进程的核心都是调用系统的clone方法实现。  线程和进程的区别:(说明:PCB即进程控制块)    进程拥有PCB,用于独立的地址空间。是操作系统最小资源分配单位。(独居地址空间)    线程也有PCB,但没有独立的地址空间,共享同一个地址空间。是操作系统最小执行单位。(合租地址空间)    Linux内核线程实现原理:    1)轻量级进程,也有PCB,创建线程使用的底层方法和进程一样都是clone方法。    2)线程有各自的PCB,但是各自的PCB指向内存资源的三级页表是相同的。所以地址空间是相同的,即共享地址空间。    3)线程可看作是寄存器和栈的集合。  三级页表映射:进程PCB -> 页目录(可看作数组,首地址位于PCB里)-> 页表(可看作数组元素) -> 物理页表 -> 内存单元  线程共享的资源:    文件描述符、信号处理方式、当前工作目录、用户ID和组ID、内存地址空间(.text/.data/.bss/heap/so)  线程非共享的资源:    线程ID、处理器现场和内核栈指针、用户空间栈、errno变量、信号屏蔽字、线程调度优先级、线程调度策略 2. pthread线程函数 

线程同步

你。 提交于 2019-12-06 12:14:21
我们使用互斥锁解决了多个线程的竞态条件问题。 互斥锁的一个主要特点是,谁先拿到锁先就可以优先访问共享资源,因此多个线程访问共享资源的互斥性是得到了保证,但是在某些场合可能还希望确保线程间执行的顺序。 如我们有一个共享内存数据资源M,我们整个程序设计需求是要求线程A在M上做了处理之后,线程B才能做处理。这种需要确保多线程间执行先后顺序的技术,称为线程的同步。 条件变量是线程同步的主要手段。其大致的实现思想就是: 线程B,调用条件变量的接口让自身阻塞; 线程A,在处理完资源后,通过条件变量接口唤醒正在等待该资源的线程B。 条件变量的初始化也有静态初始化和动态初始化两种方式。 //静态初始化 // 与互斥锁类似静态初始化一个全局的条件变量 pthread_cond_t cond = PTHREAD_COND_INITIALIZER; //动态初始化 #include <pthread.h> int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); //通知和等待条件变量 #include <pthread.h> // 等待一个指定的条件变量 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); //