Hi Everyone. I am new to c++ programming,I begin to learn Threads and I understood theoretically, why we need thread and so on. But I am confused while begin to programming. Can anyone please give very simple program which makes me to understand well please. :pleased: I have to do with _beginthreadex and _endthreadex, so if you give sample program using these functions means happy And also need some links to learn Threads in windows. So please help me friends. Thanks in advance.
As fate would have it, I too am playing with threading. I'm not quite sure if it's correct, but maybe something like Code: #include <windows.h> #include <process.h> #include <iostream> #include <ctime> unsigned __stdcall myfunction1(void *); // thread prototype unsigned __stdcall myfunction2(void *); // thread prototype int main() { HANDLE hndThread[2], hndEvt; unsigned threadID; hndEvt = CreateEvent(NULL, TRUE, FALSE, NULL); // create an event with handle not inherited by child process, // manual reset, non-signaled, and no name // HANDLE CreateEvent(SECURITY_ATTR, BOOL manual reset, BOOL initial state, LPCSTR name) // ref: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682396(v=vs.85).aspx if(hndEvt) { hndThread[0] = (HANDLE)_beginthreadex(NULL, 0, myfunction1, &hndEvt, 0, &threadID); hndThread[1] = (HANDLE)_beginthreadex(NULL, 0, myfunction2, &hndEvt, 0, &threadID); // kick off the thread functions if(hndThread[0] && hndThread[1]) { int ch; std::cout << "(enter to quit)\n"; for(; (ch = std::cin.get()) != '\n'; ) { } SetEvent(hndEvt); // signal thread it's time to stop WaitForMultipleObjects(2, hndThread, TRUE, WAIT_TIMEOUT); if(!CloseHandle(hndThread[0])) std::cerr << "\n\nerror closing thread handle[0]"; // close handles to threads if(!CloseHandle(hndThread[1])) std::cerr << "\n\nerror closing thread handle[1]"; } if(!CloseHandle(hndEvt)) std::cerr << "\n\nerror closing event handle"; // close handle to event } std::cout << "\n\ndone\n"; return 0; } unsigned __stdcall myfunction1(void *arg) { HANDLE hnd = *((HANDLE *) arg); while(WaitForSingleObject(hnd, 0) != WAIT_OBJECT_0) { // wait until signaled to quit std::time_t t1 = std::time(0), t2; // wait "2" seconds then print a message do { t2 = std::time(0); } while(t2 < t1 + 2); std::cout << "myfunction1 waiting for termination...\n"; } return 0; } unsigned __stdcall myfunction2(void *arg) { HANDLE hnd = *((HANDLE *) arg); while(WaitForSingleObject(hnd, 0) != WAIT_OBJECT_0) { // wait until signaled to quit std::time_t t1 = std::time(0), t2; // wait "6" seconds then print a message do { t2 = std::time(0); } while(t2 < t1 + 6); std::cout << "\tmyfunction2 waiting for termination...\n"; } return 0; } My understanding is that _endthreadex is called implicitly once the function ends. If it's okay, then I hope it helps.