How to use a single object in multiple threads? - c++

I want to use a single object in multiple threads using c++. I know from java that threads share all variables, but it seems that in c++ it is different.
I have the following structure to store the date
Class Flow: has multiple integers
Class UE: has a list<Flow*>
Class FlowTable: has a map<int,UE*>
Now have two threads(Objects: InOutReader and OutInReader), each of them has a FlowTable* and shall read and/or insert data to the FlowTable.
in the main() of my starter I call new FlowTable(), create the threaded objects and give the FlowTable* to them using a setter. But in the end it looks like that the two threads work with different FlowTable objects.
class InOutReader{
public:
start(){
while(true){
//read data from somewhere(tap-interface1)
//extract address from ip packet and tcp/udp header etc
Flow* myflow = new Flow(IPsrc,IPdest);
this->myflowTable->insertFlow(myflow);
}
}
}
class OutInReader{
public:
start(){
while(true){
//read data from somewhere(tap-interface1)
//extract address from ip packet and tcp/udp header etc
Flow* myflow = new Flow(IPsrc,IPdest);
this->myflowTable->existsFlow(myflow);// should return true if a flow with the same data was inserted before
}
}
}
main programm
FlowTable* myflowTable;
startThreadOne(){
InOutReader ior = InOutReader();
ior.setFlowTable(myFlowTable);
ior.start();
}
startThreadtwo(){
InOutReader oir = InOutReader();
oir.setFlowTable(myFlowTable);
oir.start();
}
void main(){
myFlowTable = new FlowTable();
std::thread t1 = std::thread(startThreadOne);
std::thread t2 = std::thread(startThreadtwo);
t1.join();
t2.join();
}
What I have to do to use the same FlowTable Object in multiple threads?

