Linux多线程函数解析

告别GIS软件!用R语言ggplot2+ggmapcn绘制出版级世界地图(附5种投影对比图) 本文提供了一份完整的实践指南,教你如何利用R语言及其强大的ggplot2生态系统,结合ggmapcn包,绘制出出版级的世界地图。文章详细对比了五种经典地图投影(如摩尔维德、罗宾森投影)的代码实现与视觉差异,并讲解了如何添加比例尺、指北针等专业元素,最终导出高清PDF,实现从数据分析到专业可视化的全流程代码化操作,彻底告别传统GIS软件。 阅读详情
Linux多线程函数用得比较多的是下面的3个:pthread_create()pthread_exit()      pthread_join();

它们都是在头文件之中。编译时需要加静态库-lpthread

函数的说明如下:
(一) pthread_create是UNIX环境创建线程函数

     int pthread_create(

         pthread_t *restrict tidp,

         const pthread_attr_t *restrict_attr,
         void*(*start_rtn)(void*),

         void *restrict arg

     );

    返回值:若成功则返回0,否则返回出错编号。
    参数:
      第一个参数为指向线程标识符的指针。
      第二个参数用来设置线程属性。
      第三个参数是线程运行函数的起始地址。
      最后一个参数是运行函数的参数。

    另外,在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库。

    该函数返回成功时,由tidp指向的内存单元被设置为新创建线程的线程ID。attr参数用于制定各种不同的线程属性。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个万能指针参数arg,如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg的参数传入。
  由 restrict 修饰的指针是最初唯一对指针所指向的对象进行存取的方法,仅当第二个指针基于第一个时,才能对对象进行存取。对对象的存取都限定于基于由 restrict 修饰的指针表达式中。 由 restrict 修饰的指针主要用于函数形参,或指向由 malloc() 分配的内存空间。restrict 数据类型不改变程序的语义。 编译器能通过作出 restrict 修饰的指针是存取对象的唯一方法的假设,更好地优化某些类型的例程。


(二) pthread_exit(void* retval);
     线程通过调用pthread_exit函数终止自身执行,就如同进程在结束时调用exit函数一样。这个函数的作用是,终止调用它的线程并返回一个指向某个对象的指针。该指针可以通过pthread_join(pthread_t tpid, void **value_ptr)中的第二个参数value_ptr获取到。

(三) 函数pthread_join用来等待一个线程的结束。函数原型为:
   extern int pthread_join __P (pthread_t __th, void **__thread_return);
     第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程退出时的返回值。这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。如果执行成功,将返回0,如果失败则返回一个错误号。
     所有线程都有一个线程号,也就是Thread ID。其类型为pthread_t。通过调用pthread_self()函数可以获得自身的线程号。

     下面是一个简单的例子,子线程thread_fun会打出5次“this is thread_fun print!”然后调用pthread_exit退出,并返回一个指向字符串“this is thread return value!”的指针。在主函数里面调用pthread_join等待thread_fun线程结束,然后读取子线程的返回值到value中,再打印出来。

输出结果是:

pthread_create ok!
this is thread_fun print!
this is thread_fun print!
this is thread_fun print!
this is thread_fun print!
this is thread_fun print!
pthread exit value: this is thread return value!


源码如下:

01.#include
02.#include
03.#include
04.#include
05.#include
06.#include
07.//////////////////////////////////////////////////////
08.void *thread_fun(void *arg) {
09.        int i=0;
10.        char *value_ptr = "this is thread return value!\n";
11.        for (i=0; i<5; i++) {
12.             printf("this is thread_fun print!\n");
13.             sleep(1);
14.        }
15.        pthread_exit((void*)value_ptr);
16.}
17.//////////////////////////////////////////////////////
18.int main(int argc, char **argv) {
19.       pthread_t pid;
20.       int ret;
21.       void* value;
22.
23.       ret = pthread_create(&pid, NULL, thread_fun, NULL);
24.       if (ret) {
25.             printf("pthread_create failed!\nerrno:%d\n", errno);
26.             return -1;
27.       }
28.       printf("pthread_create ok!\n");
29.
30.       pthread_join(pid, &value);
31.       printf("pthread exit value: %s\n", value);
32.       return 0;
33.}


