473,804 Members | 3,123 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CreateThread in Windows

73 New Member
This must be an easy question.

I am trying to create a thread in windows. I am using C as my language.

I am trying to pass a function to the CreateThread function to create a thread. My function has a return type void and paratmeter is void as well. How do I approach this?

Is it something like this?

static void run() {
myThread = CreateThread(
NULL,
0,
MyFunction,
NULL,
0,
NULL);
}

How do I pass my function to CreateThread?

What is this DWORD WINAPI functionName () that I am reading about? Do I need this function?

Thanks in advance,
Sevak.
Oct 1 '08 #1
3 7198
Laharl
849 Recognized Expert Contributor
The function you pass to CreateThread must return a DWORD and be defined with the WINAPI keyword. For example,
Expand|Select|Wrap|Line Numbers
  1. DWORD WINAPI run(void* arg); //LPVOID would be more "Windows", not that I really care
Oct 1 '08 #2
sevak316
73 New Member
sorry, but im confused. I have been lookin at code ALL day!

who ends up calling my funtion and how??
lets say my function is "void xTaskFast(void) "
Oct 1 '08 #3
Laharl
849 Recognized Expert Contributor
Then you call CreateThread as follows:

Expand|Select|Wrap|Line Numbers
  1. DWORD tid;
  2. HANDLE h = CreateThread(NULL, 0, xFastTask, 0, &tid); //check h to see if it succeeded or not
  3.  
Source: MSDN

The spawned thread, if the call is successful, will be created and will start execution at the beginning of the function passed in the third parameter. The thread that called CreateThread will continue from the next line (this should be error checking).
Oct 1 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2627
by: lamilla | last post by:
Salve a tutti, sto creando una applicazione che istanzia un thread al quale viene passato come argomento il puntatore ad una classe che contiene molte funzioni alcune delle quali devono essere necessariamente static. Non trovando nessun esempio in giro e non sapendo come referenziare le variabili globali di classe CIndiceSP nelle funzioni static della classe stessa ho utilizzato questo metodo che non mi crea nessun errore di compilazione...
1
6548
by: phark52 | last post by:
My main app calls LoadLibrary() to load a DLL, which calls CreateThread(). This does NOT return NULL and I get a thread ID. However, ThreadProc never gets executed when this code is in the DLL. It works fine in the standalone EXE source. I put example code below. #define MB(msg) MessageBox(0, msg, "", MB_OK|MB_ICONINFORMATION); DWORD APIENTRY tproc() {
5
4193
by: Vinayak Raghuvamshi | last post by:
Threads created using the CreateThread API used to leak memory if you accesed any crt methods within the thread proc. we used to use _beginthread to work around this issue. Does anyone know if this has been fixed ? -Vinayak
3
15793
by: raj | last post by:
Hi, I just want to know the difference between thread created by CreateThread and AfxBeginThread function. I got one problem when using the CreateThread. The appliction malfunction while using the CreateThread. So is that a problem with this function? Thanks in advance Rajat
3
8514
by: mccanaveras | last post by:
Hello, I need to implement a Thread in a Visual C++ 6.0 application. I've never done this, then I've been investigating, but I don't have any properly solution. One of them is use CWinThread to comunicate to a Dialog but it has been already created. And the other possibility is create a thread with the function CreateThread(), but it gives me an error in the third parameter: error C2664: 'CreateThread' : cannot convert parameter 3...
0
1311
by: liamacheung | last post by:
Hi, I am designing a GUI application using a VC++ 2005 Form template. I have created a thread function external to the class containing the form. I can successfully create the thread and run operations in the thread function, but I want to be able to call member functions in the Form from the thread process. In order to do this, I plan on passing a pointer to the object (i.e. the form) as one of the CreateThread parameters. My...
5
8081
by: eagerlearner | last post by:
Hallo, when I use class and I put one of the member function as the CreateThread argument, I keep getting the compile error in Visual Studio 2005. #include <windows.h> class X { public: void f(); void s(){}; };
1
2287
by: tobycraftse | last post by:
I heard I should use the C function - CreateThread() to create thread. I have a graphics program that draws some animations. How can I add another thread to do other stuff like file processing -OR- internet query? Then where can I find tutorial on writing win32 threading code? any books suggestion? any sample code?
17
6877
by: Daniel | last post by:
When I use the CreateThread API method, what do I need to do when I want to pass more than one parameter where LPVOID lpParameter is passed? Daniel
0
9587
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.