consumer.C
上传用户:shtangtang
上传日期:2007-01-04
资源大小:167k
文件大小:1k
- #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;
- }
- };
- void
- hexdump(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;
- }
-