ArcGIS License Server点击启动没反应,许可管理器无法启动怎么办 4 当我们再打开ArcGIS License Server Administrator的时候。3 在【服务】中找到【ArcGIS License Manager】双击。无法启动ArcGIS License Server的情况。在使用ArcGIS的过程中,我们经常会遇到。专注GIS领域知识分享,交流行业软件技巧。欢迎关注,不定期分享GIS各种问题和技巧。1 右击我的电脑——【管理】服务状态——启动,确定即可。启动类型选择——自动。 阅读详情

相关推荐

从mseed到SAC格式转换全攻略:mseed2sac工具安装与批量处理技巧

本文详细介绍了如何将mseed格式的地震数据高效转换为SAC格式,涵盖mseed2sac工具的安装、单文件转换技巧、批量处理自动化实现以及元数据高级处理策略。通过具体命令和脚本示例,帮助地震学研究者快速掌握数据预处理的关键技术,提升波形数据分析效率。

ruby5的博客 927

Linux下多线程编程详解(一) --pthread_create

 参数点解: 1、每个线程都有自己的ID即thread ID,可以简称tid,呵呵,是不是想起什么来了?。。。对,和pid有点象。其类型为pthread_t,pthread_t在头文件/usr/include/bits/pthreadtypes.h中定义:          typedef unsigned long int pthread_t;          可以看成是线程...

lzhoufly的专栏 237

游戏经济数值模型的搭建过程——以标准卡牌游戏为例

一、总体规划 1.玩家分类 首先我们按照下表对玩家进行分类 付费能力即为充值额度;充值行为比如购买月卡/购买钻石;消费行为比如用钻石抽卡 在这里我们假设玩家是理性的,即玩家会以追求更高的性价比为目标进行充值与消费(即小R会优先买首冲+月卡,而不是买一个无用的无属性皮肤) 不同类型玩家之间的差异,实际上就主要由这些行为的差异而决定。 注:早期游戏制作的时候,活跃程度也是区分玩家类型的一个重要依据。不过目前现在游戏中有各种手段抹平了不同活跃玩家的差异(体力限制,活跃时间长的收益递减)..

dudu3332的博客 4228

【C++ 语言】线程 ( 线程创建方法 | 线程标识符 | 线程属性 | 线程属性初始化 | 线程属性销毁 | 分离线程 | 线程调度策略 | 线程优先级 | 线程等待 )

线程创建方法 线程执行函数 线程标识符 线程属性 线程属性 1 ( 分离线程 | 非分离线程 ) 线程属性 2 ( 线程调度策略 ) 线程属性 3 ( 线程优先级设置 ) 线程等待 线程代码示例

让 学习 成为一种 习惯 ( 韩曙亮 の 技术博客 ) 1848

elmos 524系列芯片笔记

芯片的工作工作原理:集成芯片Elmos524.03有发射回路和接收回路,手册有关的内部原理图,大致如下: 芯片会产生固有频率的电压信号,信号通过引脚DRV1、2输出给外部压电陶瓷片,产生固定频率的机械波,也就是所谓的超声波,超声波遇到障碍物时会反射,反射信号经过AINS、D引脚进入芯片,在芯片内部先后经过GAIN放大,滤波然后进入比较器,滤波后的信号(signal)同预先设定的阈值(threshold)比较产生信号输出。此信号最终会连接MCU处理器,需要重点关注。 基于这个内部的工作原

qq_38230916的博客 2483

国产化软件设计:遵循信创要求,助力信息技术自主创新

支持国产操作系统、CPU、数据库和中间件。确保网络通信的身份鉴别、数据传输机密性和完整性。实现应用和数据层的身份鉴别、数据传输机密性完整性、数据存储机密性完整性。依托法院密码服务平台提供的商用密码能力。

qq_41186565的博客 3371