I can't make heads or tails of your explication, but if you want to have the two threads sharing the same dynamically allocated FlowTable, the solution in C++ is incredibly simple:
int
main()
{
FlowTable* sharedFlowTable = new FlowTable();
std::thread t1( startThread1, sharedFlowTable );
std::thread t2( startThread2, sharedFlowTable );
// ...
}
Then declare startThread1 and startThread2 to take a FlowTable* as argument. (This is a lot simpler than in Java; in Java, where you'd have to define one class per thread, deriving from Runnable, and give each class a constructor which took a FlowTable, and copied it to a member variable so that the run function could find it.)
EDIT:
Of course, if the value pointed to by sharedFlowTable really is a FlowTable, and no inheritance and factory functions are involved, you could just make it a local variable in main, rather than a pointer, and pass &sharedFlowTable to the threads. This would be even simpler, and more idiomatic in C++. (And I have to thank #5gon12eder for pointing this out. Embarrassingly, because I'm usually the one arguing against dynamic allocation unless it is necessary.)

Related

How to transfer data between main function and threads?

I want to store the threads that I have created in my main function in an array and later access them in my Thread class. What would be the best way to do this?. Below is the structure of my code.
Main.cpp:
int main(){
//do something
while ((new_socket = socket.Accept())) {
std::thread mythread(&Myclass::MyThread, &myclass,
std::move(new_socket), para1);
// I want to store the above threads created in an array which can later be accessed in a different thread class
}
}
MyClass.cpp
MyClass::MyThread(....){
I want to access the array of threads here.
}
I have tried mutex and cv and add these threads into a queue but it is generating many errors. What is the best way to solve this?
I am not 100% sure if array fits the bill - do you know how many threads there will be?
Maybe a vector is better:
std::vector<std::thread> threads; // could be a member - used to store the threads
while ((new_socket = socket.Accept()))
{
// Construct the thread in place inside the vector
threads.emplace_back(&Myclass::MyThread, &myclass, std::move(new_socket), para1);
}
Now you have all your threads neatly in a vector (note I did not test this with you code because there is not enough of it to test... its not a complete example)

vectors and threading in C++

I would like to request for comments/ideas on how to resolve a vector and threading issue that is becoming a bit convoluted. I have researched the issue and have not found a working solution.
The intention is to have 5 person objects (stored in a vector) and then call each of their method to read a book concurrently (hence the threads). The read function contains a never ending loop.
I have the following code(simplified) set up:
class Book{
private:
// some data
public:
// some functions
};
class Person{
private:
// some data vars
int id;
Book abook;
public:
// some functions
Person(int index=0); // constructor
void readBook();
};
int main(void){
vector<Person>PersonsVector;
vector<thread> threads;
int num_of_persons = 5;
for(int i=0; i<num_of_persons; i++){
PersonsVector.push(Person(i)); //create different people
threads.push_back(thread(PersonsVector[i].readBook)); //read book in thread
}
// wait for threads to finnish or kill threads
for(auto& thread: threads)
threads.join();
}
My issue is really in how to call the readBook() function and push this to a thread
threads.push_back(thread(PersonsVector[i].readBook)); //read book in thread
Various variations for example these below lead to errors...
threads.push_back(thread(&PersonsVector[i].Person::readBook, this));
threads.push_back(thread(&Person::PersonsVector[i].Person::readBook, this));
Any ideas how I could resolve this issue?
A pointer-to-method must be called using a combination of a pointer specifying the class and method + an instance of said class on which to invoke said method.
This... thing:
&Person::PersonsVector[i].Person::readBook
actually says...
Get me the class Person,
then a vector within an unspecified instance of Person, which does not contain any such vector...
then an index i within that invalid vector,
then somehow get the constructor of that instance,
then somehow get a method readBook() from within that constructor.
See the problem? That's invalid syntax in so many varied ways.
The correct syntaxes are as follows:
For the pointer-to-method: &Person::readBook
For the instance: PersonsVector[i]
...and you need to give these to std::thread via some mechanism that will bundle the pointer-to-method and instance and make them callable for you - such as a lambda, std::bind, etc. - something like this:
std::thread theThread{
[] // not sure which capture is needed... :/
{
(someInstance.*pointerToMethod)(the, args);
// i.e.
(PersonsVector[i].*readBook)();
}
};
You can use a lambda:
threads.push_back(thread([&PersonsVector[i]])(Person& p){ p.readBook(); });

Trouble tracking down a Bus Error/Seg Fault in C++ and Linux

I have a program that processes neural spike data that is broadcast in UDP packets on a local network.
My current program has two threads a UI thread and a worker thread. The worker thread simply listens for data packets, parses them and makes them available to the UI thread for display and processing. My current implementation works just fine. However for a variety of reasons I'm trying to re-write the program in C++ using an Object Oriented approach.
The current working program initialized the 2nd thread with:
pthread_t netThread;
net = NetCom::initUdpRx(host,port);
pthread_create(&netThread, NULL, getNetSpike, (void *)NULL);
Here is the getNetSpike function that is called by the new thread:
void *getNetSpike(void *ptr){
while(true)
{
spike_net_t s;
NetCom::rxSpike(net, &s);
spikeBuff[writeIdx] = s;
writeIdx = incrementIdx(writeIdx);
nSpikes+=1;
totalSpikesRead++;
}
}
Now in my new OO version of the program I setup the 2nd thread in much the same way:
void SpikePlot::initNetworkRxThread(){
pthread_t netThread;
net = NetCom::initUdpRx(host,port);
pthread_create(&netThread, NULL, networkThreadFunc, this);
}
However, because pthead_create takes a pointer to a void function and not a pointer to an object's member method I needed to create this simple function that wraps the SpikePlot.getNetworSpikePacket() method
void *networkThreadFunc(void *ptr){
SpikePlot *sp = reinterpret_cast<SpikePlot *>(ptr);
while(true)
{
sp->getNetworkSpikePacket();
}
}
Which then calls the getNetworkSpikePacket() method:
void SpikePlot::getNetworkSpikePacket(){
spike_net_t s;
NetCom::rxSpike(net, &s);
spikeBuff[writeIdx] = s; // <--- SegFault/BusError occurs on this line
writeIdx = incrementIdx(writeIdx);
nSpikes+=1;
totalSpikesRead++;
}
The code for the two implementations is nearly identical but the 2nd implementation (OO version) crashes with a SegFault or BusError after the first packet that is read. Using printf I've narrowed down which line is causing the error:
spikeBuff[writeIdx] = s;
and for the life of me I can't figure out why its causing my program to crash.
What am I doing wrong here?
Update:
I define spikeBuff as a private member of the class:
class SpikePlot{
private:
static int const MAX_SPIKE_BUFF_SIZE = 50;
spike_net_t spikeBuff[MAX_SPIKE_BUFF_SIZE];
....
}
Then in the SpikePlot constructor I call:
bzero(&spikeBuff, sizeof(spikeBuff));
and set:
writeIdx =0;
Update 2: Ok something really weird is going on with my index variables. To test their sanity I changed getNetworkSpikePacket to:
void TetrodePlot::getNetworkSpikePacket(){
printf("Before:writeIdx:%d nspikes:%d totSpike:%d\n", writeIdx, nSpikes, totalSpikesRead);
spike_net_t s;
NetCom::rxSpike(net, &s);
// spikeBuff[writeIdx] = s;
writeIdx++;// = incrementIdx(writeIdx);
// if (writeIdx>=MAX_SPIKE_BUFF_SIZE)
// writeIdx = 0;
nSpikes += 1;
totalSpikesRead += 1;
printf("After:writeIdx:%d nspikes:%d totSpike:%d\n\n", writeIdx, nSpikes, totalSpikesRead);
}
And I get the following output to the console:
Before:writeIdx:0 nspikes:0 totSpike:0
After:writeIdx:1 nspikes:32763 totSpike:2053729378
Before:writeIdx:1 nspikes:32763 totSpike:2053729378
After:writeIdx:1 nspikes:0 totSpike:1
Before:writeIdx:1 nspikes:0 totSpike:1
After:writeIdx:32768 nspikes:32768 totSpike:260289889
Before:writeIdx:32768 nspikes:32768 totSpike:260289889
After:writeIdx:32768 nspikes:32768 totSpike:260289890
This method is the only method where I update their values (besides the constructor where I set them to 0). All other uses of these variables are read only.
I'm going to go on a limb here and say all your problems are caused by the zeroing out of the spike_net_t array.
In C++ you must not zero out objects with non-[insert word for 'struct-like' here] members. i.e. if you have an object that contains a complex object (a std string, a vector, etc. etc.) you cannot zero it out, as this destroys the initialization of the object done in the constructor.
This may be wrong but....
You seemed to move the wait loop logic out of the method and into the static wrapper. With nothing holding the worker thread open, perhaps that thread terminates after the first time you wait for a UDP packet, so second time around, sp in the static method now points to an instance that has left scope and been destructed?
Can you try to assert(sp) in the wrapper before trying to call its getNetworkSpikePacket()?
It looks like your reinterpret_cast might be causing some problems. When you call pthread_create, you are passing in "this" which is a SpikePlot*, but inside networkThreadFunc, you are casting it to a TetrodePlot*.
Are SpikePlot and TetrodePlot related? This isn't called out in what you've posted.
If you are allocating the spikeBuff array anywhere then make sure you are allocating sufficient storage so writeIdx is not an out-of-bounds index.
I'd also check that initNetworkRxThread is being called on an allocated instance of spikePlot object (and not on just a declared pointer).

Encapsulated boost thread_group. Questions about ids and synchronization

I´m using a class that encapsulates a thread_group, and have some questions about it
class MyGroup{
private:
boost::this_thread::id _id;
boost::thread::thread_group group;
int abc;
//other attributes
public:
void foo();
};
In the class constructor, i launch N threads
for (size_t i=0;i<N;i++){
group.add(new boost::thread(boost::bind(&foo,this)));
}
void foo(){
_id = boost::this_thread::get_id();
//more code.
abc++ //needs to be sync?
}
So, here are my questions.
Do class attributes need to be synchronized?
Do every thread get a different id? For example, if I have
void bar(){
this->id_;
}
will this result in different ids for each thread, or the same for everyone?
Thanks in advance !
Yes, shared data access must be protected even if you use thread creation helpers as boost.
In the end they all will execute the same code at the same time, and there is nothing a library can do to put protection around a variable you own and you manage.
If this->_id prints the current thread id then yes, it will print different values while different threads access it.
I don't know what you are doing with this thread_group so this may or may not apply.
Yes, all threads will have a unique ID.
Yes, you need to protect your shared state, you can do this with synchronization or by 'avoiding' shared state by copying it or with message passing.
A relevant pattern here is the 'actor' pattern.
Essentially rather than just create threads in your constructor, consider either:
a) have a class that derives from boost::thread and store thread specific members there. you can then access the member variables in the thread which won't be global to the group.
e.g.
class MyThreadClass : public boost::thread
{
private:
int thread_local_int;
...
}
b) have a class that contains a boost::thread as a member variable
class MyThreadClass : public boost::thread
{
private:
int thread_local_int;
boost::thread t;
public:
boost::thread& GetThread()
{
return t;
}
...
}
store a collection of either of these in your MyGroup class and use thread_group::add_thread to put the threads in the thread_group.
You can now be incredibly thoughtful about which state is shared in the thread_group (it should be synchronized or read-only) and which state is local to your actor (or thread) and how it's accessible.
Note, I have a personal reluctance against using TLS because I like having some control and guarantees over lifetimes of objects and threads and I just find this easier when I don't use it; YMMV and its great for some uses...

