pthread

pthread线程使用小结

依然范特西╮ 提交于 2020-02-23 12:45:19
pthread线程使用小结 1.奇怪的线程参数初始化 for( i=0; i<n; i++) { // 会有什么问题? pthread_create(&tid,NULL, &thread_client_function, (void*)&i ); } 上面代码应该很容易明白,创建多个线程,传入序列号作为线程 id 。基实这里存在一个大 bug, 传递的参数会不成功!! 示例代码: view plain copy to clipboard print ? #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <memory.h> void * thread_client_function( void * param ) { int client_id = *( int *)param; printf( "client id %d\n" , client_id); } int main( int argc, char * argv[] ) { int n = atol( argv[1] ); int i=0; pthread_t tid; for ( i=0; i<n; i++) { pthread_create(&tid,NULL, &thread_client

LINUX下的简单线程池

ぐ巨炮叔叔 提交于 2020-02-23 12:30:39
前言 任何一种设计方式的引入都会带来额外的开支,是否使用,取决于能带来多大的好处和能带来多大的坏处,好处与坏处包括程序的性能、代码的可读性、代码的可维护性、程序的开发效率等。 线程池适用场合:任务比较多,需要拉起大量线程来处理;任务的处理时间相对比较短,按照线程的周期T1(创建阶段)、T2(执行阶段)、T3(销毁阶段)来算,执行阶段仅占用较少时间。 简单的线程池通常有以下功能:预创建一定数量的线程;管理线程任务,当工作线程没有事情可做时休眠自己;销毁线程池。 复杂一些的线程池有额外的调节功能:管理线程池的上限;动态调节工作线程数量,当大量工作请求到来时增加工作线程,工作请求较少时销毁部分线程。 内容部分 这次实现的是一个简单的线程池模型。 首先是线程池的头文件定义: 1 #include<unistd.h> 2 #include<stdlib.h> 3 #include<iostream> 4 #include< string > 5 #include< string .h> 6 #include<queue> 7 #include<errno.h> 8 #include<pthread.h> 9 using namespace std; 10 11 struct thread_work 12 { 13 void * (*routine)( void *); 14 void *

[Linux] undefined reference to `pthread_create'

和自甴很熟 提交于 2020-02-21 17:04:06
I tried to use pthread_create in RedHat Linux AS4, both in Eclipse+CDT and KDevelop. KDevelop: When you build a project by KDevelop, you'd better add a '-pthread' to the link option if your project includes <pthread.h>. Without that option, KDevelop would report an link error : '/root/kde/oop/src/oop.c:23: undefined reference to `pthread_create' After rebuild, the program works well. Eclipse + CDT: When you use pthread_create in your project, it will compile and link ok. But when you execute the program, the thread will receive a signal SIGSEGV that said: Execution is suspended because of

socket_one_mutex

走远了吗. 提交于 2020-02-21 14:32:37
网络连接有两种,web式的http请求回复和游戏使用的tcp长连接,使用cocos2d开发即时交互式游戏肯定式使用tcp长连接,因为交互非常频繁,如果像web一样请求的话服务器处理握手都吃不消。网络交互多线程不分家,有网络就有多线程,这个是多线程的第一篇,互斥锁 多线程的原则是:不要同时访问共享资源 那么,这里有两个线程同时对一个变量+1,并打印。假设没有互斥锁 #include <stdio.h> #include <pthread.h> #include <unistd.h> int buf = 0; void* my_thread_call(void* arg) {   for(int i = 0; i < 10; i++)   {     d += 1;     printf("%d\n", buf);     sleep(1);   }   return NULL; } int main() {   pthread_t one_thread;   pthread_t two_thread;   pthread_create(&one_thread, NULL, my_thread_call, NULL);   pthread_create(&two_thread, NULL, my_thread_call, NULL);   pthread_join(one_thread

JZ2440 数码相框项目 扩展项目(四) 加快显示速度

99封情书 提交于 2020-02-17 00:39:33
文章目录 链接 扩展项目四 1.目标 2.分析 3.实现 说明 链接 之后补充 JZ2440 数码相框项目 扩展项目(一) 多文件图标 (二) 显示png JZ2440 数码相框项目 扩展项目(三) 支持鼠标 x 扩展项目四 1.目标 "manual页面"里,点击"上一张"或"下一张"时所要显示的图片比较大, 速度有点慢: 改进它 2.分析 在打开当前图片后,就新建线程开始读取下一幅图片,若点击“下一张”按钮,获取之前线程读取的图片数据,就可以立马显示出来。 3.实现 在 manual_page.c 中包含头文件 <pthread.h> 使用 <pthread_create> <pthread_exit> <pthread_join> 这几个函数就可以实现功能 关于 pthread_create 的第二个参数 # include <pthread.h> extern int pthread_create ( // 创建进程 创建成功返回 0 pthread_t * __restrict __newthread , // 参数1: 返回新建线程号 __const pthread_attr_t * __restrict __attr , // 参数2: 参考上面链接,一般填 NULL void * ( * __start_routine ) ( void * ) , // 参数3:

linux pthread pthread_create pthread_join pthread_detach

一个人想着一个人 提交于 2020-02-16 09:49:01
linux 下面默认 pthread_create 由调用者自己负责子线程的资源回收 当父线程退出时候,子线程也会跟着退出,所以父线程推出的时候要调用pthread_join函数阻塞等待子线程的退出 pthread_detach( tid) 使线程tid 的线程处于分离状态,线程处于分离状态是该线程终止的时候资源被回收,不然的话该终止的子线程会占用系统资源直到父线程调用pthread_join 1 父线程先与子线程终止   此时:     如果子线程已近与父线程分离,如调用pthread_datach ,资源被回收    如果没有分离,资源无法释放 1 子线程先与父线程终止   此时:     如果子线程调用了线程分离函数pthread_detach, 或父线程调用了pthread_join ,资源被释放    如果上面两个都没有调用,资源无法释放 来源: https://www.cnblogs.com/songbingyu/p/4016469.html

POSIX 线程 – pthread_sigmask

这一生的挚爱 提交于 2020-02-16 03:20:44
概念 按照 POSIX, 异步 (外部) 信号发送到整个进程. 所有线程共享同一个设置, 即通过 sigaction 设置的线程处置方法. 每个线程有自己的信号掩码, 线程库根据该掩码决定将信号发送到哪个线程. 由于 Linux 线程实现上的独特性, 外部信号始终发送到特定的线程. pthread_sigmask pthread_sigmask 用来定义线程的信号掩码 其接口与 sigprocmask 一样 =============================================================================== #include <pthread.h> #include <signal.h> int pthread_sigmask (int how, const sigset_t *newmask, sigset_t *oldmask);=============================================================================== pthread_kill 和 sigwait =============================================================================== #include

gcc中-pthread和-lpthread的区别

吃可爱长大的小学妹 提交于 2020-02-16 01:10:05
用gcc编译使用了POSIX thread的程序时通常需要加额外的选项,以便使用thread-safe的库及头文件,一些老的书里说直接增加链接选项 -lpthread 就可以了,像这样: Shell代码 gcc -c x.c gcc x.o -ox -lpthread 而gcc手册里则指出应该在编译和链接时都增加 -pthread 选项,像这样: Shell代码 gcc -pthread -c x.c gcc x.o -ox -pthread 那么 -pthread 相比于 -lpthread 链接选项究竟多做了什么工作呢?我们可以在verbose模式下执行一下对应的gcc命令行看出来。下面是老式的直接加 -lpthread 链接选项的输出结果: Shell代码 $ gcc -v -c x.c ... /usr/lib/gcc/i486-linux-gnu/4.2.4/cc1 -quiet -v x.c -quiet -dumpbase x.c -mtune=generic -auxbase x -version -fstack-protector -fstack-protector -o /tmp/cch4ASTF.s ... as --traditional-format -V -Qy -o x.o /tmp/cch4ASTF.s ... $ gcc -v x.o -ox

线程池理解

久未见 提交于 2020-02-15 19:43:32
Linux 多线程编程之 线程池 的原理和一个简单的C实现,提高对多线程编 程的认知,同步处理等操作,以及如何在实际项目中高效的利用多线程开 发。 1. 线程池介绍 为什么需要线程池??? 目前的大多数网络服务器,包括Web服务器、Email服务器以及数据库服务 器等都具有一个共同点,就是单位时间内必须处理数目巨大的连接请求, 但处理时间却相对较短。 传统多线程方案中我们采用的服务器模型则是一旦接受到请求之后,即创 建一个新的线程,由该线程执行任务。任务执行完毕后,线程退出,这就 是是“即时创建,即时销毁”的策略。尽管与创建进程相比,创建线程的时 间已经大大的缩短,但是如果提交给线程的任务是执行时间较短,而且执 行次数极其频繁,那么服务器将处于不停的创建线程,销毁线程的状态, 这笔开销将是不可忽略的。 线程池为线程生命周期开销问题和资源不足问题提供了解决方案。通过对 多个任务重用线程,线程创建的开销被分摊到了多个任务上。其好处是, 因为在请求到达时线程已经存在,所以无意中也消除了线程创建所带来的 延迟。这样,就可以立即为请求服务,使应用程序响应更快。而且,通过 适当地调整线程池中的线程数目,也就是当请求的数目超过某个阈值时, 就强制其它任何新到的请求一直等待,直到获得一个线程来处理为止,从 而可以防止资源不足。 2. 线程池结构 2.1 线程池任务结点结构

C语言/原子/编译,你真的明白了吗?

本秂侑毒 提交于 2020-02-14 04:24:06
  版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖。如要转贴,必须注明原文网址   http://www.cnblogs.com/Colin-Cai/p/7668982.html   作者:窗户   QQ:6679072   E-mail:6679072@qq.com   说到原子,类似于以下的代码可能人人都可以看出猫腻。 /* http://www.cnblogs.com/Colin-Cai */ #include <stdio.h> #include <pthread.h> int cnt = 0; void* mythread(void* arg) { int i; for(i=0;i<500000000;i++) cnt++; return NULL; } int main() { pthread_t id, id2; pthread_create(&id, NULL, mythread, NULL); pthread_create(&id2, NULL, mythread, NULL); pthread_join(id, NULL); pthread_join(id2, NULL); printf("cnt = %d\n", cnt); return 0; }   我想大多数人都知道其结果未必会得到1000000000。   测试一下吧。 linux