C++0x, thread, promise and future

Hello!

I am making a simple template class for communication with two thread in C++0x, using the promise and future.
My class make a channel to communicate thread with the main program and another channel to allow the child thread to initiate communication with the parent thread (or main program).

This is the code:
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
#include <iostream>
#include <future>
#include <thread>

using namespace std;

template<typename T = std::string,typename T1 = std::string>
class ThreadChannel
{
    private:
     std::promise<T> ch1_Promise;
     std::future<T>  ch1_Future;

     std::promise<T1> ch2_Promise;
     std::future<T1>  ch2_Future;

    public:
        ThreadChannel();
        virtual ~ThreadChannel();

         T1 recvFromFather(void);
         void sendToChild(T1);
         //canale 1
         void sendToFather(T);
         T recvFromChild(void);

};


//
template<typename T,typename T1>
ThreadChannel<T,T1>::ThreadChannel()
{
 ch1_Future = ch1_Promise.get_future();
 ch2_Future = ch2_Promise.get_future();
}

template<typename T,typename T1>
ThreadChannel<T,T1>::~ThreadChannel()
{
  //cout<<"Sono nel distruttore!!"<<endl;
}

template<typename T,typename T1>
T ThreadChannel<T,T1>::recvFromChild()
{
  return ch1_Future.get();
}

template<typename T,typename T1>
void ThreadChannel<T,T1>::sendToFather(T data)
{
  return ch1_Promise.set_value(data);
}

template<typename T,typename T1>
void ThreadChannel<T,T1>::sendToChild(T1 data)
{
  return ch2_Promise.set_value(data);
}

template<typename T,typename T1>
T1 ThreadChannel<T,T1>::recvFromFather()
{
  return ch2_Future.get();
}



I use this class in my main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    ThreadChannel<int,int>* myChannel=new ThreadChannel<int,int>();
    // thread
    std::thread* t = new std::thread(myAsyncFun,myChannel);
    int myVar = 33;
    myChannel->sendToChild(myVar);
    int resp = myChannel->recvFromChild();
    cout<<"Response from thread: "<<resp<<endl;

  
    t->join();
    delete myChannel;

    return 0;
}


My Thread code:
1
2
3
4
5
6
7
8
void myAsyncFun(ThreadChannel<int,int>* ch)
{
    while(1)
    {
     int r = ch->recvFromFather();
     ch->sendToFather(++r);
    }
}


Ok...this code work, but when I run cycle (in thread) again, I obtain a runtime error (segmentation fault).

Why?

What do you mean by "run cycle again"?
Are you saying you're instantiating another thread, which coexists with *t?
OR
Are you saying you run the second execution of the entire program?


If you're instantiating another thread which operates on myVar, post the modified code and let's see what changes you've made.
Excuse my bad English.
I'm speaking about the while loop in myAsyncFun.
The first time the loop executes, it works well.
But the second time, when for the second time the statement is executed:

int r = ch-> recvFromFather ();

I have a segmentation fault.
I don't have <future> installed on my machine, so I'm afraid I can't help debug your program.
From my personal experiences, the problem is usually illegal access of memory that doesn't belong to your thread. It happens more than 80% of the time when I get a seg fault.

Why don't you cout the value of r? Since your thread is going to increment r, you *might* get an idea of the execution order/flow by looking at the r values on terminal.
Topic archived. No new replies allowed.