pthread

Linux 多线程编程(实现生产者消费者模型)

我是研究僧i 提交于 2019-12-04 01:47:08
Linux 多线程编程 线程分类 线程按照其调度者可以分为用户级线程和内核级线程两种。 内核级线程 在一个系统上实现线程模型的方式有好几种,因内核和用户空间提供的支持而有一定程度的级别差异。最简单的模型是在内核为线程提供了本地支持的情况,每个内核线程直接转换成用户空间的线程。这种模型称为“1:1线程模型(threading)”,因为内核提供的线程和用户的线程的数量是1:1。该模型也称为“内核级线程模型(kernel-level threading)”,因为内核是系统线程模型的核心。 Linux 中的线程就是“1:1线程模型”。在linux内核中只是简单地将线程实现成能够共享资源的进程。线程库通过系统调用 clone() 创建一个新的线程,返回的“进程”直接作为用户空间的线程。也就是说,在Linux上,用户调用线程和内核调用线程基本一致。 Linux的线程实现是在核外进行的,核内提供的是创建进程的接口do_fork()。内核提供了两个系统调用clone()和fork(),最终都用不同的参数调用do_fork()核内API。当然,要想实现线程,没有核心对多进程(其实是轻量级进程)共享数据段的支持是不行的,因此,do_fork()提供了很多参数,包括CLONE_VM(共享内存空间)、CLONE_FS(共享文件系统信息)、 CLONE_FILES(共享文件描述符表)、CLONE

linux获取线程ID

