Why string variable print with garbage - c++

I wrote the following simple program to understand threads. In the result there are some garbage characters - why are these characters appearing? I'm using GNU G++ compiler in Fedora 17.
#include <iostream>
#include <string.h>
#define THREAD_COUNT 5
struct Man
{
char name[10];
int age;
std::string address;
};
void* ThreadTask(void* man)
{
Man *my_man = (Man*)man;
std::cout << "Address of the person: " << my_man->address << std::endl;
}
int main()
{
pthread_t threads[THREAD_COUNT];
Man men_list[THREAD_COUNT];
for(int i=0; i<THREAD_COUNT; i++)
{
strcpy(men_list[i].name,"nayana");
men_list[i].age = i;
men_list[i].address = "Singapore";
int rc = pthread_create(&threads[i],NULL,ThreadTask,(void*)&men_list[i]);
if(rc)
{
std::cout << "Error while creating the thread" << std::endl;
}
}
pthread_exit(NULL);
return 0;
}
Result:
Address of the person: Singapore�0+���*��! ����Singapore�0��▒s�!��t��s���E��t��s���EI
Address of the person: Singapore�0;s��:s�!�w ����Singapore�0+���*��! ����Singapore�0��▒s�!��t��s���E��t��s���EI
Address of the person: Address of the person:

You need to ensure that all the threads run before men_list goes out of scope and the memory is reclaimed.
This is not what pthread_exit does -- rather it terminates the calling thread.
The pthread_exit(NULL) at the end of your main() is not accomplishing anything sensible. Even though the man page says that exiting the main function with pthread_exit will "allow other threads to run to completion" (as you refer to in the comments), this doesn't mean that the scope of the main function won't end when the function does, nor does it mean that the other threads will run to completion before pthread_exit returns.
You can use pthread_join to wait for a thread to run to completion. To wait for all your threads to run to completion, you could use something like this:
int main()
{
pthread_t threads[THREAD_COUNT];
Man men_list[THREAD_COUNT];
for(int i=0; i<THREAD_COUNT; i++)
{
strcpy(men_list[i].name,"nayana");
men_list[i].age = i;
men_list[i].address = "Singapore";
int rc = pthread_create(&threads[i],NULL,ThreadTask,(void*)&men_list[i]);
if(rc)
{
std::cout << "Error while creating the thread" << std::endl;
}
}
for(int i=0; i < THREAD_COUNT; i++)
{
pthread_join( threads[i], NULL );
}
return 0;
}

Also I see synchronization issue. You need to synchronize the common resource whenever you are using threads. Here common resource is std::cout. one way could be put the printing code under some synchronization object. then you will have everything printed in order.

Related

C++: Changing global variable in multiple threads

I don't really know much about threads in c++. Here is a simple code:
int a = 0;
int main()
{
std::thread t1([=]() {
for (int i = 0; i < 10; i++)
{
a += 1;
std::cout << "A in another thread: " << a << std::endl;
}
});
for (int i = 0; i < 10; i++)
{
a += 1;
std::cout << "A in main: " << a << std::endl;
}
}
When I run this code it gives me a Debug Error: abort() has been called.
How can I change a global variable's value on multiple threads?
because you didn't joined thread after your programme finished. OS will send SIGABRT to detached thread to avoid this problem you should join the thread.
you should put below line at end of main:
t1.join()
Note: beware about possible race condition on variable a.
You have 2 issues:
First of all when you start another thread you have to join it or detach before the program terminates.
Second thing is that you can read the same variable from many threads, but writing to it creates race condition.

How to print to standard output from multiple threads using POCO in c++

