Linux multi-threaded process ed (a) - create a thread

Linux multi-threaded encoding process has matured pthread library support, first of all common API for multi-threaded encoding process to sort out.

Thread

Concurrency and parallelism

In a multithreaded process on a single processor, the processor can switch execution resources between threads to perform concurrent
shared memory multiprocessor multithreaded process, the process of each thread may be in a separate cpu the concurrent execution.

User-level threads

Thread is visible only in the internal process, the internal threads of the process, such as shared address space, open files, and other resources to process all
of the following states are unique to a thread for each

  • Thread ID
  • Register status
  • Stack
  • Signal mask
  • priority
  • Thread-specific storage

    Common API

    The main point of concern has three threads, thread creation, thread and thread waits exit, you need to reference the header files in linux#include <pthread.h>

    Create a thread
    int pthread_create(pthread_t *tidp,const pthread_attr_t *attr,
    (void*)(*start_rtn)(void*),void *arg);

    Compile and link options
    -lpthread, due to the default library pthread library is not a Linux system, but POSIX thread library, so the need to add at compile time or -lpthread -pthread to display a link to the library function is not wrong in the implementation of We will modify the system error errno but returns an error value.

Analytical into ginseng
1.pthread_t pointer tidp thread ID, one thread for each unique to
2.pthread_attr_t
attr this parameter is Posix thread attribute defaults to NULL
3. (void ) ( start_rtn) (void *) running the function starting address of the thread
4. the operating parameters of the function, when a plurality of parameters define the structure

The return value resolved
if a thread is created success, returns 0; failed to create thread return the wrong reasons;
if the thread is successfully created, tidp memory unit is set to point to the newly created thread thread ID

The sample code

  1 #include<stdio.h>
  2 #include<pthread.h>
  3 #include<string.h>
  4 #include<sys/types.h>
  5 #include<unistd.h>
  6 void* printP(void* arg)
  7 {
  8     int i=0;
  9 
 10     while (i != 100)
 11     {
 12         i++;
 13         printf("This is the %d  times print\n", i);
 14         sleep(1);
 15     
 16     }
 17     
 18 }
 19 
 20 bool CreateThread()
 21 {
 22     pthread_t m_pthread;
 23     printf("This is new  pthread\n");
 24 
 25     //创建线程
 26     if (pthread_create(&m_pthread, NULL, printP, NULL) != 0)
 27     {
 28         return false;
 29     }
 30     return true;
 31 
 32 }
 33 
 34 int  main()
 35 {
 36     if(CreateThread() == false)
 37     {
 38          return -1;
 39     }
 40 
 41     int i = 0;
 42     while(i < 100)
 43     {
 44         i++;
 45         printf("This is the %d times say Hello!\n",i);
 46         sleep(1);
 47     }
 48     return 1;
 49 }           

Code running results

[jager@VM_0_10_centos pthread]$ ./test                                
This is new  pthread
This is the 1 times say Hello!
This is the 1  times print
This is the 2 times say Hello!
This is the 2  times print
This is the 3 times say Hello!
This is the 3  times print
This is the 4 times say Hello!
This is the 4  times print
This is the 5 times say Hello!
This is the 5  times print
This is the 6 times say Hello!

Note
1. After the thread is created, the main thread main thread with pthread alternately executed.

Guess you like

Origin www.cnblogs.com/limushu/p/11447164.html