buffer.C
上传用户:shtangtang
上传日期:2007-01-04
资源大小:167k
文件大小:1k
- //
- // This example demonstrates semaphores.
- //
- #include <thread.h>
- #include <thread_semaphore.h>
- #include <string>
- class typeit : public pthread {
- protected:
- semaphore t_sem;
- string s;
- int i;
- public:
- typeit() { t_sem.wait(); i=0; t_sem.post(); };
- ~typeit() { };
- char text()
- {
- if ( t_sem.trywait() >= 0 )
- return s[i++];
- return 0;
- }
- int thread(void *)
- {
- int n=0;
- char buf[80];
- cout << "launched" << endl;
- cin.getline(buf,80);
- n = strlen(buf);
- while( n-- > 0 )
- t_sem.post();
- s = buf;
- return 0;
- }
- };
- int main()
- {
- char ch=0;
- typeit data;
- while( (ch=data.text()) != 0 )
- cout << ch;
- cout << "." << endl;
- exit(0);
- }