Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
1.09/5 (4 votes)
See more:
C++
#include <windows.h>
#include <cstdio>
#include <process.h>

#define MY_MSG WM_USER+100
const int MAX_INFO_SIZE = 20;

HANDLE hStartEvent; // thread start event
                    /*
                    要把SETTING 改为多线程的
                    Project->Settings->C/C++,
                    在Category 中选Code Generation, 然后在Use run-time libray 中选一个
                    Multithread 配置

*/
// thread function

#include <queue>
using namespace std;

queue<int> g_test;

unsigned __stdcall fun(void *param)
{
    printf("thread fun start\n");

    hStartEvent = ::CreateEvent(0,FALSE,FALSE,0); //create thread start event
    if(hStartEvent == 0)
    {
        printf("create start event failed,errno:%d\n",::GetLastError());
        return 1;
    }
    while(true)
    {
        //wait thread start event to avoid PostThreadMessage return errno:1444
        ::WaitForSingleObject(hStartEvent,INFINITE);
        printf("收到信号...,%d\n",g_test.front());
        g_test.pop();

    }
    return 0;
}

int main()
{
    HANDLE hThread;
    unsigned nThreadID;
    //start thread
    hThread = (HANDLE)_beginthreadex( NULL, 0, &fun, NULL, 0, &nThreadID );
    if(hThread == 0)
    {
        printf("start thread failed,errno:%d\n",::GetLastError());
        CloseHandle(hStartEvent);
        return 1;
    }

    int count = 0;
    while(true)
    {
        g_test.push(++count);
        if(!SetEvent(hStartEvent)) //set thread start event
        {
            printf("set start event failed,errno:%d\n",::GetLastError());
            //return 1;
        }
        if (count>10)
        {
            //::Sleep(5000);
        }
        ::Sleep(50);
    }

    CloseHandle(hStartEvent);
    CloseHandle(hThread);
    return 0;
}
Posted
Updated 1-Jul-10 0:37am
v2
Comments
Smithers-Jones 1-Jul-10 6:09am    
Reason for my vote of 1
Then describe your problem, and don't just throw bits of code and expect people to do everything for your.
R. Giskard Reventlov 1-Jul-10 6:11am    
Reason for my vote of 1
What is the actual problem? No one, as has been said, is going to wade through your code to do your job for you.
Aescleal 1-Jul-10 6:42am    
Sir's first problem might be having an exceptionally skinny monitor...
Smithers-Jones 1-Jul-10 7:12am    
Hi Aescleal, that's a formatting problem here on Codeproject, which is hopefully fixed soon, and not the OP's fault.
Donsw 1-Jul-10 7:52am    
Reason for my vote of 2
explain issue

We can't help you if you don't tell us WHERE the problem is happening along with WHAT the problem is. You've simply given us a block of code and expect us to be able/willing to work at trying to get it to run on our own machines.
 
Share this answer
 
Comments
Smithers-Jones 1-Jul-10 8:01am    
Isn't that, how Codeproject Q&A works? :-) At least you could get that impression, when having a look at many of the "questions" here.
Johnny J. 1-Jul-10 9:17am    
Come now both - people also complain when the OP DOESN'T submit any code...
Quote:
Is this programe have some problem
There is no problem here. Your program is doing exactly what the code is supposed to do.

If you want to know if it match your expectations, it is another problem, because you didn't told us what are your expectations.

If you know that the program is doing wrong, explain what is wrong and we may be able to help fixing that program.
 
Share this answer
 
Comments
CHill60 17-Dec-15 20:37pm    
Er... the post is over 5 years old. I doubt the OP is still wondering what the problem was
Patrice T 17-Dec-15 23:22pm    
I always forget to check the date.
CP should put something to prevent such old messages to come back on top of list.
Jochen Arndt 18-Dec-15 3:00am    
This can't be prevented. It happens when someone replies to such old threads. Because these replies are mainly spam or site driving, they are removed and the removal itself is another changing pushing the question again on top.

Just accustom to read also the posting date before answering questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900