class member function threading
1. No argument
< code start >
< code end >
2. additional arguments
< code start >
< code end >
3. Lambda Express
std::thread t( [&] { x.greeting(); } ); //no argument
std::thread t( [&] { x.greeting( "goodbye" ); } ); //with argument
Static or global function threading
< code start >
< code end >
#thread, #std, #lambda
Showing posts with label Thread. Show all posts
Showing posts with label Thread. Show all posts
2/23/2017
11/16/2011
Thread Test in the C++ console mode.
The goal of this source code is how to send large data to the thread and how to wait until all thread finish their job.
I made 3 threads for testing.
I use struct to send the large data.
and I use the flags for waiting the time of the all thread finish.
<source code>
Please leave comment if the source code has problem.
Thank you~
I made 3 threads for testing.
I use struct to send the large data.
and I use the flags for waiting the time of the all thread finish.
<source code>
//////////////////////////////////////////////////////////////////////////
// Made by J.H.KIM, 2011 / feelmare@daum.net, feelmare@gmail.com //
// blog : http://feelmare.blogspot.com //
// My Lab : VISLAB(http://me.pusan.ac.kr) //
//////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <process.h>
#include <windows.h>
struct InputData
{
int * data;
int width;
int height;
int OriginW;
int reserved;
int flag;
};
void ThreadProcessing(void* pParam);
void main()
{
//Data Allocation and Setting
int W = 100;
int H = 100;
int * buffer;
buffer =new int[W*H];
for(int i=0; i<W*H; ++i)
{
buffer[i] = i;
}
int sx,sy,w,h;
//thread1, preparing Transite values
InputData th1_input;
sx = 0; sy = 0; w = 80; h = 90; //(80x90) image processing
th1_input.width = w;
th1_input.height = h;
th1_input.OriginW = W;
th1_input.data = &buffer[sy*W+sx];
th1_input.reserved = 1;
th1_input.flag = TRUE;
_beginthread(ThreadProcessing,0,&th1_input);
//thread2, preparing Transite values
InputData th2_input;
sx = 10; sy = 10; w = 50; h = 50; //(50x50) image processing
th2_input.width = w;
th2_input.height = h;
th2_input.OriginW = W;
th2_input.data = &buffer[sy*W+sx];
th2_input.reserved = 2;
th2_input.flag = TRUE;
_beginthread(ThreadProcessing,0,&th2_input);
//thread3, preparing Transite values
InputData th3_input;
sx = 10; sy = 10; w = 20; h = 20; //(10x10) image processing
th3_input.width = w;
th3_input.height = h;
th3_input.OriginW = W;
th3_input.data = &buffer[sy*W+sx];
th3_input.reserved = 3;
th3_input.flag = TRUE;
_beginthread(ThreadProcessing,0,&th3_input);
//wait until all thread is finished.
while( th1_input.flag || th2_input.flag || th3_input.flag)
{
Sleep(100);
}
//Data buffer release
delete[] buffer;
}
void ThreadProcessing(void* pParam){
//copy struct data to variable
int w = ((InputData*)pParam)->width;
int h = ((InputData*)pParam)->height;
int ow = ((InputData*)pParam)->OriginW;
//Processing Sum(v^2.)
int v;
double sum=0;
for(int j=0; j<h; ++j)
{
for(int i=0; i<w; ++i)
{
v = ((InputData*)pParam)->data[j*ow+i];
sum += (v*v);
}
}
printf("%d thread value = %lf\n", ((InputData*)pParam)->reserved , sum);
((InputData*)pParam)->flag = FALSE;
}
Please leave comment if the source code has problem.
Thank you~
Subscribe to:
Posts (Atom)
-
This is data acquisition source code of LMS511(SICK co.) Source code is made by MFC(vs 2008). The sensor is communicated by TCP/IP. ...
-
The MNIST dataset is a dataset of handwritten digits, comprising 60 000 training examples and 10 000 test examples. The dataset can be downl...
-
Logistic Classifier The logistic classifier is similar to equation of the plane. W is weight vector, X is input vector and y is output...
-
Image size of origin is 320*240. Processing time is 30.96 second took. The result of stitching The resul...
-
Background subtractor example souce code. OpenCV support about 3 types subtraction algorithm. Those are MOG, MOG2, GMG algorithms. Det...
-
I use MOG2 algorithm to background subtraction. The process is resize to small for more fast processing to blur for avoid noise affectio...
-
RTSP(Real Time Streaming Protocol) is video streaming, it usually sent from network camera. VideoCapture function in opencv also can get r...
-
Open Source License Commercial Use Guide Understanding Commercial Rights in Open Source Licenses π Legend ✅ Free : Comm...
-
This example source code is to extract HOG feature from images. And save descriptors to XML file. The source code explain how to use HOGD...
-
Created Date : 2009.10. Language : C++ Tool : Visual Studio C++ 2008 Library & Utilized : Point Grey-FlyCapture, Triclops, OpenCV...

