File: thread_example.cpp

package info (click to toggle)
libthreadar 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,072 kB
  • sloc: sh: 4,379; cpp: 2,499; makefile: 78
file content (87 lines) | stat: -rw-r--r-- 2,128 bytes parent folder | download
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

#include <libthreadar/libthreadar.hpp>


class my_thread: public libthreadar::thread
{
public:
	// for that class we will provide
	// all parameters using set_* methods
    my_thread() { synchro.lock(); };

	// an example of thread setup, expected to
	// be called before the thread is run()
    void set_message(const std::string & msg)
    {
	if(is_running())
	    throw libthreadar::exception_bug(__FILE__,__LINE__);
	message = msg;
    };

	// we could also handle here the communication/synchronisation with
	// the thread for example using a mutex or barrier
    void unlock_thread() { synchro.unlock(); };

	// implementing our way to cleanly stop the thread
    void stop()
    {
	kill(); // requesting thread to be killed
	synchro.try_lock(); // they might be locked on synchro
	synchro.unlock();   // so we unlock them for the can end
	join(); // now waiting for their effective terminaison
	    // resetting the mutex in its initial
	    // state ready for a new run()
	synchro.try_lock();
    }

protected:
	// the method that will run as a separated thread
	// but will still have access to private fields of the
	// object. Its up to the class developper to define
	// the way the inherited_run() thread access these
	// in concurrence with the thread caller by mean
	// of provided method like unlock_thread() above
    virtual void inherited_run() override
    {
	while(1)
	{
	    synchro.lock();
	    std::cout << message << std::endl;
	}
    }

private:
    st::string message;
    libthreadar::mutex synchro;
};


int main()
{
	// we will play with two sub-threads
    my_thread t1, t2;

	// we setup the object parameters
	// if more get added in the future
	// it will be easy to add a new method in the
	// class without breaking backward compatibility
    t1.set_message("hello");
    t2.set_message("world");

	// launching the threads
    t1.run();
    t2.run();

	// interacting with living threads
    for(unsigned int i = 0; i < 20; ++i)
    {
	if(i % 2 == 0)
	    t1.unlock_thread();
	if(i % 3 == 0)
	    t2.unlock_thread();
    }

	// using our own stopping method
    t1.stop();
    t2.stop();
}