📄 consumer.c
字号:
#define __THREADS_MAIN#include <thread.h>#include <string>extern "C" {# include <unistd.h>};cond t_control;mutex t_link;string s;class producer : public pthread {public: producer() { }; ~producer() { }; int thread(void *) { char *buf = new char[80]; cout << "Enter text for consumer: "; cout.flush(); cin.getline(buf,80); t_link.lock(); s = buf; t_control.signal(); t_link.unlock(); delete buf; return 0; }};class consumer : public pthread {public: consumer() { }; ~consumer() { }; int thread(void *) { int r; t_link.lock(); t_control.wait(t_link); cout << "Consumed: '" << s << "'\n"; r = s.length(); t_link.unlock(); return r; }};voidhexdump(const unsigned char *ptr, int len){ string s; int i = 0; while(i < len) { if( (i%16) == 0 ) { if ( i > 0 ) cout << " ;" << s.c_str() << endl; cout.form("%08X ",(int)ptr); s = ""; } cout.form(" %02X",(unsigned int)ptr[i]); if ( isprint(ptr[i]) ) s += ptr[i]; else s += '.'; i += 1; } while( (i%16) ) cout << " ",i++; cout << " ;" << s.c_str() << endl;}int main(){ pthread *p1, *p2; p1 = new consumer; p2 = new producer; p1->join(); cout << "return value " << p1->retcode() << endl;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -