First, you need to keep some state in each thread. You can do this with the iword function and the pointer you pass to it given by xalloc :
inline int geti() { static int i = ios_base::xalloc(); return i; } ostream& add_one(ostream& os) { os.iword(geti()) = 1; return os; } ostream& add_none(ostream& os) { os.iword(geti()) = 0; return os; }
After that, you can already get some state in all threads. Now you just need to connect to the corresponding output operation. Digital output is performed by the facet, as it potentially depends on the locale. So you can do
struct my_num_put : num_put<char> { iter_type do_put(iter_type s, ios_base& f, char_type fill, long v) const { return num_put<char>::do_put(s, f, fill, v + f.iword(geti())); } iter_type do_put(iter_type s, ios_base& f, char_type fill, unsigned long v) const { return num_put<char>::do_put(s, f, fill, v + f.iword(geti())); } };
Now you can test the material.
int main() {
If you want only the next number to increase, just set the word 0 again after each do_put call.
Johannes Schaub - litb Apr 28 '09 at 9:23 2009-04-28 21:23
source share