C language programming in Linux: creation and use of threads

Source: Internet
Author: User
The following functions are used to create a thread: includeintpthread_create (pthread_t * thread, pthread_attr_t * attr, void * (* start_routine) (void *), v

The following functions are used to create threads.

  1. # Include int pthread_create (pthread_t * thread, pthread_attr_t * attr, void * (* start_routine) (void *), void * arg); void pthread_exit (void * retval ); int pthread_join (pthread * thread, void ** thread_return );

Pthread_create creates a thread. thread is used to indicate the ID of the thread to be created. attr indicates the attributes of the thread during creation. We use NULL to indicate that the default attribute is used. Start_routine function pointer is the function that starts execution after the thread is created successfully. arg is the only parameter of this function. Indicates the parameter passed to start_routine.

The pthread_exit function is similar to the exit function used to exit the thread. this function ends the thread, releases the function resources, and blocks the thread until other threads use the pthread_join function to wait for it. Then pass the * retval value to ** thread_return. because this function releases function resources, retval cannot point to the local variable of the function.

Pthread_join is the same as wait call to wait for the specified thread. Next we will use an instance to explain how to use it. In practice, we often need to back up some files. The following program backs up all files in the current directory. the suffix after backup is bak.

  1. # Include # define BUFFER 512 struct copy_file {int infile; int outfile ;}; void * copy (void * arg) {int infile, outfile; int bytes_read, bytes_write, * bytes_copy_p; char buffer [BUFFER], * buffer_p; struct copy_file * file = (struct copy_file *) arg; infile = file-> infile; outfile = file-> outfile;/* when the thread exits, all variable spaces are released, so I We had to allocate our own memory */if (bytes_copy_p = (int *) malloc (sizeof (int) = NULL) pthread_exit (NULL); bytes_read = bytes_write = 0; * bytes_copy_p = 0; while (bytes_read = read (infile, buffer, BUFFER ))! = 0) {if (bytes_read =-1) & (errno! = EINTR) break; else if (bytes_read> 0) {buffer_p = buffer; while (bytes_write = write (outfile, buffer_p, bytes_read ))! = 0) {if (bytes_write =-1) & (errno! = EINTR) break; else if (bytes_write = bytes_read) break; else if (bytes_write> 0) {buffer_p + = bytes_write; bytes_read-= bytes_write ;}} if (bytes_write =-1) break; * bytes_copy_p + = bytes_read;} close (infile); close (outfile); pthread_exit (bytes_copy_p);} int main (int argc, char ** argv) {pthread_t * thread; struct copy_file * file; int byte_copy, * byte_copy_p, num, I, j; char filename [BUFFER]; struct dirent ** n Amelist; struct stat filestat;/* obtain the number of all files (including directories) in the current path */if (num = scandir (". ", & namelist, 0, alphasort) <0) {fprintf (stderr," Get File Num Error: % sna ", strerror (errno); exit (1 );} /* allocate space to the thread. In fact, there is no need for so many */if (thread = (pthread_t *) malloc (sizeof (pthread_t) * num) = NULL) | (file = (struct copy_file *) malloc (sizeof (struct copy_file) * num) = NULL) {fprintf (stderr, "Out Of Memory! Na "); exit (1) ;}for (I = 0, j = 0; id_name); if (stat (filename, & filestat) =-1) {fprintf (stderr, "Get File Information: % sna", strerror (errno); exit (1) ;}/ * we ignore the directory */if (! S_ISREG (filestat. st_mode) continue; if (file [j]. infile = open (filename, O_RDONLY) <0) {fprintf (stderr, "Open % s Error: % sna", filename, strerror (errno); continue ;} strcat (filename ,". bak "); if (file [j]. outfile = open (filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR) <0) {fprintf (stderr, "Creat % s Error: % sna", filename, strerror (errno); continue;}/* create thread to copy files */if (pthread_create (& thread [j], NULL, copy, (void *) & File [j])! = 0) fprintf (stderr, "Create Thread [% d] Error: % sna", I, strerror (errno); j ++;} byte_copy = 0; for (I = 0; I
Related Article

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.