2015-10-07 91 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变量来保存有关该线程的信息。