{"action":"created","issue":{"url":"https://api.github.com/repos/milk-org/ImageStreamIO/issues/3","repository_url":"https://api.github.com/repos/milk-org/ImageStreamIO","labels_url":"https://api.github.com/repos/milk-org/ImageStreamIO/issues/3/labels{/name}","comments_url":"https://api.github.com/repos/milk-org/ImageStreamIO/issues/3/comments","events_url":"https://api.github.com/repos/milk-org/ImageStreamIO/issues/3/events","html_url":"https://github.com/milk-org/ImageStreamIO/issues/3","id":308335712,"number":3,"title":"Semophore usage","user":{"login":"a-sevin","id":32364245,"avatar_url":"https://avatars1.githubusercontent.com/u/32364245?v=4","gravatar_id":"","url":"https://api.github.com/users/a-sevin","html_url":"https://github.com/a-sevin","followers_url":"https://api.github.com/users/a-sevin/followers","following_url":"https://api.github.com/users/a-sevin/following{/other_user}","gists_url":"https://api.github.com/users/a-sevin/gists{/gist_id}","starred_url":"https://api.github.com/users/a-sevin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/a-sevin/subscriptions","organizations_url":"https://api.github.com/users/a-sevin/orgs","repos_url":"https://api.github.com/users/a-sevin/repos","events_url":"https://api.github.com/users/a-sevin/events{/privacy}","received_events_url":"https://api.github.com/users/a-sevin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-03-25T09:39:03Z","updated_at":"2018-03-25T18:26:14Z","closed_at":null,"author_association":"OWNER","body":"Hi,\r\n\r\nI want to introduce a discussion on the semaphore usage.\r\n\r\nIn the ImageStreamIO functions, semaphores are used to block consumers but if the producer runs faster than the consumer, the number of tokens will increase and it will not block the consumer anymore.\r\n\r\nThe consequence is you need to implement 2 thinks:\r\n\r\n- the producer; check if the value is < to SEMAPHORE_MAXVAL before post a new token\r\n- the consumer: check if the flag md->write is 0\r\n\r\nPossible solutions are: \r\n\r\n- using modern C++ implementations with mutex and condition variables in SHM (complex solution but more portable on various architectures)\r\n- using functions to handle the lock mechanism:\r\n  - lock: reset all semaphores to 0, but the flag md->write is 1\r\n  - unlock post all semaphores to 1, but the flag md->write is 0\r\n\r\nWhat do you think?\r\n\r\nArnaud"},"comment":{"url":"https://api.github.com/repos/milk-org/ImageStreamIO/issues/comments/375992036","html_url":"https://github.com/milk-org/ImageStreamIO/issues/3#issuecomment-375992036","issue_url":"https://api.github.com/repos/milk-org/ImageStreamIO/issues/3","id":375992036,"user":{"login":"a-sevin","id":32364245,"avatar_url":"https://avatars1.githubusercontent.com/u/32364245?v=4","gravatar_id":"","url":"https://api.github.com/users/a-sevin","html_url":"https://github.com/a-sevin","followers_url":"https://api.github.com/users/a-sevin/followers","following_url":"https://api.github.com/users/a-sevin/following{/other_user}","gists_url":"https://api.github.com/users/a-sevin/gists{/gist_id}","starred_url":"https://api.github.com/users/a-sevin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/a-sevin/subscriptions","organizations_url":"https://api.github.com/users/a-sevin/orgs","repos_url":"https://api.github.com/users/a-sevin/repos","events_url":"https://api.github.com/users/a-sevin/events{/privacy}","received_events_url":"https://api.github.com/users/a-sevin/received_events","type":"User","site_admin":false},"created_at":"2018-03-25T18:26:14Z","updated_at":"2018-03-25T18:26:14Z","author_association":"OWNER","body":"Hi,\r\n\r\nAt this time, for the solution 2, there is no emergency, so the first one who find a free slot, code it.\r\n\r\nYou are right on the solution 1, you need to rewrite a part of the data structure but this part is only used in ImageStreamIO (I think). You can easily recode ImageSteamIO as a C++ library with the benefits of template and the rest of MILK can stay in C.\r\n\r\nFor mutex, you are right, the are designed for exclusive access to data, it's why you add condition variable (CV) to assure the synchronization. For the case of multiple consumers, CV can notify only one consumer (the first who takes the notification) with notify_one or notify_all which unlocks all the consumers. http://thispointer.com/c11-multithreading-part-7-condition-variables-explained/\r\n\r\nFor your last idea, in pure-C, it's quasi-impossible, I think. A way to reserve a sem slot for a consumer can be a vector of char*. When a consumer reserves a sem, it puts its name at the same index (clear enought?). In C++, there are more powerful tools, you can use a map to index a vector of semaphore with a string. You can also put guards on objects/pointer to handle ungraceful exit for example and assure that the consumer unregister itself.\r\n\r\nArnaud\r\n\r\n"}}
{"actor":{"display_login":"a-sevin"}}