Linux编程实践----第一个多线程程序
来源:互联网 发布:彩票11选5遗漏数据分析 编辑:程序博客网 时间:2024/06/04 08:13
#include<iostream>#include<sys/types.h>#include<stdlib.h>#include<pthread.h>#include<unistd.h>#include<string.h>using namespace std;int num=0;void *thread_function1(void *arg){ cout<<"Thread 1 is running! The global variable is "<<*((int *)arg)<<endl; cout<<"Let thread 1 change it to 1"<<endl; *((int *)arg)=1;}int main(){ cout<<"Main thread is running.The global variable is "<<num<<endl; pthread_t thread; int id; id=pthread_create(&thread,NULL,thread_function1,(void *)(&num)); if(0!=id) { cout<<"Thread creation failed!"<<endl; exit(EXIT_FAILURE); } sleep(10); cout<<"Main thread is going on! The global ivariable is "<<num<<endl; exit(0);}
1.编译时首先定义宏_REENTRANT。程序只是为了验证功能,不能作为最后的产品代码。此测试程序也没有一定的命名规范,Just for learning!
2.执行结果为:
0 0
- Linux编程实践----第一个多线程程序
- 第一个LINUX多线程程序
- 第一个Linux程序
- Linux编程之一:创建第一个C/C++程序
- 【Linux环境编程入门】三、第一个程序
- windows编程第一个程序
- Linux多线程编程(实践)
- 微信小程序实践 第一个项目
- 编程第一课 第一个程序
- 第一个 Linux c 程序
- 第一个linux串口程序
- Linux上第一个程序
- linux驱动第一个程序
- 第一个c++程序编程总结(一)
- windows编程--第一个WinMain程序
- Unix网络编程第一个程序
- QT编程,第一个小程序
- WinPcap编程0x0200 第一个程序
- Java学习笔记一
- Java中的IO流
- 北京邮电大学《Hadoop大数据处理》课程2014年讲义-C10.Hive Pig
- Web Services 平台元素
- 在虚拟机上安装Ubuntu以及在Windows下配置PHP环境
- Linux编程实践----第一个多线程程序
- hibernate中利用criteria分页的实现
- Linux的inode的理解
- hdu 2602 and poj 3624(01背包)
- ZooKeeper应用实例:Java实现与ZooKeeper的连接
- C#中使用Split分隔字符串的技巧
- Mysql 获取排列序号。
- Java线程(一):线程安全与不安全
- Android ListView字母索引(仿微信通讯录列表)