I want to create a simple program that starts 5 threads that print "hello world! This is thread [thread number]".
Each print operation should be preceded by a random wait operation, to demonstrate that the threads are running concurrently. (The threads will print their message in a random order, as opposed to printing in order which would happen if the threads are running sequentially.)
Here is the code that should achieve this:
Poco::Thread thread[5];
class HelloRunnable: public Poco::Runnable
{
public:
HelloRunnable(int arg)
{
n = arg;
}
int n;
virtual void run()
{
usleep(rand()%100000);
std::cout << "Hello, world! This is thread " << n << std::endl;
}
};
int main()
{
for(int i=0; i<5; i++)
{
HelloRunnable runnable(i);
thread[i].start(runnable);
}
for(int i=0; i<5; i++)
{
thread[i].join();
}
return 0;
}
However, at run time, this gives the error:
//pure virtual method called
//terminate called without an active exception
//The program has unexpectedly finished.
If instead, I put thread[i].join() inside the same for loop as thread[i].start(), then the program runs without error but it prints the threads in order. (Becuase join() waits until the thread has finished before moving on, hence the threads are executed sequentially and not concurrently.
How can I make the threads run concurrently and print out the message to the standard output as soon as cout is called?
Since you create the objects inside the for loop their lifetime will end immediately after each iteration. This will cause issues. As the documentation for start() states:
Note that the given Runnable object must remain valid during the entire lifetime of the thread, as only a reference to it is stored internally.
You need to create the runnables in a way that keeps them alive outside the loop also.
As Sami has said, the runnable was destroyed before the thread had finished. I removed the for loops and typed each line out explicitly. I also removed a thread as I can only run 4 threads without causing a crash. Finally, I created a separate runnable for each thread, as in the original code each thread was using the same one.
Poco::Thread thread[5];
class HelloRunnable: public Poco::Runnable
{
public:
HelloRunnable(int arg)
{
n = arg;
}
int n;
virtual void run()
{
//sleep for random length of time
timeval t;
gettimeofday(&t, NULL);
srand(t.tv_usec * t.tv_sec);
int uS = rand()%100000;
usleep(uS);
//print message
std::cout << "Hello, world! This is thread " << n << " I slept for "<< uS << "uS" <<std::endl;
return;
}
};
int main()
{
HelloRunnable runnable1(1);
thread[1].start(runnable1);
HelloRunnable runnable2(2);
thread[2].start(runnable2);
HelloRunnable runnable3(3);
thread[3].start(runnable3);
HelloRunnable runnable4(4);
thread[4].start(runnable4);
thread[1].join();
thread[2].join();
thread[3].join();
thread[4].join();
return 0;
}

Mutex does not work as expected

I have used mutex in inherited classes but seems it does not work as I expected with threads. Please have a look at below code:
#include <iostream>
#include <cstdlib>
#include <pthread.h>
// mutex::lock/unlock
#include <iostream> // std::cout
#include <thread> // std::thread
#include <chrono> // std::thread
#include <mutex> // std::mutex
typedef unsigned int UINT32t;
typedef int INT32t;
using namespace std;
class Abstract {
protected:
std::mutex mtx;
};
class Derived: public Abstract
{
public:
void* write( void* result)
{
UINT32t error[1];
UINT32t data = 34;
INT32t length = 0;
static INT32t counter = 0;
cout << "\t before Locking ..." << " in thread" << endl;
mtx.lock();
//critical section
cout << "\t After Create " << ++ counter << " device in thread" << endl;
std::this_thread::sleep_for(1s);
mtx.unlock();
cout << "\t deallocated " << counter << " device in thread" << endl;
pthread_exit(result);
}
};
void* threadTest1( void* result)
{
Derived dev;
dev.write(nullptr);
}
int main()
{
unsigned char byData[1024] = {0};
ssize_t len;
void *status = 0, *status2 = 0;
int result = 0, result2 = 0;
pthread_t pth, pth2;
pthread_create(&pth, NULL, threadTest1, &result);
pthread_create(&pth2, NULL, threadTest1, &result2);
//wait for all kids to complete
pthread_join(pth, &status);
pthread_join(pth2, &status2);
if (status != 0) {
printf("result : %d\n",result);
} else {
printf("thread failed\n");
}
if (status2 != 0) {
printf("result2 : %d\n",result2);
} else {
printf("thread2 failed\n");
}
return -1;
}
so the result is:
*Four or five arguments expected.
before Locking ... in thread
After Create 1 device in thread
before Locking ... in thread
After Create 2 device in thread
deallocated 2 device in thread
deallocated 2 device in thread
thread failed
thread2 failed
*
So here we can see that second thread comes to critical section before mutex was deallocated.
The string "After Create 2 device in thread" says about that.
If it comes to critical section before mutex is deallocated it means mutex works wrong.
If you have any thoughts please share.
thanks
The mutex itself is (probably) working fine (I'd recommend you to use std::lock_guard though), but both threads create their own Derived object, hence, they don't use the same mutex.
Edit: tkausl's answer is correct -- however, even if you switch to using a global mutex, the output may not change because of the detail in my answer so I'm leaving it here. In other words, there are two reasons why the output may not be what you expect, and you need to fix both.
Note in particular these two lines:
mtx.unlock();
cout << "\t deallocated " << counter << " device in thread" << endl;
You seem to be under the impression that these two lines will be run one right after the other, but there is no guarantee that this will happen in a preemptive multithreading environment. What can happen instead is that right after mtx.unlock() there could be a context switch to the other thread.
In other words, the second thread is waiting for the mutex to unlock, but the first thread isn't printing the "deallocated" message before the second thread preempts it.
The simplest way to get the output you expect would be to swap the order of these two lines.
You shall declare your mutex as a global variable and initiate it before calling pthread_create. You created two threads using pthread_create and both of them create their own mutex so there is absolutely no synchronization between them.

Run methods in new process and wait for them to finish

I need my app to be able to run some methods under a new process, and ideally be able to get a return value from those methods however I have not yet found how I can do this (my C++ knowledge is pretty basic).
So to explain better, let's say I have methods A, A1 and A2. Method A will start executing and at some point it will:
Run method A1 under a new process
Wait for A1 to complete and possibly get return value
Run method A2 under another new process
Wait for A2 to complete and again get return value
Continue running code under original process
I found that I can use fork() to run code in a subprocess, however this does not suit my needs because it seems to be creating a copy of the parent process and not just running the specific code I want only in the new process. Here is an excerpt of what I tried, I'm not sure if it can be modified to do what I want it to or if I should use something else completely:
int main(){
std::cout << "START" << std::endl;
test1();
test2();
std::cout << "FINISH" << std::endl;
return 0;
}
void test1(){
pid_t pid = fork();
if (pid == 0){
int i = 0;
for (; i < 5; ++i) {
std::cout << "Test 1 " << std::endl;
}
}
}
void test2(){
pid_t pid = fork();
if (pid == 0){
int i = 0;
for (; i < 5; ++i) {
std::cout << "Test 2 " << std::endl;
}
}
}
This however results in test2() being executed twice, and FINISH printed 4 times since the parent process is copied to the subprocess.
I am doing this on Linux at the moment, although I'll need to do the same for Windows eventually.
First of all your parent process should wait for the child processes to exit.
Then your child process should exit once they're done, or else the functions will return on both the child and parent processes.
It sound to me like multi-threading might be the best option for you. This way you share the same memory space and can easily get return values. Look into using OpenMP. I think it is by far the easiest way to multi thread. You can launch tasks for each of the function in a parallel block.
int main(){
std::cout << "START" << std::endl;
int ret1, ret2;
#pragma omp parallel
{
#pragma omp task
ret1 = test1();
#pragma omp task
ret2 = test2();
} //blocks at end of parallel block to wait for tasks to finish
std::cout << "FINISH" << std::endl;
return 0;
}
int test1(){
int i = 0;
for (; i < 5; ++i) {
std::cout << "Test 1 " << std::endl;
}
return 0;
}
int test2(){
int i = 0;
for (; i < 5; ++i) {
std::cout << "Test 2 " << std::endl;
}
return 0;
}
I modified the code in my browser so I can not guarantee it compiles but this is how you can launch functions in parallel and get a return value. I do not think forking is the best way to go about it since you would then need some sort of interprocess communication to get data back. Also OpenMP is probably much more efficient. You could also look into using PThreads which is what I think OpenMP uses on the backed but that is more complicated. Also if you are using C++11 look into using std::async(...) which can spawn threads for functions.

Still having race condition with boost::mutex

I am trying an example, which causes race condition to apply the mutex. However, even with the mutex, it still happens. What's wrong? Here is my code:
#include <iostream>
#include <boost/thread.hpp>
#include <vector>
using namespace std;
class Soldier
{
private:
boost::thread m_Thread;
public:
static int count , moneySpent;
static boost::mutex soldierMutex;
Soldier(){}
void start(int cost)
{
m_Thread = boost::thread(&Soldier::process, this,cost);
}
void process(int cost)
{
{
boost::mutex::scoped_lock lock(soldierMutex);
//soldierMutex.lock();
int tmp = count;
++tmp;
count = tmp;
tmp = moneySpent;
tmp += cost;
moneySpent = tmp;
// soldierMutex.unlock();
}
}
void join()
{
m_Thread.join();
}
};
int Soldier::count, Soldier::moneySpent;
boost::mutex Soldier::soldierMutex;
int main()
{
Soldier s1,s2,s3;
s1.start(20);
s2.start(30);
s3.start(40);
s1.join();
s2.join();
s3.join();
for (int i = 0; i < 100; ++i)
{
Soldier s;
s.start(30);
}
cout << "Total soldier: " << Soldier::count << '\n';
cout << "Money spent: " << Soldier::moneySpent << '\n';
}
It looks like you're not waiting for the threads started in the loop to finish. Change the loop to:
for (int i = 0; i < 100; ++i)
{
Soldier s;
s.start(30);
s.join();
}
edit to explain further
The problem you saw was that the values printed out were wrong, so you assumed there was a race condition in the threads. The race in fact was when you printed the values - they were printed while not all the threads had a chance to execute
Based on this and your previous post (were it does not seem you have read all the answers yet). What you are looking for is some form of synchronization point to prevent the main() thread from exiting the application (because when the main thread exits the application all the children thread die).
This is why you call join() all the time to prevent the main() thread from exiting until the thread has exited. As a result of your usage though your loop of threads is not parallel and each thread is run in sequence to completion (so no real point in using the thread).
Note: join() like in Java waits for the thread to complete. It does not start the thread.
A quick look at the boost documentation suggests what you are looking for is a thread group which will allow you to wait for all threads in the group to complete before exiting.
//No compiler so this is untested.
// But it should look something like this.
// Note 2: I have not used boost::threads much.
int main()
{
boost::thread_group group;
boost::ptr_vector<boost::thread> threads;
for(int loop = 0; loop < 100; ++loop)
{
// Create an object.
// With the function to make it start. Store the thread in a vector
threads.push_back(new boost::thread(<Function To Call>));
// Add the thread to the group.
group.add(threads.back());
}
// Make sure main does not exit before all the threads have completed.
group.join_all();
}
If we go back to your example and retrofit your Soldier class:
int main()
{
boost::thread batallion;
// Make all the soldiers part of a group.
// When you start the thread make the thread join the group.
Soldier s1(batallion);
Soldier s2(batallion);
Soldier s3(batallion);
s1.start(20);
s2.start(30);
s3.start(40);
// Create 100 soldiers outside the loo
std::vector<Soldier> lotsOfSoldiers;
lotsOfSoldiers.reserve(100); // to prevent reallocation in the loop.
// Because you are using objects we need to
// prevent copying of them after the thread starts.
for (int i = 0; i < 100; ++i)
{
lotsOfSoldiers.push_back(Solder(batallion));
lotsOfSoldiers.back().start(30);
}
// Print out values while threads are still running
// Note you may get here before any thread.
cout << "Total soldier: " << Soldier::count << '\n';
cout << "Money spent: " << Soldier::moneySpent << '\n';
batallion.join_all();
// Print out values when all threads are finished.
cout << "Total soldier: " << Soldier::count << '\n';
cout << "Money spent: " << Soldier::moneySpent << '\n';
}