Function: pthread_create

Function:pthread_create

do what:To create a new thread

head file

include <pthread.h>

prototype

int pthread_create( pthread_t *thread,
                    pthread_attr_t *attr,
                    void *(*start_routine)(void *), void *arg );     

explain

demo: ptcreate.c

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

void *myThread( void *arg) {

        printf("thread ran! \n");

        /* terminate the thread */
        pthread_exit(NULL);
}

int main() {

        int ret;
        pthread_t mythread;
        void *point_null;

        ret = pthread_create( &mythread, NULL, myThread, NULL );

        if (ret != 0) {
                printf( "Can‘t create pthread (%s)\n",
                        strerror(errno) );
                exit(-1);
        }
        pthread_join(mythread, &point_null); //wait for thread to terminate itself
        return 0;
}
 

compile command:

gcc ptcreate.c -o ptcreate -lpthread

because function pthread is not the default library of linux system,

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

时间: 2024-07-30 11:49:25

Function: pthread_create的相关文章

转载:多线程整理

pthread多线程编程整理 1 Introduction 不用介绍了吧… 2 Thread Concepts 1.     Thread由下面部分组成: a.     Thread ID b.     Stack c.     Policy d.     Signal mask e.     Errno f.      Thread-Specific Data 3 Thread Identification 1.     pthread_t用于表示Thread ID,具体内容根据实现的不同而不

life of a NPTL pthread

这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=91 March 7, 2013 life of a NPTL pthread Filed under: concurrency,linux,posix — Tags: NPTL, pthread — Raison @ 12:52 pm (Original Work by Peixu Zhu) NPTL pthread is the default pthread imple

pthread多线程编程的学习小结

pthread多线程编程的学习小结 程序员必上的开发者服务平台 —— DevStore pthread多线程编程整理 1 Introduction 不用介绍了吧… 2 Thread Concepts 1.     Thread由下面部分组成: a.     Thread ID b.     Stack c.     Policy d.     Signal mask e.     Errno f.      Thread-Specific Data 3 Thread Identification

几个linux实验

环境 Ubuntu18.04.3(desktop-amd64)4核70G.Linux 5.4.1(最新).Deepin15.11(Ubuntu用起来不习惯).draw.io.VSCode Part1 目的 添加系统调用,扩展操作系统的功能. ①基本配置 ①先下载源码,我是从主机上下载后传到虚拟机上的,放到/usr/src文件夹中.下载链接 ②解压,注意权限要切换成root. tar -xavf linux-5.4.1.tar.xz ③依赖包,注意安装前更新一下apt,最好换一下源,碰到错误缺什么

通过百度echarts实现数据图表展示功能

现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解echarts是个怎样技术的开发者来说,可以到echarts官网进行学习了解,官网有详细的API文档和实例供大家参考学习. 2.以下是我在工作中实现整理出来的实例源码: 公用的支持js文件 echarts.js.echarts.min.js,还有其他的图表需要支持的js文件也可以到官网下载 echa

帮同学做的大一大作业:《我的家乡—郑州》

---恢复内容开始--- 最近在上海上学的一个高中同学让我帮忙,帮她做她们的计算机课程大作业. 由于关系不错我也不好意思拒绝就帮忙做了,因为这个学期刚刚开始接触HTML5和css,所以制作过程中有很多不懂的,而且由于HTML5是选修课,一星期只有一节,所以做这个花费了比较多的时间,这个网站是我制作的第一个网站,比较有纪念意义,所以发在博客上,作为纪念. 通过去做这个作业,我了解到很多课上学不到的东西.因为没有美工,从头到尾,都是我一个人在臆想,刚开始的时候,根本无从下手,我去参考别人做的家乡网站

线程异常:undefined reference to &#39;pthread_create&#39; 处理

源码: #include <stdio.h> #include <pthread.h> #include <sched.h> void *producter_f (void *arg); void *consumer_f (void *arg); int buffer_has_item=0; pthread_mutex_t mutex; int running =1 ; int main (void) { pthread_t consumer_t; pthread_t

pthread_create

//\glibc-2.24\sysdeps\nptl\pthread.h/* Create a new thread, starting with execution of START-ROUTINE getting passed ARG. Creation attributed come from ATTR. The new handle is stored in *NEWTHREAD. */ extern int pthread_create (pthread_t *__restrict _

类成员函数作为pthread_create函数参数

from:http://www.cnblogs.com/shijingxiang/articles/5389294.html 近日需要将线程池封装成C++类,类名为Threadpool.在类的成员函数exec_task中调用pthread_create去启动线程执行例程thread_rounter.编译之后报错如下: spfs_threadpool.cpp: In member function ‘int Threadpool::exec_task(task*)’: spfs_threadpoo