Linux下多线程编程思考与学习----01(线程创建pthread_create函数详解)

pthread_create ()线程创建的使用与总结

HHzzy0123的博客 3225

AI人工智能代理工作流 AI Agent WorkFlow:在股票市场预测中的应用

AI人工智能代理工作流 AI Agent WorkFlow:在股票市场预测中的应用 作者:禅与计算机程序设计艺术 / Zen and the Art of Computer Programming 关键词: AI Agent, W

AI天才研究院 859

Linux中的线程创建函数pthread_create函数

Linux系统中用户线程的创建和运行方法,即pthread_create函数的用法,并举了一个简单的实例。

baidu_38797690的博客 5577

KEPServerEX物联网网关(IoT Gateway)REST客户端代理(非原创)

(中文版)资料来源于【OPC-KEPServerEX学习KEPWare交流】905485143群的【455196383】【零下一度】 一。KEPServerEX物联网网关REST客户端代理演示 在KSE中,首先构建一个带有通道/设备/标签的模拟器系统。 例如,下面是三个随机项目。 直接到IoT网关插件,添加新代理。 从类型列表中选择REST Client。 终结点...

-雲飛兄-博客 5805

CAD二次开发c# .net加载cui\cuix菜单文件

public static void LoadMyCui(string cuiFile) { Document doc = Application.DocumentManager.MdiActiveDocument; object oldCmdEcho = Application.GetSystemVariable("CMDECHO"); object oldFileDia = Application.GetSys..

2127

Linux并发处理方式——多线程pthread

Linux并发处理方式——多线程pthread 前言: 简单来说,线程是进程的一条执行路径,在Unix系统下也叫做轻量级进程。所有的线程都在同一个进程下运行的,所以所有线程都可以共享进程里的资源,同时每个线程也有属于自身的栈空间、寄存器环境等等,每个线程执行着自己的任务。这时候又不得不考虑进程里面的资源分配问题了,如何解决死锁问题;父进程与子进程之间的关系,如何关闭子线程。 一、相关函数 1、父、子线程 主线程和子线程的默认关系是:无论子线程执行完毕与否,一旦主线程执行完毕退出,所有子线程执行都会终止。这时

weixin_45062087的博客 1503

【AHE数据集】历史/未来全球网格热排放数据 PF-AHF

AHE数据集

WW、forever的博客 127

Linux 多线程函数解析

作者: yao_guet (1 篇文章) 日期: 二月 14, 2012 在 5:32 下午 Linux多线程函数解析 Linux多线程函数用得比较多的是下面的3个 pthread_create(),pthread_exit(),pthread_join();它们都是在头文件之中。编译时需要加静态库-lpthread 下面是函数的说明:   pthread_create是UNIX环境

wxl1986622的专栏 617

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Linux多线程编程:深入解析pthread_detach函数

本文深入解析Linux多线程编程中的pthread_detach函数,强调线程资源管理的重要性。通过对比pthread_join,阐明其自动回收资源、提升并发度的优势,并提供基础用法与网络服务器实战案例。文章指出使用时的黄金法则(必须选择join或detach之一)及常见错误码,最后总结其性能优势(内存效率、系统稳定性)。符号系统(如⚡、💎)贯穿全文,增强技术要点的可视化表达。

软件科学-郝学胜 1848

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

linux多线程调用同一个函数解析

原文地址:http://blog.youkuaiyun.com/mq_ydn3102/article/details/8546722 问题背景:在工作中遇到过一个问题,就是在两个线程同时调用同一个函数的时候,到底是应该在这个函数的内部什么处理上加上互斥。原因又是什么? 为了调查这个问题,我决定从以下三方面调查 1.线程中函数的调用 2.同一个进程中的两个线程调用同一个处理函数 3.两个不同进程中

dragon_cdut的博客 6403

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

上一篇: 数据挖掘中的分类和聚类
下一篇: 基于linux的文件传输器实现详解----客户端实现详解
wustdatamining
博客等级 码龄15年 12粉丝 81原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值