473,394 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Solved: Threading: how to invoke it properly?

131 128KB
I'm using MS' optimizing compiler CL 13.10 (one from VCToolkit 2003) along with WinAPI threading functions and is being compiled as a C console program.

I was wondering how to implement threading in a production setting? I've seen and tried various examples, but they all show basically the same thing - startup in main, run their function, clean up, and then the program exits.

I don't know the proper terminology, but I was looking for two maybe three functions to run simultaneously with a loop in main. I tried a small test program and was wondering if it's setup correctly.

A structure is used as the argument for each function.

Expand|Select|Wrap|Line Numbers
  1. int running = 1; // global variable
  2.  
  3. DWORD WINAPI function_1(LPVOID);
  4. DWORD WINAPI function_2(LPVOID); 
  5.  
  6. main() {
  7.  
  8.    HANDLE hndThreads[2];
  9.    DWORD threadIDs[2];
  10.    // other initialization
  11.  
  12.    hndThreads[0] = CreateThread(...);
  13.    hndThreads[1] = CreateThread(...);
  14.  
  15.    if(handles are valid) {
  16.  
  17.       while(running == 1) {
  18.          // user interaction; user
  19.          // input sets running = 0 to quit
  20.       }
  21.  
  22.       WaitForMultipleObjects(2, hndThreads, TRUE, INFINITE);
  23.       // close handles 
  24.    }
  25.  
  26.    // clean up main
  27. }
  28.  
  29. DWORD WINAPI function_1(LPVOID lpParam) {
  30.  
  31.    // sdata is typedef struct
  32.  
  33.    sdata test = *((sdata *)lpParam);
  34.  
  35.    do {
  36.       // manipulate test
  37.    } while(running == 0);
  38.  
  39.    return 0;
  40. }
Right now, function_2 is just a copy of function_1's definition. Everything *appears* to do what I want, but is it setup correctly? Any comments would be greatly appreciated

Thank you
Jun 12 '12 #1

✓ answered by weaknessforcats

You will need to set an event that your threads can look for.

The thread starts running as soon as CreateThread fires. Each thread should have a loop where you can use WaitForSingleObject and test the event handle. This is the only way your thread knows whether to continue or not.

Then in the mainline you can use WaitFoMltileObjects until all your threads have repoted in that they have stopped.

And don't forget to use CRITICAL_SECTION functions for all shared data.

Check the book Windows via C/C++ by Jeremy Richter for goos examples on thread management.

2 1822
weaknessforcats
9,208 Expert Mod 8TB
You will need to set an event that your threads can look for.

The thread starts running as soon as CreateThread fires. Each thread should have a loop where you can use WaitForSingleObject and test the event handle. This is the only way your thread knows whether to continue or not.

Then in the mainline you can use WaitFoMltileObjects until all your threads have repoted in that they have stopped.

And don't forget to use CRITICAL_SECTION functions for all shared data.

Check the book Windows via C/C++ by Jeremy Richter for goos examples on thread management.
Jun 13 '12 #2
divideby0
131 128KB
You've been a great help again. :) Thank you for this information; I sure do appreciate your time.
Jun 14 '12 #3

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

Similar topics

6
by: Stephen Brooker | last post by:
Hi all, I've got a basic TCP app that is giving me trouble. I have a separate class that takes care of the TCP connection, and uses the NetworkStreams BeginRead and EndRead with a callback...
8
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
2
by: Ludovic SOEUR | last post by:
Have everyone tried to create controls in separated threads ? I have a problem that I do not understand. To simplify the explanations, I wrote theses few lines to show an example of the problem....
3
by: Asad | last post by:
Hi, I am trying to write some threading code to my application. The reason I've been tempted to do this is because, I am doing some FTP uploads, and sometimes during the put method, the...
6
by: John Rugo | last post by:
Hi All, I have the most basic understanding of using Threading. I have no problem creating a thread, addressing it to a Sub Procedure, and Starting the Thread. Works Great! My problem is the...
3
by: Jeff Stewart | last post by:
I have a worker thread running a POP3 mail retriever that raises a NewEmail event every time a message is retrieved. The NewMail handler updates a ListView in my form with a line representing the...
0
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode...
7
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it...
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.