2015-10-07 89 views
-2

從代碼中提取的「運動」這個線程環境結構是做什麼的?它的目的是什麼?

struct context { 
char conf_filename[PATH_MAX]; 
int threadnr; 
unsigned short int daemon; 
char pid_file[PATH_MAX]; 

struct config conf; 
struct images imgs; 
struct trackoptions track; 
struct netcam_context *netcam; 
struct image_data *current_image;  /* Pointer to a structure where the image, diffs etc is stored */ 
unsigned short int new_img; 

int locate; 
struct rotdata rotate_data;    /* rotation data is thread-specific */ 

int noise; 
int threshold; 
int diffs_last[THRESHOLD_TUNE_LENGTH]; 
int smartmask_speed; 

/* Commands to the motion thread */ 
volatile unsigned short int snapshot; /* Make a snapshot */ 
volatile unsigned short int makemovie; /* End a movie */ 
volatile unsigned short int finish;  /* End the thread */ 
volatile unsigned short int restart;  /* Restart the thread when it ends */ 
/* Is the motion thread running */ 
volatile unsigned short int running; 
volatile int watchdog; 

... 
}; 

我猜測,在Linux中程序顯然不能只是有一個單一的過程,因此需要「線程」,就像Java程序。

線程的一個常見問題是在它們之間切換,如果需要的話,這當然可以主要由Linux操作系統來完成。

因此,我們需要一個上下文結構來保存我們需要執行此類操作的所有數據,因爲運行所謂的線程顯然需要上下文結構來存儲所有重要信息。因此最終你可以像上面顯示的那樣擁有一個「上下文結構」數組,每個線程對應一個結構。

因此,這裏是我的問題:

A.難道我就在做上述所有假設?還是我錯過了100英里?我知道,在某些時候,這些結構線程,或者至少是一個非常重要的部分。

B.除了保持信息對於運行和切換線程至關重要之外,該結構還有什麼用處?我知道代碼是不完整的,所以這是一個相當開放的問題。鼓勵基於以前編碼經驗的答案。

C.這種做法是否普遍?通過「這種做法」,我的意思是將應用程序劃分爲多個線程,並使用上下文結構來跟蹤它們?

回答

0

您在需要以異步方式處理數據的程序中使用線程,但我並不熟悉使用上下文結構,至少在應用程序代碼中不是這樣。

這種信息只有內核需要。在應用程序中,您使用pthread_t變量來保存有關該線程的信息。