| | |
Need help with threads in C++
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Try this and see if it works..... But the problem is some libraries does not support
(#include "XYLock.h")
(#include "XYLock.h")
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <iostream> #include "XYLock.h" XYCriticalSection myCriticalSection; void ThreadProc(void *dummy) { while(true) { try { XYLock myLock(&myCriticalSection); printf("Thread '%X' has the lock\n",::GetCurrentThreadId()); throw "a test"; } catch(char* p) { printf("Caught: %s\n",p); } ::Sleep(1000); } } void main() { for(int i=0;i<100;i++) { if(_beginthread(ThreadProc,0,NULL)==-1) { printf("Failed to create a thread\n"); return; } } while(true) { try { XYLock myLock(&myCriticalSection); printf("The main thread '%X' has the lock\n",::GetCurrentThreadId()); throw "a test from the main thread"; } catch(char* p) { printf("Caught: %s\n",p); } ::Sleep(1000); } }
Last edited by Traicey; Mar 26th, 2008 at 11:08 am.
Threads are not natively supported by the c++ language but there are several api functions and libraries that you can use. One of them is win32 api function CreateThread()
Also see this thread
[edit]^^^ Tracey's example is excellent too.[/edit]
Also see this thread
[edit]^^^ Tracey's example is excellent too.[/edit]
Last edited by Ancient Dragon; Mar 26th, 2008 at 11:11 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Thanks tracey, but I need help with a few things. First of all, I don't understand the code (I'm new a C++) so could you explain it to me? Second, what s the code supoosed to do? Thirdly, if I wanted to wait on an input from the user for a limited amount of ime, would I have to use threads and how could this be accomplished? Oh, one more thing my library doesn't have XYLock.h, so if I were to get a copy from you and link it to my library, would it work? Thank a lot! for your time! (^ _ ^)
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
As AncientDragon said, CreateThread() can be used in windows. Check it out on MSDN. Following is some code to show you how to use it. Note that the thread proc should be of the form
DWORD WINAPI myThreadProc(void*someData) . someData is a pointer passed by CreateThread to your thread proc (I've set it to 0 in the following example). C++ Syntax (Toggle Plain Text)
#include <windows.h> #include <iostream> using namespace std; DWORD WINAPI myThreadProc(void*someData){ while(1){ cout << "myThreadProc says hi" << endl; Sleep(1000); } } main(){ CreateThread(0, 0, myThreadProc, 0, 0, 0); for(int i = 0; i < 10; i++){ cout << "main says hi" << endl; Sleep(1500); } }
Hey doug, thanx for your suggestion, it complied and worked perfectly, but I don't really understand how the program works and I was wondering, how an example like this would work if an input from the user was required.
Correct me if I'm wrong, but based on the output that I'm getting, and looking at the code itself, it seems like the computer is switvching between tasks. Am I right?
(^ _ ^) Thanx!
Correct me if I'm wrong, but based on the output that I'm getting, and looking at the code itself, it seems like the computer is switvching between tasks. Am I right?
(^ _ ^) Thanx!
>>it seems like the computer is switvching between tasks. Am I right?
Yes. Unless you have a duel processing computer then the os can only do one thing at the same exact time. It is just an illusion that it looks like two or more things run at the same time. The operating system contains a task switcher that gives only a small amount of CPU time to each program at a time. It has an array of threads that have to be serviced and switches the CPU between them in round-robin fashion. So when you call CreateThread you just add another thread to that list in the task scheduler.
Yes. Unless you have a duel processing computer then the os can only do one thing at the same exact time. It is just an illusion that it looks like two or more things run at the same time. The operating system contains a task switcher that gives only a small amount of CPU time to each program at a time. It has an array of threads that have to be serviced and switches the CPU between them in round-robin fashion. So when you call CreateThread you just add another thread to that list in the task scheduler.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- I want google to index forum threads (Search Engine Optimization)
- new way the threads are timed (DaniWeb Community Feedback)
- IE won't let me access threads. Could someone email me a solution please? (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: rand(); always ='s 41?
- Next Thread: Help with adding 2 arrays of different sizes
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






