zoukankan      html  css  js  c++  java
  • 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! 
    ");
    
            /* 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)
    ",
                            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系统的默认库)

  • 相关阅读:
    C++ 不用 < > 与 : ?运算符判断 a,b大小
    CentOS7 MariaDB10
    CentOS Linux 挂载NTFS
    Linux访问Windows共享
    Emacs配置与插件集记录
    驱动精妙耍流氓,强制安装"新毒霸"
    C# TextBox控件之大小写自动转换
    生活随笔
    显示外网IP
    MySql
  • 原文地址:https://www.cnblogs.com/craigtao/p/4999392.html
Copyright © 2011-2022 走看看