c++ multithread - c++

I use C++ to implement a thread class. My code shows in the following.
I have a problem about how to access thread data.
In the class Thread, I create a thread use pthread_create() function. then it calls EntryPoint() function to start thread created. In the Run function, I want to access the mask variable, it always shows segment fault.
So, my question is whether the new created thread copy the data in original class? How to access the thread own data?
class Thread {
public:
int mask;
pthread_t thread;
Thread( int );
void start();
static void * EntryPoint (void *);
void Run();
};
Thread::Thread( int a) {
mask =a;
}
void Thread::Run() {
cout<<"thread begin to run" <<endl;
cout << mask <<endl; // it always show segmentfault here
}
void * Thread::EntryPoint(void * pthis) {
cout << "entry" <<endl;
Thread *pt = (Thread *) pthis;
pt->Run();
}
void Thread::start() {
pthread_create(&thread, NULL, EntryPoint, (void *)ThreadId );
pthread_join(thread, NULL);
}
int main() {
int input_array[8]={3,1,2,5,6,8,7,4};
Thread t1(1);
t1.start();
}

I'm not familiar with the libraries you're using, but how does EntryPoint know that pthis is a pointer to Thread? Thread (this) does not appear to be passed to pthread_create.

It's great that you're attempting to write a Thread class for educational purposes. However, if you're not, why reinvent the wheel?

pThis is most likely NULL, you should double check that you're passing the correct arguments to pthread_create.

Basically, the problem is as soon as you start your thread, main exits and your local Thread instance goes out of scope. So, because the lifetime of your thread object is controlled by another thread, you've already introduced a race condition.
Also, I'd consider joining a thread immediately after you've created it in Thread::start to be a little odd.

Related

Posix Thread Class and Start Routines (pthread)