Detecting when an object is passed to a new thread in C++?

I have an object for which I'd like to track the number of threads that reference it. In general, when any method on the object is called I can check a thread local boolean value to determine whether the count has been updated for the current thread. But this doesn't help me if the user say, uses boost::bind to bind my object to a boost::function and uses that to start a boost::thread. The new thread will have a reference to my object, and may hold on to it for an indefinite period of time before calling any of its methods, thus leading to a stale count. I could write my own wrapper around boost::thread to handle this, but that doesn't help if the user boost::bind's an object that contains my object (I can't specialize based on the presence of a member type -- at least I don't know of any way to do that) and uses that to start a boost::thread.
Is there any way to do this? The only means I can think of requires too much work from users -- I provide a wrapper around boost::thread that calls a special hook method on the object being passed in provided it exists, and users add the special hook method to any class that contains my object.
Edit: For the sake of this question we can assume I control the means to make new threads. So I can wrap boost::thread for example and expect that users will use my wrapped version, and not have to worry about users simultaneously using pthreads, etc.
Edit2: One can also assume that I have some means of thread local storage available, through __thread or boost::thread_specific_ptr. It's not in the current standard, but hopefully will be soon.
In general, this is hard. The question of "who has a reference to me?" is not generally solvable in C++. It may be worth looking at the bigger picture of the specific problem(s) you are trying to solve, and seeing if there is a better way.
There are a few things I can come up with that can get you partway there, but none of them are quite what you want.
You can establish the concept of "the owning thread" for an object, and REJECT operations from any other thread, a la Qt GUI elements. (Note that trying to do things thread-safely from threads other than the owner won't actually give you thread-safety, since if the owner isn't checked it can collide with other threads.) This at least gives your users fail-fast behavior.
You can encourage reference counting by having the user-visible objects being lightweight references to the implementation object itself [and by documenting this!]. But determined users can work around this.
And you can combine these two-- i.e. you can have the notion of thread ownership for each reference, and then have the object become aware of who owns the references. This could be very powerful, but not really idiot-proof.
You can start restricting what users can and cannot do with the object, but I don't think covering more than the obvious sources of unintentional error is worthwhile. Should you be declaring operator& private, so people can't take pointers to your objects? Should you be preventing people from dynamically allocating your object? It depends on your users to some degree, but keep in mind you can't prevent references to objects, so eventually playing whack-a-mole will drive you insane.
So, back to my original suggestion: re-analyze the big picture if possible.
Short of a pimpl style implementation that does a threadid check before every dereference I don't see how you could do this:
class MyClass;
class MyClassImpl {
friend class MyClass;
threadid_t owning_thread;
public:
void doSomethingThreadSafe();
void doSomethingNoSafetyCheck();
};
class MyClass {
MyClassImpl* impl;
public:
void doSomethine() {
if (__threadid() != impl->owning_thread) {
impl->doSomethingThreadSafe();
} else {
impl->doSomethingNoSafetyCheck();
}
}
};
Note: I know the OP wants to list threads with active pointers, I don't think that's feasible. The above implementation at least lets the object know when there might be contention. When to change the owning_thread depends heavily on what doSomething does.
Usually you cannot do this programmatically.
Unfortuately, the way to go is to design your program in such a way that you can prove (i.e. convince yourself) that certain objects are shared, and others are thread private.
The current C++ standard does not even have the notion of a thread, so there is no standard portable notion of thread local storage, in particular.
If I understood your problem correctly I believe this could be done in Windows using Win32 function GetCurrentThreadId().
Below is a quick and dirty example of how it could be used. Thread synchronisation should rather be done with a lock object.
If you create an object of CMyThreadTracker at the top of every member function of your object to be tracked for threads, the _handle_vector should contain the thread ids that use your object.
#include <process.h>
#include <windows.h>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class CMyThreadTracker
{
vector<DWORD> & _handle_vector;
DWORD _h;
CRITICAL_SECTION &_CriticalSection;
public:
CMyThreadTracker(vector<DWORD> & handle_vector,CRITICAL_SECTION &crit):_handle_vector(handle_vector),_CriticalSection(crit)
{
EnterCriticalSection(&_CriticalSection);
_h = GetCurrentThreadId();
_handle_vector.push_back(_h);
printf("thread id %08x\n",_h);
LeaveCriticalSection(&_CriticalSection);
}
~CMyThreadTracker()
{
EnterCriticalSection(&_CriticalSection);
vector<DWORD>::iterator ee = remove_if(_handle_vector.begin(),_handle_vector.end(),bind2nd(equal_to<DWORD>(), _h));
_handle_vector.erase(ee,_handle_vector.end());
LeaveCriticalSection(&_CriticalSection);
}
};
class CMyObject
{
vector<DWORD> _handle_vector;
public:
void method1(CRITICAL_SECTION & CriticalSection)
{
CMyThreadTracker tt(_handle_vector,CriticalSection);
printf("method 1\n");
EnterCriticalSection(&CriticalSection);
for(int i=0;i<_handle_vector.size();++i)
{
printf(" this object is currently used by thread %08x\n",_handle_vector[i]);
}
LeaveCriticalSection(&CriticalSection);
}
};
CMyObject mo;
CRITICAL_SECTION CriticalSection;
unsigned __stdcall ThreadFunc( void* arg )
{
unsigned int sleep_time = *(unsigned int*)arg;
while ( true)
{
Sleep(sleep_time);
mo.method1(CriticalSection);
}
_endthreadex( 0 );
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hThread;
unsigned int threadID;
if (!InitializeCriticalSectionAndSpinCount(&CriticalSection, 0x80000400) )
return -1;
for(int i=0;i<5;++i)
{
unsigned int sleep_time = 1000 *(i+1);
hThread = (HANDLE)_beginthreadex( NULL, 0, &ThreadFunc, &sleep_time, 0, &threadID );
printf("creating thread %08x\n",threadID);
}
WaitForSingleObject( hThread, INFINITE );
return 0;
}
EDIT1:
As mentioned in the comment, reference dispensing could be implemented as below. A vector could hold the unique thread ids referring to your object. You may also need to implement a custom assignment operator to deal with the object references being copied by a different thread.
class MyClass
{
public:
static MyClass & Create()
{
static MyClass * p = new MyClass();
return *p;
}
static void Destroy(MyClass * p)
{
delete p;
}
private:
MyClass(){}
~MyClass(){};
};
class MyCreatorClass
{
MyClass & _my_obj;
public:
MyCreatorClass():_my_obj(MyClass::Create())
{
}
MyClass & GetObject()
{
//TODO:
// use GetCurrentThreadId to get thread id
// check if the id is already in the vector
// add this to a vector
return _my_obj;
}
~MyCreatorClass()
{
MyClass::Destroy(&_my_obj);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
MyCreatorClass mcc;
MyClass &o1 = mcc.GetObject();
MyClass &o2 = mcc.GetObject();
return 0;
}
The solution I'm familiar with is to state "if you don't use the correct API to interact with this object, then all bets are off."
You may be able to turn your requirements around and make it possible for any threads that reference the object subscribe to signals from the object. This won't help with race conditions, but allows threads to know when the object has unloaded itself (for instance).
To solve the problem "I have an object and want to know how many threads access it" and you also can enumerate your threads, you can solve this problem with thread local storage.
Allocate a TLS index for your object. Make a private method called "registerThread" which simply sets the thread TLS to point to your object.
The key extension to the poster's original idea is that during every method call, call this registerThread(). Then you don't need to detect when or who created the thread, it's just set (often redundantly) during every actual access.
To see which threads have accessed the object, just examine their TLS values.
Upside: simple and pretty efficient.
Downside: solves the posted question but doesn't extend smoothly to multiple objects or dynamic threads that aren't enumerable.