C/C++ 用 pthread 进行多线程开发 |
您所在的位置:网站首页 › linux使用gcc编译c语言程序多进程 › C/C++ 用 pthread 进行多线程开发 |
作为一个程序员,不管你用的开发语言是 C、C++、Java、Python 或者其它,你总会需要处理多任务。 多线程是实现多任务处理的一种最常用的手段,线程相比进程而言显得轻量级。 本文分享如何用 C 进行多线程开发。 核心在于 pthread 这个库。 调用 pthread_create()函数就可以创建一个线程。 它的函数原型如下: #include extern int pthread_create (pthread_t *__restrict __newthread, const pthread_attr_t *__restrict __attr, void *(*__start_routine) (void *), void *__restrict __arg)pthread.h 是它的库。 参数说明: 第一个参数是 pthread_t* 也就是代表线程实体的指针 第二个参数为了设置线程的属性,一般为 NULL 第三个参数是线程运行时的函数,这是个函数指针。 第四个参数也是一个指针,它是用来将数据传递进线程的运行函数 下面用一个代码来示例说明。 #include #include #include #include //线程函数 void *test(void *ptr) { int i; for(i=0;i |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |