Linux thread note 1 creation thread

Source: Internet
Author: User

1 Thread-to-process comparison

Here is a detailed description of the notes

http://blog.csdn.net/laviolette/article/details/51506953

2 Creating a thread function

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

Parameter meaning:

typedef unsigned LONG int pthread_t

(1) pthread_t *thread: storage thread ID Unique

(2) const pthread_attr_t *ATTR: Thread property setting is typically null for stack-related properties

(3) void * (*start_routine) (void *): function pointer specifies which function code to run

(4) void *arg: The parameter address of the running function is usually passed to the parameter of the custom function

Example: Creating a new thread and passing multiple parameters using a struct note that when compiling, add-lpthread

1#include <pthread.h>2#include <stdio.h>3#include <stdlib.h>4#include <string.h>5 6 structStudent7 {8     intAge ;9     Char*name;Ten }; One  A void*print (structStudent *str) - { -printf"The age is %d\n",str->Age ); theprintf"The name is%s\n",str->name); - } -  - intMain () + { -     structstudent Stu; + pthread_t thread_id; AStu.age = -; atStu.name ="GoGoGo"; -Pthread_create (&thread_id,null, (void*) *print,&stu);//Creating Threads -printf"The new thread ID is%u\n", thread_id); -Pthread_join (Thread_id,null);//wait for the child thread to end in the main thread -     return 1; -}

Added: int pthread_join (pthread_t thread, void **retval), thread wait function

    

Linux thread note 1 creation thread

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.