半腔热情 提交于 2019-12-03 13:25:05
pthread_self()获取当选线程的ID。 这个ID与pthread_create的第一个参数返回的相同。 但是与ps命令看到的不同,因此只能用于程序内部,用于对线程进行操作。 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> void* fun(void* p) { printf("child thread id=%lu\n",pthread_self());//获取当前线程ID //sleep(100); return NULL; } int main(int argc,char* argv[]) { pthread_t tid; printf("main thread id=%lu\n",pthread_self());//获取当前线程ID pthread_create(&tid,NULL,fun,NULL); printf("child's tid=%lu\n",tid); sleep(100); //wait child return 0; } 来源: https://www.cnblogs.com/mathyk/p/11796872.html

多线程API

六月ゝ 毕业季﹏ 提交于 2019-12-03 12:11:48
线程创建 #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); thread: 指向pthread_t结构类型的指针,用来和该线程交互 attr:用于指定该线程相关属性,一般设置为NULL,使用默认属性 start_routine:指定线程运行的函数,例如如果参数为int,返回int,那么应该是: int (*start_routine)(int) arg:指定线程函数的参数 线程完成 #include <pthread.h> int pthread_join(pthread_t thread, void **retval); thread: 指定需要等待的线程 retval:指向你希望得到的返回值 示例: #include <pthread.h> #include <stdio.h> #include <stdlib.h> void *mythread(void *arg) { int m = (int)arg; printf("%d\n", m); return (void *)(arg + 1); } int main(int argc, char *argv[]) {

C,线程池

十年热恋 提交于 2019-12-03 11:58:01
/* 线程池组成: 1、管理者线程:创建并管理线程,包括添加、删除、销毁线程,添加新任务 2、工作线程:线程池中的线程,执行管理者分配的任务 3、任务接口:任务要实现的接口,供工作线程调用 4、任务队列:存放没有处理的任务,缓冲作用 */ #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <assert.h> #include <stdio.h> #include <string.h> #include <signal.h> #include <errno.h> #include "threadpool.h" #define DEFAULT_TIME 10 /*10s检测一次*/ #define MIN_WAIT_TASK_NUM 10 /*如果queue_size > MIN_WAIT_TASK_NUM 添加新的线程到线程池*/ #define DEFAULT_THREAD_VARY 10 /*每次创建和销毁线程的个数*/ #define true 1 #define false 0 //任务接口 typedef struct { void *(*function)(void *); /* 函数指针,回调函数 */ void *arg; /* 上面函数的参数 */ } threadpool

Does sleep() interfere with scanf()?

匿名 (未验证) 提交于 2019-12-03 10:10:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have two threads xThread : Continuously Prints X on the console inputThread : Gets input from the stdin The continuous printing stops when the user enters 'C' or 'c' #include <stdio.h> #include <sys/select.h> #include <pthread.h> #define S sleep ( 0 ) int read_c = 0 ; pthread_mutex_t read_c_mutex = PTHREAD_MUTEX_INITIALIZER ; void * inputThread_fn ( void * arg ) { char inputChar ; while ( 1 ) { S ; printf ( "\nChecking input" ); scanf ( "%c" ,& inputChar ); if ( inputChar == 'C' || inputChar == 'c' ) { pthread_mutex_trylock (&

Thread timer in c

匿名 (未验证) 提交于 2019-12-03 09:02:45
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: can someone help me to transform my code to a working code : #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* call back function - inform the user the time has expired */ void timeout_call_back() { printf("=== your time is up ===\n"); // doing some other stuff } /* Go to sleep for a period of seconds */ static void* start_timer(void *args) { /* function pointer */ void (*finish_function)(); int seconds = *((int*) args); pthread_mutex_lock(&mutex); // I want

Why are the atomics much slower than the lock in this uncontended case?

匿名 (未验证) 提交于 2019-12-03 08:59:04
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I wrote something using atomics rather than locks and perplexed at it being so much slower in my case I wrote the following mini test: #include <pthread.h> #include <vector> struct test { test(size_t size) : index_(0), size_(size), vec2_(size) { vec_.reserve(size_); pthread_mutexattr_init(&attrs_); pthread_mutexattr_setpshared(&attrs_, PTHREAD_PROCESS_PRIVATE); pthread_mutexattr_settype(&attrs_, PTHREAD_MUTEX_ADAPTIVE_NP); pthread_mutex_init(&lock_, &attrs_); } void lockedPush(int i); void atomicPush(int* i); size_t index_; size_t size_; std

C语言-线程

感情迁移 提交于 2019-12-03 08:27:45
线程c程序同时执行很多任务,与进程相比较,代码量小,执行速度快 每次执行线程函数都需要检查错误调用error函数 1.如何调用线程? 线程函数的返回类型为void * 需要pthread.h头文件支持 创建线程: pthread_t xxx; pthread_create(&xxx, NULL, 执行的函数名, NULL) 回收线程 void * xxxx; pthread_join(线程名字, &xxxx) 用来防止线程同时调用相同变量,需要建立互斥锁 创建互斥锁: pthread_mutex_t xxx=PTHREAD_MUTEX_INITIALIZER 使用互斥锁 pthread_mutex_lock(&xxx); 打开互斥锁 pthread_mutex_unlock(&xxx); 当使用了pthread.h头文件后,gcc对源码的编译需要加入-lpthread参数连接pthread库 来源: https://www.cnblogs.com/renren-study-notes/p/11784962.html

RTOS学习笔记

萝らか妹 提交于 2019-12-03 07:18:59
一、RTOS系统配置 总的来说,一般的发行版本的linux都不是实时系统,一般的发行版本linux准确地说是分时系统,更强调多用户的分时调用。 因次在设计的过程中,考虑的更多的是系统的并发特性,保证及时响应每个用户的任务应用处理需求。而在实时系统中,需要 保证实时任务在规定内时间完成任务,因此如何保证实时任务能够实时的执行是操作系统调度机制的核心问题。正因此可抢占式 调度策略是操作系统内核调度策略的必备选项,保证实时任务的优先级最高,一旦就绪完毕,无论系统的负载如何,都能够获取 CPU的使用权,保证实时任务执行。 一句话总结就是:更改内核配置,把默认分时间片调度策略变为可抢占。 Getting the sources First, the kernel version should be chosen. After this, take a look if the PREEMPT_RT patch is available for this particular version. The source of the desired version has to be downloaded (for the Linux kernel as well as for the PREEMPT_RT patch). This example is based on the Linux

DDOS 单例

百般思念 提交于 2019-12-03 06:26:52
DDOS.H #pragma once //g++ ../../../Main.cpp ../../../DDOS.cpp -lpthread #include <stdio.h> #include <ctype.h> #include <unistd.h> #include <fcntl.h> #include <signal.h> #include <sys/time.h> #include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <netdb.h> #include <errno.h> #include <stdlib.h> #include <time.h> #include <arpa/inet.h> #include <pthread.h> // -lpthread // ./a.out www.baidu.com 80 struct ip { unsigned char hl; unsigned char tos; unsigned short total_len; unsigned short id; unsigned short frag_and_flags; unsigned char ttl; unsigned char proto; unsigned short