Good Lambda Functor and Thread -- C++11

Aug 21, 2012 at 5:21pm
To implement this you should know how to use c++11 lambda closures.
There are a few ways to do this:
The first way uses c++11 auto type detection:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

auto LambdaObject = [](const char* txt)->void // explicit return type "->void"
                      {
                          std::cout << txt << std::endl;
                      };
                      
void Function(void (*fn)(const char*),const char* txt) // 1st arg: type of lambda
{                                                      // 2nd arg: printed text
	fn(txt);
}

int main()
{
	Function(LambdaObject,"This is text.");
    while(1);
}

|Output|

This is text.


The second way is usefull for defining callback types:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

#define CALLBACK_T(fn)     void* (*fn)(void*)      // Declaring a callback
#define CALLBACKARG_T(num) void* (*fn##num)(void*) // Declaring a function arg to accept a callback
#define CALLBACKARG(num)   fn##num                 // Used to access a callback function arg

CALLBACK_T(FnObj) = [](void* data)->void*
		     {
		         std::cout << (char*)data << std::endl;
		     };
						
void Function(CALLBACKARG_T(1),char* txt)
{
	CALLBACKARG(1)(txt);
}

int main()
{
	Function(FnObj,"This is text.");
    while(1);
}

|Output|

This is text.


This is a way to turn a lambda into a pthread:
NOTE! link using -lpthread or equivalent for your compiler.
||
Compile this and run to see the output
the thread prints "check" every half second
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <chrono>
#include <ratio>
#include <pthread.h>

#define CALLBACK_T(fn)     void* (*fn)(void*)      // Declaring a callback
#define CALLBACKARG_T(num) void* (*fn##num)(void*) // Declaring a function arg to accept a callback
#define CALLBACKARG(num)   fn##num                 // Used to access a callback function arg

CALLBACK_T(FnObj) = [](void* data)->void*
		     {
		         std::chrono::steady_clock clock;
		         std::chrono::time_point<std::chrono::steady_clock> tref1,tref2;
		         std::chrono::duration<int,std::milli> ms(500);
		         tref1 = clock.now();
				 while(1)
		         {
					if(tref2 - tref1 >= ms)
					{
						tref1 = clock.now();
						std::cout << "check\n" << std::endl;
					}
					tref2 = clock.now();
				 }
		     };
		     
int main()
{
	pthread_t mythread;
	pthread_create(&mythread,nullptr,FnObj,nullptr);
	pthread_join(mythread,nullptr);
	while(1);
}
Last edited on Aug 21, 2012 at 5:45pm
Aug 21, 2012 at 5:45pm
What is the question? And why is the following lambda defined with trailing return type

1
2
3
4
auto LambdaObject = [](const char* txt)->void // explicit return type "->void"
                      {
                          std::cout << txt << std::endl;
                      };


instead of simple

1
2
3
4
auto LambdaObject = [](const char* txt)
                      {
                          std::cout << txt << std::endl;
                      };



And why is the following lambda

1
2
3
4
CALLBACK_T(FnObj) = [](void* data)->void*
		     {
		         std::cout << (char*)data << std::endl;
		     };

defined without a return statement and with the trailing type void *?

What value does this lambda return?
Last edited on Aug 21, 2012 at 5:47pm
Aug 21, 2012 at 5:55pm

And why is the following lambda

CALLBACK_T(FnObj) = [](void* data)->void*
{
std::cout << (char*)data << std::endl;
};


pthread_create() takes a function ruturning void*, and one argument of type void*
Aug 21, 2012 at 6:10pm
But this code has undefined behavior because the function defined with return type other than void returns nothing.
Aug 21, 2012 at 7:27pm
You should make it an article, not in the forum.
Topic archived. No new replies allowed.