Thread Basics

Thread concept:

A typical process can be thought of as having only one control sequence : a process can only do one thing at a time. With multiple threads of control, the process can be designed so that it can do more than one thing at a time, and each thread handles its own independent task. And this control sequence we call it "thread". In other words, a line of execution in a program is called a thread. A process has at least one execution route.

Speaking of threads, the first thing that comes to mind is processes. So, what is the relationship between threads and processes?

(1) Process is the basic event that undertakes system allocation of resources.

(2) A thread is the smallest unit of program execution.

(3) Threads share process data, but also have some of their own data.

The difference between them is:

(1) The process is the unit of resource allocation, and the thread is the unit of CPU scheduling.

(2) Multiple threads can be included in the same thread, and the threads share the resources of the process.

(3) The creation of the process calls fork or vfork, and the creation of the thread calls pthread_creat. After the process ends, all threads it owns will be destroyed, and the end of the thread will not affect other threads in the same process.

(4) Threads are lightweight processes. It takes much less time to create and destroy than a process. All execution functions in the operating system are performed by creating threads.

(5) Synchronization and mutual exclusion are generally required when executing in a thread. Because all the resources of the process are shared between threads, it is easy to cause problems with resources.

(6) Although threads share process resources, each thread has its own private stack structure.

Advantages of using threads:

(1) Creating a new thread is much less expensive than creating a new process.

(2) Compared to processes, switching between threads requires little work from the operating system

(3) Threads occupy less resources than processes

(4) It can make full use of the parallel efficiency of multiprocessors.

Disadvantages of threads:

(1) Performance loss

(2) Reduced robustness

(3) Lack of access control

(4) Programming difficulty increases

A simple code of thread is attached below, mainly to be familiar with the use of pthread_create and pthread_join

#include<stdio.h>
  2 #include<pthread.h>
  3 #include<string.h>
  4 #include<string.h>
  5 #include<unistd.h>
  6
  7 void* thread1(void* arg)
  8 {
  9   printf("thread1 returning...\n");
 10   int *p = (int*)malloc(sizeof(int));
 11   *p = 1;
 12   return (void*)p;
 13 }
 14
 15 void* thread2(void* arg)
 16 {
 17   printf("thread2 exiting...\n");
 18   int *p = (int*)malloc(sizeof(int));
 19   *p = 2;
 20   pthread_exit((void*)p);
 21 }
 22
 23
 24 void * thread3(void* arg)
 25 {
 26    while(1){
 27     printf("thread3 is running...\n");
 28     sleep(1);
 29   }
 30  return NULL;
 31 }
 32
 33
 34 int main()
 35 {
 36   pthread_t tid;
 37 void* ret;
 38
 39   pthread_create(&tid,NULL,thread1,NULL);
 40   pthread_join(tid,&ret);
 41   printf("thread return,thread id:%x,return code:%d\n",tid,*(int*)ret);
 42 free (ret);
 43
 44   pthread_create(&tid,NULL,thread2,NULL);
 45   pthread_join(tid,&ret);
 46   printf("thread return,thread id:%x,return code:%d\n",tid,*(int*)ret);
47 free (ret);
 48
 49   pthread_create(&tid,NULL,thread3,NULL);
 50   sleep(3);
 51   pthread_cancel(tid);
 52   pthread_join(tid,&ret);
 53   if(ret == PTHREAD_CANCELED)
 54     printf("thread return,thread id:%x,return code:PTHREAD_CANCELED\n",tid);    else
 55    printf("thread return,thread id:%x,return code:NULL\n",tid);
 56
 57 }

                                            

Guess you like

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