Hi all,
I'm a beginner programer in VC++. I have two functions that I'm calling from the main function. I want to control the time that each function run for. I'm illustrarting below with the following simple code:
#include <stdafx.h>
#include <stdlib.h>
#include <windows.h>
#include <set>
#include <conio.h>
#include <iostream>
usingnamespace std;
int a,b,c,d;
int function1(int, int);
int function2(int);
int main(){
cout << "Enter a: ";
cin >> a;
cout << "Enter b: ";
cin >> b;
function1(a,b);
function2(c);
return 0;
} /* End of Main */
int function1(int a, int b){
c = (2*(a+b))-(3*b);
cout << "c= " << c << "\n";
return c;
} /* End of function1 */
int function2(int c){
d = (2*c)+(c-3);
cout << "d= " << d << "\n";
return d;
}/* End of function2 */
I want for instance to run function1 for 10ms and function2 for 50 ms. I know one of the ways to do this is multithreading using Mutex, yet I have no experience in multithreading. I tried to read about it but honestly it was beyond my knowledge of the language to understand the sample code. So I was hoping to find an easy way maybe using timers, waitbar timers or anything else. I don't want to use Sleep(ms) to achieve as I want to have full control of the running time.
Well, let me dig up my code and find an example of threading...
I think this should work. Let me know if it doesn't and I'll give you a more literal copy:
#include <windows.h>
struct parameters{
//whatever parameters thread0 needs
};
DWORD thread0(void *params){
parameters *a=(parameters *)params;
//put parameters into separate variables, if you want
//thread0's body
}
//I like to use this function to start threads more easily
HANDLE newThread(LPTHREAD_START_ROUTINE routine,LPVOID parameters,int priority=0){
HANDLE res=CreateThread(0,0,routine,parameters,0,0);
if (priority)
SetThreadPriority(res,priority);
return res;
}
int main(){
parameters *thread0data=new parameters;
//initialize thread0data
HANDLE *thread0handle=newThread(thread0,thread0data);
//Let's suppose you want to wait 100 ms (10 is below the granularity of Sleep(), if I remember correctly)
Sleep(100);
//http://msdn.microsoft.com/en-us/library/ms684335(VS.85).aspx
thread0handle=OpenThread(THREAD_TERMINATE,0,GetThreadId(thread0handle));
if (!thread0handle){
//OpenThread() has failed. Deal with the error here.
}
//http://msdn.microsoft.com/en-us/library/ms686717(VS.85).aspx
TerminateThread(thread0handle,0);
//do other stuff
return 0;
}
What exactly do you mean by "sampling time (frequency)"? This doesn't take samples of how long it has passed. It's simply one thread executing while another waits a certain amount of time and then immediately kills the other thread. There's no sampling.
Hi, I'm a newbie in C++ programming on Win Operative Systems.
I'm a little confused about Multithreading in Visual C++. I wrote some image processing libraries on Linux using some computer Vision and Image Compressing Libraries those are portable. Now I reduced my portabiliy problems on Threading code... In Linux I've got Posix Thread, but in Visual C++ I've got .NET Threading system, Multithreading C with Win32 (from COM i suppose (?)) and Multithreading with MFC... What most considereable differences are between these systems... What would you suggest me for multithreading image processing?