| 发表于:2007-05-28 09:28:54 楼主 |
//vc7, 为什么我用了criticalsection还会说发生访问冲突??? 我是想在主线程向 //buffer 里面放字符串, 在子线程里面不断查看buffer, 如果有字符串, 就拿出来 //有其它方法也可以, 急啊. #include <windows.h> #include <iostream> #include <vector> #include <stack> #include <queue> using namespace std; char *t; queue <char *> tt; int threadcount = 0; critical_section cri; dword winapi threadproc(lpvoid lpparam) { char *mt; char array[1000]; memset(array, '\0 ',1000); while(1) { entercriticalsection(&cri); mt=tt.front(); tt.pop(); strcpy(array,mt); array[strlen(mt)] = '\0 '; delete[] mt; leavecriticalsection(&cri); cout < < array < < endl; } return 0; } int main(int argc, char* argv[]) { initializecriticalsection(&cri); for (int i=0;i <1000;i++) { t = new char[20]; memset(t, '\0 ',20); strcpy(t,itoa(i,t,10)); entercriticalsection(&cri); tt.push(t); leavecriticalsection(&cri); if (threadcount == 0) { threadcount++; createthread(null,0,threadproc,null,0,null); } } system( "pause "); return 0; } |
|
|
|
|