thread basic function

  • The thread library is not included in the system library, it needs to be linked when compiling gcc -l pthread  
  • Threads include the main thread and the secondary thread, the threads are equal, the shared data is short, and the stack space is different. The only difference is that the stack space is different.
  • tid do not print

2. pthread_cread (tid,&attr,func,&arg) //successful return 0, error return error code

  tid: thread id number; attr: thread attribute (mostly fill in NULL, will be explained later)

  func: startup routine; args: arguments to startup routine

  如:pthread_create(tid,NULL,doWork,NULL);

3.pthread_join(tid,void **retval)

  Function: Wait for the thread whose thread number is tid to recycle thread resources after the execution ends, similar to the wait() function of the process, which will block. Any thread can recycle other threads

  tid: the thread waiting for recycling

  retval: 1. Can be used as a thread end return code. Determine whether the thread ends normally

      2. Receive the returned data space at the end of the thread

4.pthread_exit(void *retval)

  Function: End the thread and return the end code: 0, 1 can be used to indicate whether the thread returns normally

  You can also return the address of a data, which is convenient for other threads to call

5. Pthread_cancel(tid)  //Return 0 on success, erro on failure

  Function: Send a cancellation request to the thread whose thread number is tid

  Note: Successful sending does not mean that the thread tid will end

  Reason: Each thread has a cancellation option, and the corresponding cancellation request depends on the cancellation option.

  Cancellation options include: Cancellable Status and Cancellable Type

There are two types of cancelable states :

  • PTHREAD_CANCEL_ENABLE: Can be cancelled, but the specific behavior depends on the cancellation type
  • PTHREAD_CANCEL_DISABLE: cannot be cancelled

  pthread_setcancelstate():// Set the thread cancellation state

Cancellable types also include two :

  • PTHREAD_CANCEL_DEFERRED       delayed cancellation (calling pthread_cancel does not immediately terminate the thread, but continues to run until a certain cancellation point is reached )
  • PTHREAD_CANCEL_ASYNCHRONOUS     walk canceled (thread can be canceled at any time)

  pthread_setcanceltype()   //Set the thread cancellation type (understanding: a mechanism similar to process locking, it cannot be cancelled during this period, ensuring data integrity)

  pthread_testcancel () //Set the cancellation point

6. pthread_self(void) //Get the thread id number

7. pthread_equal(pthread_t tid1, pthread_t tid2); //Compare the id numbers of two threads, return 0 if they are equal, and return non-0 if they fail

8. When the thread exits, the scheduled exit cleaner can be called (similar to atexit)

  void pthread_cleanup_push(void (*rtn)(void *),void *arg);//将清理程序入栈(清理程序安装)
       //execute为0弹出不执行 非0弹出并执行
  void pthread_cleanup_pop(int execute);   //这两个函数成对出现,pop()可以写在return后面不执行,但必须要写。

  注:当执行以下动作时调用清理函数
      .调用pthread_exit
      .响应取消请求
      .execute非零调用pop

9.   int pthread_detach(pthread_t tid); //如果不关心线程返回状态,可以使线程进入分离状态,分离的线程退出时,底层资源会立即回收,不需要再调用pthread_join()来回收资源,

10.线程属性

  1. 线程的分离状态属性  :可以用函数pthread_detach()函数来代替
  2. 线程的栈未尾警戒缓冲区大小 
  3. 线程栈最低地址
  4. 线程栈大小:    当有多个线程高并发时需要设置该属性

    指定stack大小不能小于       #define PTHREAD_STACK_MIN       16384   

   int pthread_attr_getstack(const pthread_attr_t *attr,void **stackaddr,void*size);
  int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,int size);//设置栈大小,但需要自  己申请一块未使用的空间

int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);//设置栈大小,系统自动分配空间

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324879544&siteId=291194637