I would like to implement a thread class using pthread.
Of course I would like to have different starting routines for each thread I'm creating.
pthread_create tho allows only a static function as starting routine, so it can't be instantiated.
Is there a way to allow that or is it better to use a struct to handle my threads ?
This is the code I wrote sofar:
class thread {
string name;
pthread_t id;
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_attr_t attr;
public:
thread (string t_name);
static void* start(void*);
int id_get();
private:
};
thread::thread (string t_name)
{
name = t_name;
pthread_attr_init(&attr);
int stacksize = sizeof(double) * TH_STACK_SIZE * 30;
pthread_attr_setstacksize(&attr, stacksize);
int rc = pthread_create (&id, &attr, &start, NULL);
cout << "return_code: " << rc << endl;
cout << id;
}
void* thread::start(void*)
{
while(1){
cout << "here";
pthread_exit(NULL);
}
}
int thread::id_get()
{
return id;
}
and my test main:
int main(void) {
cout << "Creating threads" << endl;
thread test1("first");
thread test2("second");
pthread_join(test1.id_get(),NULL);
pthread_join(test2.id_get(),NULL);
return 0;
}
I would like to have different starting routines for each thread I'm
creating.
Back when I used posix thread, (I now use std::thread), I used a 'two-step' entry mechanism. At the (small) cost of these two steps, every class could easily have its own thread.
I always keep these entry methods private.
class Foo_t
{
// ... etc
private:
static void* threadEntry(void* ptr);
void* threadEntry2(void); // thread actions in an object method
// ... etc
}
Because these are private, the class has some public method to create the posix thread, typically something like:
void Foo_t::startApp()
{
// ... etc
int pcStat = m_Thread.create(Foo_t::threadEntry, this);
// this 2 parameter method of my thread wrapper class
// invoked the 4 parameter "::pthread_create(...)".
// The 'this' param is passed into the 4th parameter, called arg.
dtbAssert(0 == pcStat)(m_nodeId)(pcStat)(errno);
// ...
}
Note the second parameter, 'this', to m_Thread.create().
The thread would start in the static method:
void* Foo_t::threadEntry(void* a_ptr)
{
dtbAssert(a_ptr != 0);
Foo_t* a_foo = static_cast<Foo_t*>(a_ptr);
void* retVal = a_foo->threadEntry2();
return(retVal);
}
Here, the void* parameter is filled in with the 'this' pointer of the class instance, and then static_cast back to what we need, a Foo_t*. Remember, this method is private, so only startApp() would create a thread.
Note that threadEntry() invokes an actual method of the class instance called:
void* Foo_t::threadEntry2(void)
{
DBG("Thread %2d (id=%lx): sems %p/%p, "
"Entering sem controlled critical region\n", ...);
// ... start thread work
}
And from here, any method of the instance is available.
So, what next. There are so many ways to proceed to different thread routines.
Consider adding a parameter to startApp:
void Foo_t::startApp(int select);
The 'int select' and a switch/case statement could run a unique threadEntry().
Perhaps the 'int select' could be installed (in the instance) so that a later switch/case in threadEntry() could run a unique method or threadEntry2_x().
Or perhaps the switch/case might be installed in threadEntry2().
Consider that the startApp parameter might be a method pointer.
void Foo_t::startApp(<method pointer>);
The method pointer could be (somewhat more directly) invoked instead of the 'fixed' name threadEntry2().
The above are small issues.
Mutex and having more than 1 thread running in an instance are bigger issues.
I have indeed had multiple threads 'running-around' in a single class instance. For that I used critical sections, under mutex or some other guard mechanisms. std::mutex is convenient, and works with 'Posix' threads, but, on Ubuntu, I often use a Posix Process Semaphore, set to Local mode (unnamed, unshared). PPLSem_t is efficient and fits into 4 one line methods wrapped in a small class.
pthread_create tho allows only a static function as starting routine,
so it can't be instantiated.
There is no difficulty instantiating an instance of a class containing a static method. I'm not sure what you mean in this statement / context.
Review the approach I have detailed above, and you should quickly get to functioning Posix threads in your class instance.
Remember to check on stack usage and how much ram is available on your ARM system. The Ubuntu default stack size is 8 MBytes. Perhaps your ARM provides stack size control.
If you have POSIX threads availble, std::thread will be available for any C++ compiler supporting the current standard (since c++11).
So basically you don't need to roll your own thread class for your cross compiled target (e.g. GCC supports that since version 4.9 or so).
But in general your approach is correct. To make it applicable for various classes you can simmply make the thread class a template:
template<typename T>
class thread {
string name;
pthread_t id;
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_attr_t attr;
public:
thread (string t_name, T& runnable);
static void* start(void*);
int id_get();
T& runnable_;
};
And implement the constructor and start() function as follows:
template<typename T>
thread<T>::thread (string t_name)
: name(t_name)
, runnable_(runnable)
{
pthread_attr_init(&attr);
int stacksize = sizeof(double) * TH_STACK_SIZE * 30;
pthread_attr_setstacksize(&attr, stacksize);
int rc = pthread_create (&id, &attr, &start, this);
// ^^^^
cout << "return_code: " << rc << endl;
cout << id;
}
template<typename T>
void* thread<T>::start(void* pThis) {
thread<T>* realThis = reinterpret_cast<thread<T>*>(pThis);
(realThis->runnable)_.start();
pthread_exit(NULL);
}
The thread class can be used then like follows:
struct MyRunnable {
MyRunnable(/* Whatever parameters needed */)
: /* Whatever needs to be initialized */ {
}
void start() {
/* Full access to all class member variables */
}
}
int main() {
MyRunnable run(/* Whatever parameters needed */);
thread<MyRunnable> t("TheTreadName",run); // start() will execute here
// do concurrent stuff
t.join();
}
I just would choose a different name as thread to avoid any clashes with the c++ standard library.

passing one function to different threads

