(reprint) Pthreads thread (i) Basic API

Source: Internet
Author: User

Pthreads thread (i) Basic API

1. Create a thread
int Pthread_create (pthread_t *restrict_ptid,
Const pthread_attr_t *RESTRICT_ATTR,
void * (*start_routine) (void*), void *restrict_arg);
      Ptid is a pthread_t * type pointer, pthread_t is a data structure similar to pid_t, which represents the thread ID attr indicates that the thread creates the property, uses the system default property if NULL, Start_routine is the main function of the thread, its argument is a pointer of void * type, and the return value is a pointer to the void * type, and Arg is the argument that the thread creator passed to the new thread. That is start_routine parameters, if you need to pass to the Start_routine function more than one parameter, then you need to put these parameters into a structure, and then the structure of the address as a Restrict_arg parameter passed in. (If thread creation succeeds, 0 is returned.) If thread creation fails, the error number is returned)
Note: There is no fork between the thread creator and the new Thread () Call that parent-child relationship, which is a peer relationship. After calling Pthread_create () to create a thread, the thread creator and the new thread which runs first are indeterminate, especially on the multi-processing machine.

2. Terminating thread
  void Pthread_exit ( void *value_ptr);
      thread calls Pthread_exit () ends itself, parameter value_ptr is called as the return value of the thread Pthread_ The thread used by the join. Because multiple threads in a process are shared data segments, usually after a thread exits, the resources that are consumed by the exit threads are not freed as the thread terminates, but the resources can be synchronized and freed with the Pthread_join () function.


3.pthread_self (): This function returns the ID of the calling thread. This value is the same as the value returned by the *thread parameter that was used when calling Pthread_create to create this thread.
Note:   Thread IDs only guarantees uniqueness within a process. The thread ID may be reused after an end thread joined (using join to wait for a thread to end), or an detached state that is terminated. The thread ID returned by pthread_self () is not the same as the kernel thread ID that was obtained by calling Gettid ().


4.pthread_equal (): Compares the thread ID and the size of the thread ID is meaningless.
Introduction Reason: Thread ID type is pthread_t type in threads, because the POSIX standard is adopted in Linux, so, under different system, the type of pthread_t is different, for example, under UBUNTN, is unsigned long type, In the Solaris system, it is the unsigned int type. In FreeBSD, it is a structural problem pointer. So you cannot use the = = interpretation directly, but should use pthread_equal to judge.


3. Cancel thread
  int pthread_cancel (pthread_t thread);
Note: If you want to terminate each thread when the entire program exits, you should send the Cancel command after a successful Use the  pthread_join function to wait until the specified thread has completely exited before proceeding, otherwise it is easy to create a "segment error".

4. Connection thread (blocked)
waits for thread to end, and sets the return value of *value_ptr to thread. Pthread_join blocks the caller until the thread ends. When the function returns, the resource that is waiting for the thread is retracted. If the process has ended, the function returns immediately. and thread-specified threads must be joinable.
thread termination There are several methods:
1. Return from the main function;  
2. Call Pthread_exit () yourself;
3. Other threads call Pthread_cancel ();
4. Any thread in the process that the thread belongs to calls exit () causes all threads to end.

5. Separating threads
int Pthread_detach (pthread_t thread);
The semantics of separating threads is that the system can reclaim its private data after the thread ends.
Note: Pthread has two states joinable state and unjoinable state the default state of a thread is joinable if the thread is joinable state when the thread function returns to its own exit or Pthread_ Exit does not release the stack and thread descriptors that the thread occupies (a total of more than 8K). These resources will be released only if you call Pthread_join. These resources are automatically freed when the thread function exits or pthread_exit, if the threads are in a unjoinable state. The Unjoinable property can be specified at pthread_create time, or Pthread_detach itself in the thread after the thread is created, such as: Pthread_detach (Pthread_self ()) or the parent thread calls Pthread_detach (thread_id) to end the corresponding child process.

Original address: http://www.cnblogs.com/dongsheng/p/4184153.html

(reprint) Pthreads thread (i) Basic API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.