Linux多线程实例练习 - pthread_create()
Linux多线程实例练习 pthread_create():创建一个线程
int pthread_create(pthread_t *tidp,
const pthread_attr_t *attr,
(void*)(*start_rtn)(void*),
void *arg);
1、代码如下 xx_pthread_create.c
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h> // for gettimeofday #define debugMsg(fmt, arg...)\
do{\
unsigned long lSec = ; unsigned long lUSec = ;\
getTimeuSec(&lSec, &lUSec);\
printf("[%ld.%06ld]:", lSec, lUSec);\
printf(fmt, ##arg);\
}while() int getTimeuSec(unsigned long *lSec, unsigned long *lUSec)
{
struct timeval start;
gettimeofday( &start, NULL );
*lSec = start.tv_sec;
*lUSec = start.tv_usec; return ;
}
void * doPrint(void *arg)
{
int i = ;
while(i < )
{
debugMsg("pthread %2d; main %d\n", i++, *(int*)arg);
usleep();
} return NULL;
} int main()
{
pthread_t pid;
int iX = ;
pthread_create(&pid, NULL, doPrint, &iX);
while(iX <= + )
{
debugMsg("main : %d\n", iX++);
usleep();
} return ;
}
2、CentOS 下编译通过
g++ -g -c -o xx_pthread_create.o xx_pthread_create.c
g++ -g -o xx_pthread_create xx_pthread_create.o -lpthread
3、运行结果
[1422496189.763862]:main :
[1422496189.764341]:pthread ; main
[1422496189.965627]:pthread ; main
[1422496190.065601]:main :
[1422496190.166510]:pthread ; main
[1422496190.366393]:main :
[1422496190.367391]:pthread ; main
[1422496190.568275]:pthread ; main
[1422496190.667215]:main :
[1422496190.769157]:pthread ; main
[1422496190.968039]:main :
[1422496190.970323]:pthread ; main
[1422496191.171922]:pthread ; main
[1422496191.269869]:main :
[1422496191.373803]:pthread ; main
[1422496191.571696]:main :
[1422496191.574958]:pthread ; main
[1422496191.776566]:pthread ; main
[1422496191.873512]:main :
[1422496191.977457]:pthread ; main
[1422496192.174348]:main :
[1422496192.178362]:pthread ; main
[1422496192.379214]:pthread ; main
[1422496192.475159]:main :
[1422496192.580095]:pthread ; main
[1422496192.776006]:main :
[1422496192.781267]:pthread ; main
[1422496192.981968]:pthread ; main
[1422496193.076864]:main :
[1422496193.182797]:pthread ; main
[1422496193.377656]:main :
[1422496193.384089]:pthread ; main
[1422496193.584595]:pthread ; main
[1422496193.678472]:main :
[1422496193.785406]:pthread ; main
[1422496193.980296]:main :
[1422496193.987689]:pthread ; main
[1422496194.189201]:pthread ; main
[1422496194.281149]:main :
[1422496194.390049]:pthread ; main
[1422496194.582987]:main :
[1422496194.590944]:pthread ; main
[1422496194.792793]:pthread ; main
[1422496194.883821]:main :
[1422496194.993852]:pthread ; main
[1422496195.184826]:main :
[1422496195.195665]:pthread ; main
[1422496195.397447]:pthread ; main
[1422496195.486468]:main :
[1422496195.598408]:pthread ; main
[1422496195.787329]:main :
Linux多线程实例练习 - pthread_create()的更多相关文章
- Linux多线程实例练习 - pthread_cancel()
Linux多线程实例练习 - pthread_cancel 1.代码 xx_pthread_cancel.c #include <pthread.h> #include <stdio ...
- Linux多线程实例练习 - pthread_exit() 与 pthread_join()
Linux多线程实例练习 - pthread_exit 与 pthread_join pthread_exit():终止当前线程 void pthread_exit(void* retval); pt ...
- Linux多线程实例解析
Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...
- Linux多线程实例 定时重启httpd和mysqld
#include <stdio.h> #include <pthread.h> void *start_routine(void *arg) { while(1) { syst ...
- Linux多线程编程实例解析
Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...
- Linux 多线程应用中如何编写安全的信号处理函数
http://blog.163.com/he_junwei/blog/static/1979376462014021105242552/ http://www.ibm.com/developerwor ...
- 笔记整理--Linux多线程
Unix高级环境编程系列笔记 (2013/11/17 14:26:38) Unix高级环境编程系列笔记 出处信息 通过这篇文字,您将能够解答如下问题: 如何来标识一个线程? 如何创建一个新线程? 如何 ...
- Linux多线程编程初探
Linux线程介绍 进程与线程 典型的UNIX/Linux进程可以看成只有一个控制线程:一个进程在同一时刻只做一件事情.有了多个控制线程后,在程序设计时可以把进程设计成在同一时刻做不止一件事,每个线程 ...
- 【操作系统作业-lab4】 linux 多线程编程和调度器
linux多线程编程 参考:https://blog.csdn.net/weibo1230123/article/details/81410241 https://blog.csdn.net/skyr ...
随机推荐
- [WebService] the namespace on the "definitions" element, is not a valid SOAP version
公司对外通过webservice访问别人接口,对方webservice IP地址发生变化,切换过去之后,始终报错,在网上搜索了各种办法之后,暂时总结该问题几种可能解决办法,待真正解决时用的到. 异常详 ...
- Java后端WebSocket的Tomcat实现
转自:http://blog.chenzuhuang.com/archive/28.html 文章摘要随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5 ...
- C/C++面试题
第一部分:基本概念及其它问答题 1. 关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被声明为静态的变量 ...
- (C#基础) byte[] 之初始化, 赋值,转换。(转)
byte[] 之初始化赋值 用for loop 赋值当然是最基本的方法,不过在C#里面还有其他的便捷方法. 1. 创建一个长度为10的byte数组,并且其中每个byte的值为0. byte[] myB ...
- 【JDBC 报错】Connections could not be acquired from the underlying database!
项目启动报错: [2016-07-13 10:04:15,074] ERROR org.apache.ibatis.executor.BaseExecutor Could not get a data ...
- AngularJS学习之模型
1.ng-model指令:可以将输入域的值与AngularJS创建的变量绑定,用于绑定应用程序数据到HTML控制器(input,select,textarea)的值: <div ng-app=& ...
- Codeforces Round #356 (Div. 2)-B
B. Bear and Finding Criminals 链接:http://codeforces.com/contest/680/problem/B There are n cities in B ...
- 《用delphi开发共享软件》-15.2桌面提示器
打开一个配置文件: 打开一个配置文件 操作TStringGrid Procedure EmptyGrid(Var sg:TStringGrid); Var i:Integer; begin do sg ...
- vector初始化
对vector对象来说,直接初始化的方式适用于3种情况:1.初始值已知且数量少:2.初始值是另一个vector对象的副本:3.所有元素的初始值都一样.此外还有一种更常见的情况是4.创建一个vector ...
- JavaScrip实现3D旋转动态效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...