pthread

linux 多线程编程

穿精又带淫゛_ 提交于 2019-12-24 12:27:07
原文地址: 1.Linux“线程” 进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型。Linux是一种“多进程单线程”的操作系统。Linux本身只有进程的概念,而其所谓的“线程”本质上在内核里仍然是进程。 大家知道,进程是资源分配的单位,同一进程中的多个线程共享该进程的资源(如作为共享内存的全局变量)。Linux中所谓的“线程”只是在被创建时clone了父进程的资源,因此clone出来的进程表现为“线程”,这一点一定要弄清楚。因此,Linux“线程”这个概念只有在打冒号的情况下才是最准确的。 目前Linux中最流行的线程机制为LinuxThreads,所采用的就是线程-进程“一对一”模型,调度交给核心,而在用户级实现一个包括信号处理在内的线程管理机制。LinuxThreads由Xavier Leroy (Xavier.Leroy@inria.fr)负责开发完成,并已绑定在GLIBC中发行,它实现了一种BiCapitalized面向Linux的Posix 1003.1c “pthread”标准接口。Linuxthread可以支持Intel、Alpha、MIPS等平台上的多处理器系统。 按照POSIX 1003.1c 标准编写的程序与Linuxthread 库相链接即可支持Linux平台上的多线程,在程序中需包含头文件pthread. h

线程属性总结 线程的api属性

我是研究僧i 提交于 2019-12-23 04:48:14
http://blog.csdn.net/zsf8701/article/details/7842392 //线程属性结构如下: typedef struct { int etachstate; //线程的分离状态 int schedpolicy; //线程调度策略 structsched_param schedparam; //线程的调度参数 int inheritsched; //线程的继承性 int scope; //线程的作用域 size_t guardsize; //线程栈末尾的警戒缓冲区大小 int stackaddr_set; //线程的栈设置 void* stackaddr; //线程栈的位置 size_t stacksize; //线程栈的大小 }pthread_attr_t; 属性值不能直接设置,须使用相关函数进行操作,初始化的函数为pthread_attr_init,这个函数必须在pthread_create函数之前调用。之后须用pthread_attr_destroy函数来释放资源。线程属性主要包括如下属性:作用域(scope)、栈尺寸(stack size)、栈地址(stack address)、优先级(priority)、分离的状态(detached state)、调度策略和参数(scheduling policy and parameters)

28 POSIX Threads

为君一笑 提交于 2019-12-23 00:58:38
1 What are Threads? 一个进程可以有多个线程 线程共享进程的资源,多进程是复制内存空间,彼此独立 线程被OS调度,意味着一个进程会使用超过100%的cpu(多核系统) 2 Hello POSIX Threads? POSIX became the standard interface for many system(C,Java,Python) 2.1 Creating a Thread /* hello_pthread.c*/ # include <stdio.h> # include <stdlib.h> # include <string.h> # include <pthread.h> void * hello_fun ( void * args ) { printf ( "Hello World!\n" ) ; return NULL ; } int main ( int argc , char * argv [ ] ) { pthread_t thread ; //thread identifier //create a new thread have it run the function hello_fun pthread_create ( & thread , NULL , hello_fun , NULL ) ; //wait until

linux下C/C++,多线程pthread

╄→гoц情女王★ 提交于 2019-12-22 15:14:30
  ·线程创建   函数原型:int pthread_create ( pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg);   返回值:若是成功建立线程返回0,否则返回错误的编号。   形式参数:pthread_t *restrict tidp要创建的线程的线程id指针;const pthread_attr_t *restrict attr创建线程时的线程属性;void* (start_rtn)(void)返回值是void类型的指针函数;void *restrict arg start_rtn的形参。    ·线程挂起: 该函数的作用使得当前线程挂起,等待另一个线程返回才继续执行。也就是说当程序运行到这个地方时,程序会先停止,然后等线程id为thread的这个线程返回,然后程序才会断续执行。   函数原型:int pthread_join ( pthread_t thread, void **value_ptr);   参数说明如下:thread等待退出线程的线程号;value_ptr退出线程的返回值。   · 线程退出   函数原型:void pthread_exit (void *rval_ptr);   ·

linuxc线程信号-pthread_cond_wait理解