I have to create an application where I'll have to make multiple threads. SoI thought to try making one function and passing it to different threads. Initially I've created two threads and have declared one function to be passed to both of them. All I am trying to do is to pass different integers to those threads and display it in the thread function,here is my code:
DWORD WINAPI Name(LPVOID lpParam)
{
int *Ptr=(int*)lpParam;
for(int j=0;j<2;j++)
{
cout<<"Thread"<<endl;
cout<<*Ptr<<endl;
}
return 0;
}
int main()
{
int a=10,b=15,c=25;
HANDLE thread1,thread2;
DWORD threadID,threadID2;
thread2= CreateThread(NULL,0,Name,LPVOID(a),0,&threadID2);
thread1= CreateThread(NULL,0,Name,LPVOID(b),0,&threadID);
for(int i=0;i<5;i++)
{
cout<<"Main Thread"<<endl;
}
if(thread1==NULL)
{
cout<<"Couldn't Create Thread:("<<endl;
exit(0);
}
if(thread2==NULL)
{
cout<<"Couldn't Create Thread:("<<endl;
exit(0);
}
return 0;
}
but this code is not running properly,i.e compliles fine,starts fine but afterwards gives a debugging error.
Could someone let let me know of my mistake and how I could correct it coz being able to utilize one function for multiple threads will be really helpful for me.
Wait for your child threads to return. Do this:
int main()
{
int a=10,b=15,c=25;
HANDLE thread[2];
DWORD threadID,threadID2;
thread[1]= CreateThread(NULL,0,Name,LPVOID(a),0,&threadID2);
thread[0]= CreateThread(NULL,0,Name,LPVOID(b),0,&threadID);
for(int i=0;i<5;i++)
{
cout<<"Main Thread"<<endl;
}
if(thread[0]==NULL)
{
cout<<"Couldn't Create Thread:("<<endl;
exit(0);
}
if(thread[1]==NULL)
{
cout<<"Couldn't Create Thread:("<<endl;
CloseHandle(thread[0]);
exit(0);
}
WaitForMultipleObjects(2, thread, TRUE, INFINITE);
CloseHandle(thread[0]);
CloseHandle(thread[1]);
return 0;
}
The handle of a thread is signaled when the thread is terminated (refer CreateThread).
You are passing the address of a local variable in the function into your thread. By the time the thread gets around to running your function has probably exited main already. So the thread will try to access a variable that no longer exists on the stack so will be reading some random value which when you try to dereference it as a pointer will likely crash.
You main needs to wait. For a simple test just put in a Sleep(10000) or something before it exits. Obviously that's no use for a real program.
To summarize the comments: There are two ways you can pass data. Either directly inside the void pointer, because "void pointer" is an integral type and thus can represent integers (but it doesn't necessarily have the same width as int), or indirectly by passing an actual address of the thing you care about.
Method 1 (pass the value):
DWORD WINAPI Name(LPVOID lpParam)
{
intptr_t n = reinterpret_cast<intptr_t>(lpParam);
// ...
}
int main()
{
intptr_t a = 10;
thread1 = CreateThread(NULL, 0, Name, reinterpret_cast<void *>(a), 0, &threadID);
// ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
For this method, we use the integral type intptr_t, which has the same width as void *. We use reinterpret-casts to store and retrieve arbitrary integral values.
Method 2 (pass a pointer):
DWORD WINAPI Name(LPVOID lpParam)
{
T * p = static_cast<T *>(lpParam);
// ... use *p ...
}
int main()
{
T thing_I_care_about;
thread1 = CreateThread(NULL, 0, Name, &thing_I_care_about, 0, &threadID);
// ... ^^^^^^^^^^^^^^^^^^^
}
This is the more general method, but requires that thing_I_care_about remain alive, and it becomes a brittle shared state, so lifetime management and synchronisation become issues. Note that any object pointer is implicitly convertible to void *, so there's no need for the cast at the call site.
Finally, as others have commented, don't forget to join or detach your threads.

user defined thread library constructor getting invoked only once

I created my own thread library... Of course its a simple non-preemptive library in which the threads are executed in the order they are invoked. The brief out line of the program is as follows:
class Thread
{
static Thread sched;
/*called only once*/
static void init()
{
*create a context for sched thread*
static void schedule()
{
....
setcontext(current thread from the queue);
return if empty
}
Thread(function)
{
intializes the context via getcontext and sets the uc_link to the context of sched
and pushed into the queue and calls schedule function
}
Every thing seems to be fine with single thread. But when i initialize two thread objects only one of them is executing.
I doubt that when the first thread completes its job, it returns to the schedule function and when the schedule function sees the queue is empty... it also returns.
But I observe that the constructor is called only once for the first thread!! Why is that so?
If i don't call the schedule function from the constuctor, instead i define a function like
void start()
{
schedule();
}
and call it after all the threads are initialized it was executing correctly. But i dont want to use this method..
please explain me the path of execution and the solution to the above problem.
Here's the actual code for it
class thread
{
static queue<thread> Q;
ucontext_t con;
int u_p;
int c_p;
static void init()
{
flag=0;
getcontext(&schd.con);
//cout<<"fgh\n";
schd.con.uc_link=0;
schd.con.uc_stack.ss_sp=malloc(ST_S);
schd.con.uc_stack.ss_size=ST_S;
makecontext(&schd.con, schd.schedule, 0);
}
static thread main, schd;
static int flag;
public:
thread()
{ }
thread(int i){ init(); }
static void schedule()
{
//cout<<"ii\n";
if(Q.empty()){
flag=0;
return;
}
main=Q.front();
Q.pop();
setcontext(&main.con);
init();
//cout<<"yea\n";
}
thread(void (*fn)(), int u_p=15)
{
getcontext(&con);
con.uc_link=&schd.con;
con.uc_stack.ss_sp=malloc(ST_S);
con.uc_stack.ss_flags=0;
con.uc_stack.ss_size=ST_S;
makecontext(&con, fn, 0);
//cout<<"hjkl\n";
Q.push(*this);
if(flag==0){
flag=1;
schedule();
}
}
static void start(){ schedule(); }
};
queue<thread> thread::Q;
thread thread::main;
thread thread::schd;
int thread::flag;
Seems like this is happening:
you create a new thread
Thread::Thread() calls Thread::shedule()
Thread::shedule() calls setcontext()
control flow switches to function of new Thread object
this new thread executes, not switching back to its parent context, so another thread cannot be created
I think all thread libraries and OS API's I have seen either create threads suspended or provide a flag to create threads in suspended state. So it's a good way to go, otherwise you lose stop current thread at the moment of creating another one. Moreover, since you use lightweight threads, you're almost forced to create threads suspended without any flag in order to be able to create multiple threads from the main one.

c++ multithread [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
c++ multithread
I use c++ to implement a thread class. The code is in the following.
I initialize two objects, wish it will start two threads (I use pthread_self() to look the thread Id).
But the result shows that there is only one thread beside the main thread.
I am a bit confused...
class Thread {
public:
int mask;
pthread_t thread;
Thread( int );
void start();
static void * EntryPoint (void *);
void Run();
};
Thread::Thread( int a) {
mask =a;
}
void Thread::Run() {
cout<<"thread begin to run" <<endl;
cout <<" Thread Id is: "<< pthread_self() << endl; // the same thread Id.
}
void * Thread::EntryPoint(void * pthis) {
cout << "entry" <<endl;
Thread *pt = (Thread *) pthis;
pt->Run();
}
void Thread::start() {
pthread_create(&thread, NULL, EntryPoint, (void *)ThreadId );
pthread_join(thread, NULL);
}
int main() {
int input_array[8]={3,1,2,5,6,8,7,4};
Thread t1(1);
Thread t2(2);
t1.start();
t2.start()
}
You are seeing this behavior because you join with each of your threads immediately after you spawn them.
When you join with a thread, you block until the thread terminates.
You are spawning two threads, but the first thread is joined (and destroyed) before the second thread is spawned, so you have effectively only one thread running at a time. The way to fix this is:
Create a separate join function that invokes join().
Do not call join directly from your start() function.
In your join() function, be sure to mark the thread as having been joined/destroyed.
In your destructor, if your thread has not been joined, then you should detach it.
I should also point out that boost::thread provides cross-platform multithreading for C++.

QNX c++ thread question

I have a question concerning this code which I want to run on QNX:
class ConcreteThread : public Thread
{
public:
ConcreteThread(int test)
{
testNumber = test;
}
void *start_routine()
{
for(int i = 0; i < 10; i++)
{
sleep(1);
cout << testNumber << endl;
}
}
private:
int testNumber;
};
class Thread
{
public:
Thread(){};
int Create()
{
pthread_t m_id;
return pthread_create(&m_id, NULL, &(this->start_routine_trampoline), this);
}
protected:
virtual void *start_routine() = 0;
private:
static void *start_routine_trampoline(void *p)
{
Thread *pThis = (Thread *)p;
return pThis->start_routine();
}
};
Now, when I run this code without the sleep in *start_routine, it will simply print the number 10 times, before continuing on to the next line of code (sequential instead of parallel). However, when I use a sleep like in the code, it doesn't print any numbers at all and simply goes on to the next line of code. Why doesn't sleep work and how can I make a thread like this work, instead of running sequential?
Note 1: If you only have 1 processor the code can only be done sequentially no matter how many threads you create. Each thread is given a slice of processor time before it is swapped out for the next threads.
Note 2: If the main thread exits pthreads will kill all child threads before they have a chance to execute.
Now to answer you questions:
Without the sleep. The thread once started has enough time in the single slice it was given to execute the loop 10 times completely.
With the sleep: Your worker thread is going to sleep for a full second. So your main thread has time to do a lot of work. If the main thread exits in this time the worker will be killed.
I would make the following changes:
// Remove the Create() method
// Put thread creation in the constructor.
// Make the thread variable part of the object
pthread_t m_id;
Thread()
{
if (pthread_create(&m_id, NULL, &(this->start_routine_trampoline), this) != 0)
{
throw std::runtime_error("Thread was not created");
}
}
// Make sure the destructor waits for the thread to exit.
~Thread()
{
pthread_join(m_id);
}
If you go and look at boost threading library. you will find that all the little mistakes like this have already been taken care of; Thus making threading easier to use.
Also note. That using a static may work but it is non portable. This is because pthread's is a C library and is thus expecting a function pointer with a C ABI. You are just getting lucky for your platform here. You need to define this as a function and declare the ABI by using extern "C"
// This needs to be a standard function with C Interface.
extern "C" void *start_routine_trampoline(void *p)
{
}
Try to make the pthread_t id a class member instead of a function local variable. That way the caller can pthread_join it.
Not doing this is technically a resource leak (unless the thread is specifically not joinable). And joining will avoid the issue that Martin York described.
From man pthread_join:
The joined thread th must be in the joinable state: it must not have
been detached using pthread_detach(3) or the PTHREAD_CREATE_DETACHED
attribute to pthread_create(3).
When a joinable thread terminates, its memory resources (thread
descriptor and stack) are not deallocated until another thread performs
pthread_join on it. Therefore, pthread_join must be called once for
each joinable thread created to avoid memory leaks.
Going off on a tangent here... With respect to Martin York's post:
Also note. That using a static may work but it is non portable. This is because pthread's is a C library and is thus expecting a function pointer with a C ABI. You are just getting lucky for your platform here. You need to define this as a function and declare the ABI by using extern "C" // This needs to be a standard function with C Interface. extern "C" void * start_routine_trampoline(void * p) {...}
I'm not so sure about that...
(1) C++ was designed to be as compatible with C as possible. There are a few differences... But I was under the impression that extern "C" was used mostly to circumvent the name-mangling required to implement C++ function overloading.
(2) It seems like, once you have the function pointer, the calling conventions (what gets pushed on the stack to make the function call) just has to be the same between C & C++. Otherwise, how would function pointers work?
E.g.:
C code:
void bar( int i ) { printf( "bar %d\n", i ); }
C++ code:
class Foo
{
public:
static void foo( int i ) { cout << "foo " << i << endl; }
};
extern "C" { void bar(int); }
int main()
{
void (*p)(int);
p = & Foo::foo;
(*p)(1);
p = & bar;
(*p)(2);
}