Title: Implementing readers - writers problem using Tcl C APi
Tags: c;tcl;reader
Question: I have the following problem (readers writers problem) :
Suppose I created a Tcl_Channel that is controlling the stdout channel , meaning everything that is printed to stdout channel is going through my_output_function that is part of my Tcl_Channel.
Now suppose I'm putting Tcl_Mutex on this part of the code of this function so this code will be a critical code.
In this code I'm copying the buffer that I got on this channel to some buffer queue in my system and then unlocking the Tcl_Mutex.
Now I want to send the queue with the copied buffers to my printers (can be many but for now it is just one) and to print the strings in this queue one by one by those printers.
This problem is similar to readers-writers problem that is very common.
Now , I'm not so expert in Tcl C Api but I saw there are some Tcl C structs that are probably will help me like Tcl_Condition (instead of using cond_t) and Tcl_Notify.
My questions are :
is there some good reference code or website that I can see that implementing something similar to this?
my code is in C and C++ only (I used the Tcl C API only for consistency to the rest of my code and because I redirected the stdout using Tcl_Channel) so is it better to implement it using C/C++ types (using it with mutex_t and cond_t etc.)
(I think the most important) how can I make the writer always be on waiting condition for the next input? If I create two threads one for input and the other for output will it work or maybe it is better for the thread that is receiving the input also call the printer function.
Comment: The types `Tcl_Mutex` and `Tcl_Condition` are basically wrappers round `mutex_t` and `cond_t` to make them cross-platform (i.e., they're implemented differently on different platforms but your code doesn't need to know the details to be portable) and a bit easier to use.

Comment: great!  I also thought that they just wrappers but wasn't quiet sure about this. Thanks!



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:11/28/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c><tcl><reader>', 'Title': 'Implementing readers - writers problem using Tcl C APi', 'ViewCount': 50, 'id': 53527470, 'user_AboutMe': None, 'user_AccountId': 8372918.0, 'user_DisplayName': 'ms_stud', 'user_Reputation': 321.0, 'user_Views': 72.0, 'user_id': 8747958.0}}