buffer.c

来自「多线程库」· C语言 代码 · 共 51 行

C
51
字号
//// 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);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?