非 Y 不嫁゛ 提交于 2019-12-22 11:21:56
pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)函数 传入的參数mutex用于保护条件,由于我们在调用pthread_cond_wait时,假设条件不成立我们就进入堵塞。可是进入阻 塞这个期间,假设条件变量改变了的话,那我们就漏掉了这个条件。由于这个线程还没有放到等待队列上。所以调用pthread_cond_wait前要先锁 相互排斥量, 即调用pthread_mutex_lock(),pthread_cond_wait在把线程放进堵塞队列后,自己主动对mutex进行解锁,使得 其他线程能够获得加锁的权利。这样其他线程才干对临界资源进行訪问并在适当的时候唤醒这个堵塞的进程。 当pthread_cond_wait返回的时候又自己主动给mutex加锁。 实际上边代码的加解锁步骤例如以下: /************pthread_cond_wait()的用法**********/ pthread_mutex_lock(&qlock); /*lock*/ pthread_cond_wait(&qready, &qlock); /*block-->unlock-->wait() return-->lock*/ pthread_mutex_unlock(&qlock); /*unlock*/ /**********

线程的同步

左心房为你撑大大i 提交于 2019-12-22 05:10:36
多个线程共享相同的内存时,需要确保每个线程看到一致的数据视图。 1.互斥量 可以通过使用pthread的互斥接口保护数据,确保同一时间只有一个线程访问数据,互斥量(mutex)从本质上说是一把锁, 在访问共享资源前对互斥量进行加锁,在访问完成后释放互斥量上的锁。对互斥量进行加锁后,任何其他试图再次对互斥 量进行加锁的线程将被阻塞直到当前线程释放该互斥锁。如果释放互斥锁时有多个线程阻塞,所有在该互斥锁上阻塞线程都会变成可运行状态,第一个变为运行状态的线程可以对互斥量进行加锁,其他线程将会看到互斥锁依然被锁住,只有回去再次等待它重新变成可用。在这种方式下,每次只有一个线程可以向前执行。 互斥变量用pthread_mutex_t数据类型来表示,在使用互斥变量以前,必须首先对它进行初始化,可以把它置为常量PTHREAD_MUTEX_INITIALIZER(只对静态分配的互斥量),也可以通过调用pthread_mutex_init函数进行初始化。如果动态地分配互斥量(例如通过调用malloc),那么在释放内存前需要调用pthread_mutex_destroy。 #include<pthread.h> int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr

C++中多线程与Singleton的那些事儿

独自空忆成欢 提交于 2019-12-21 23:50:39
前言 前段时间在网上看到了个的面试题,大概意思是如何在不使用锁和C++11的情况下,用C++实现线程安全的Singleton。 看到这个题目后,第一个想法就是用Scott Meyer在《Effective C++》中提到的,在static成员函数中构造local static变量的方法来实现,但是经过一番查找、思考,才明白这种实现在某些情况下是有问题的。本文主要将从最基本的单线程中的Singleton开始,慢慢讲述多线程与Singleton的那些事。 单线程 在单线程下,下面这个是常见的写法: template<typename T> class Singleton { public: static T& getInstance() { if (!value_) { value_ = new T(); } return *value_; } private: Singleton(); ~Singleton(); static T* value_; }; template<typename T> T* Singleton<T>::value_ = NULL; 在单线程中,这样的写法是可以正确使用的,但是在多线程中就不行了。 多线程加锁 在多线程的环境中,上面单线程的写法就会产生race condition从而产生多次初始化的情况。要想在多线程下工作

[Operating System] {ud923} PSet1 (unfinished)

僤鯓⒐⒋嵵緔 提交于 2019-12-21 22:37:19
/* Requirements: Priority Readers and Writers Write a multi-threaded C program that gives readers priority over writers concerning a shared (global) variable. Essentially, if any readers are waiting, then they have priority over writer threads -- writers can only write when there are no readers. This program should adhere to the following constraints: Multiple readers/writers must be supported (5 of each is fine) Readers must read the shared variable X number of times Writers must write the shared variable X number of times Readers must print: The value read The number of readers present when

简单的C/C++日志模块实现

旧时模样 提交于 2019-12-21 15:48:48
诸如log4cxx之类的日志库还是有些复杂,自己实现了一个简单的日志模块。 支持文件设置、日志级别、非原子打印,还附加了常用的线程锁相关宏,如下: sys_logger.h 1 #ifndef __sys_logger_h_ 2 #define __sys_logger_h_ 3 4 #include <stdio.h> 5 #include <iostream> 6 #include <cstring> 7 #include <stdarg.h> 8 #include <time.h> 9 #include <unistd.h> 10 #include <pthread.h> 11 #include <sys/time.h> 12 #include <sys/syscall.h> 13 14 #define gettid() syscall(SYS_gettid) 15 16 #define LEVEL_SOCKET_DEBUG 0 17 #define LEVEL_DEBUG 1 18 #define LEVEL_INFO 2 19 #define LEVEL_WARNING 3 20 #define LEVEL_ERROR 4 21 22 #define LOG_BUF_SIZE 2048 23 #define MAX_FILENAME_LEN 256 24 25 26

Linux下undefined reference to ‘pthread_create’问题解决

风格不统一 提交于 2019-12-20 22:39:18
Linux下undefined reference to ‘pthread_create’问题解决 在试用Linux 线程模块时,试用pthread_create 函数。 编译命令为 gcc main.c -o test 时,会出现如下错误 /tmp/ccIvH3bU.o: In function `main': main.c:(.text+0x81): undefined reference to `pthread_create' collect2: error: ld returned 1 exit status 问题的原因:pthread不是linux下的默认的库,也就是在链接的时候,无法找到phread库中哥函数的入口地址,于是链接会失败。 解决:在gcc编译的时候,附加要加 -lpthread 参数即可解决。 试用如下命令即可编译通过 gcc main.c -o test -lpthread #include <unistd.h> #include <pthread.h> #define NUM 10 int count; void* thread_func(void *arg) { count++; printf("count %d\n", count); return; } int main() { pthread_t tid[NUM]; int i = 0; for