📄 readme.threads
字号:
This is just a brief rundown on how the various threads interactvia mutexes and semaphores.There are three main threads,gtk runs in its own threadthe display function runs its own threadthe grab function (gets the picture from the cam) runs its own threadthere are three semaphoress_draws_grab1s_grab2and three mutexespref_mutexfreeze_mutexiscam_mutexThere are two picture buffers, one (camera->pic) is the one that is displayed.The other (camera->picbuff) is the one that is written to by the grab threadthe display function waits for the s_draw semaphore to be raised.It then swaps the two buffers (pic/picbuff) and raises the s_grab1 sempahoreThen it displays the image using gtkThen it goes back to the top, and waits for the s_draw sempahore to be raisedagain.The grab function waits on alot of stuff, but mainly it waits for the twograb semaphores to be raised. As we have seen, display() raises the s_grab1semaphore whenever it wants a new image in camera->picbuff. The s_grab2sempahore is used if you are doing frames per second limiting. if FPS islocked at 10 fps, 10 times a second the s_grab2 semaphore will be raised.So whenever display() wants another image AND we haven't gotten a new imagein the last 10th of a second, get an image.This is complicated a bit because we don't want to get any new images whenthe 'freeze' button is on, and we don't want to try to get images when wedon't have an open camera. This is where two of our three mutexes come in.open_camera and close_camera control the 'iscam_mutex' When there is NOTa camera, that mutex is locked. The grab function wants to lock that mutexbefore it grabs an image, so if close_camera locked the mutex, grab_image()will sit and wait for open_camera() to release the iscam_mutex.The freeze_mutex works similarly. When you click the 'freeze' button,it locks the freeze_mutex. grab_image() will wait to get another imageuntil it can get the freeze_mutex. When you click on the 'freeze' buttonto turn it off, it releases the mutex and grab_image() will start runningagain.If you were wondering why freeze_mutex and iscam_mutex are in grab_imageinstead of display, don't. It doesn't really matter which they are in,I just felt like putting them in grab_image().The only mutex left is pref_mutex. I don't know how neccessary this onereally is, but it makes me feel much safer knowing it is there. This mutexis used whenever you click a slider or push a radio button. Only one threadcan access the preferences at a time, so they don't get in each other's way.This might be overkill, but it makes me feel safer. grab_image() checksthe camera->update_camera value, and if it is set, it does a set_cameraand clears the update_camera flag.Thats the whole explanation, I hope it was more helpful than confusing.The nice thing about this little setup is that the grab_image() thread startsreading another picture AT THE SAME TIME that the display thread is writingto the screen. This allows me to get 30 fps on my CPiA camera that hasa theoretical limit of, well, 30. With weaker threading, or just a singleprocess for grab/display I could only get a max of 28 fps. Besides, thisis more fun.enjoy,john smallberries ( spred@geocities.com )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -