Why the synchronization is not happening in multithread application c++ - c++

I am new to multithreading.
When I am running the below program it is giving output as
Function1
Function2
1000...1001
but when I am debugging the program it is giving output as expected.
1000...1001
Function1
Function2
So,I think at directly running time (without debugging) mode it is getting some synchronization problem. But one thing confuses me that, I am using mutex then why synchronization problem is happening?
Please help me.
#include <iostream>
#include <cstdlib>
#include <pthread.h>
using namespace std;
pthread_mutex_t myMutex;
void * function1(void * arg);
void * function2(void * arg);
void * function0(void * arg);
int count = 0;
const int COUNT_DONE = 10;
main()
{
pthread_t thread1, thread2, thread0;
pthread_mutex_init(&myMutex, 0);
pthread_create(&thread0, NULL, &function0, NULL );
pthread_create(&thread1, NULL, &function1, NULL );
pthread_create(&thread2, NULL, &function2, NULL );
pthread_join(thread0, NULL );
pthread_join(thread1, NULL );
pthread_join(thread2, NULL );
pthread_mutex_destroy(&myMutex);
return 0;
}
void *function1(void * arg)
{
cout << "Function1\n";
}
void *function0(void *arg)
{
int i, j;
pthread_mutex_lock(&myMutex);
for (i = 0; i <= 1000; i++)
{
for (j = 0; j <= 1000; j++)
{
}
}
cout << i << "..." << j << endl;
pthread_mutex_unlock(&myMutex);
}
void *function2(void * arg)
{
cout << "Function2\n";
}

"... getting some synchronization problem" where do you see a problem?
The threads output could come in any order, as the threads are not synchronised in any case.
The mutex is used only in one thread.
Also
Function1
1000...1001
Function2
might be an expectable and valid result.
As well as:
1000
Function1
...
Function2
1001
Modifying function1() and function2() like so:
void *function1(void * arg)
{
pthread_mutex_lock(&myMutex);
cout << "Function1\n";
pthread_mutex_unlock(&myMutex);
}
void *function2(void * arg)
{
pthread_mutex_lock(&myMutex);
cout << "Function2\n";
pthread_mutex_unlock(&myMutex);
}
would protect the program from producing the 2nd of my example outputs.

Related

member variable in std::async thread

I am working on a project in which I am using threads. The threads are launched with std::async and I ran into some things with member variables in a class that I do not understand. In the code below there are four different implementations of the thread launching function "start_thread_n". The four different functions give somewhat different results when executed and the output I get when running them is shown in the comment before each of the functions. The difference between the functions is in the call std::async(std::launch::async, &ThreadTest::run, &h) and specifically the param "h". What I would like to get is what start_thread_3 delivers (and _4). The difference in behaviour is what the variable m_a contains at different places in the execution of the code, as can be seen in the print outs to the screen (in the comments before each function).
Can you hint on why I get the different results? I run on Linux with C++14.
Many thanks
#include <future>
#include <thread>
#include <vector>
#include <signal.h>
sig_atomic_t g_stop(false);
std::vector<std::future<void>> my_futures;
std::future<void> g_thread;
class ThreadTest
{
public:
ThreadTest();
void run();
private:
unsigned m_a;
};
ThreadTest::ThreadTest()
{
m_a = 1;
std::cout << m_a << std::endl;
}
void ThreadTest::run()
{
std::cout << "Thread started" << std::endl;
while (not g_stop) {
std::cout << "." << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::cout << std::endl;
std::cout << m_a << std::endl;
std::cout << "Thread stopped" << std::endl;
}
void start_thread_1();
void start_thread_2();
void start_thread_3();
ThreadTest start_thread_4();
void stop_threads();
int main()
{
start_thread_1();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
g_stop = true;
stop_threads();
}
/*
* > ./thread_test
* 1
* Thread started
* ..........
* 0
* Thread stopped
*/
void start_thread_1()
{
ThreadTest h;
g_thread = std::async(std::launch::async, &ThreadTest::run, &h);
my_futures.push_back(std::move(g_thread));
}
/*
* > ./thread_test
* Thread started
* ..........
* fish: “./thread_test” terminated by signal SIGSEGV (Address boundary error)
*/
void start_thread_2()
{
std::shared_ptr<ThreadTest> h;
g_thread = std::async(std::launch::async, &ThreadTest::run, h);
my_futures.push_back(std::move(g_thread));
}
/*
* > ./thread_test
* 1
* Thread started
* ..........
* 1
* Thread stopped
*/
void start_thread_3()
{
ThreadTest h;
g_thread = std::async(std::launch::async, &ThreadTest::run, h);
my_futures.push_back(std::move(g_thread));
}
/*
* > ./thread_test
* 1
* Thread started
* ..........
* 1
* Thread stopped
*/
ThreadTest start_thread_4()
{
ThreadTest h;
g_thread = std::async(std::launch::async, &ThreadTest::run, h);
my_futures.push_back(std::move(g_thread));
return h;
}
void stop_threads()
{
size_t m = my_futures.size();
for(size_t n = 0; n < m; n++) {
auto e = std::move(my_futures.back());
e.get();
my_futures.pop_back();
}
}```

How to make a thread contain a loop using mutexes to control flow?

I am working on a homework assignment and I've hit a wall. The assignment is to use 2 threads with controlling mutex to reduce a number x over a loop of 5 iterations.
The first thread does x=x-5
The second thread does x = x/5
The proper result should reduce x to 5 from 19530 over 5 iterations alternating between thread1 and thread2 at each iteration.
I have the following result as of now:
Thread1: x = 19525
Thread1: x = 19520
Thread1: x = 19515
Thread1: x = 19510
Thread1: x = 19505
From above it is clear that my second thread is not only not doing it's job but it's not doing anything at all.
Below is my code, it is written in C++ but the style is using the tools we learned in class which is how one would do it in C but it should work the same either way.
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
using namespace std;
void *first(void *);
void *second(void *);
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
int x = 19530; // global var that is being manipulated
int i = 1; // counter for the loops
int main() {
int t1, t2;
pthread_t thread1, thread2;
if((t1 = pthread_create( &thread1, NULL, first, NULL))) {
printf("Thread creation failed: %d\n", t2);
}
if((t2 = pthread_create( &thread2, NULL, second, NULL))) {
printf("Thread creation failed: %d\n", t2);
}
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return(0);
}
void *first(void *){ // function for thread1
for(; i <=5; i++){
pthread_mutex_lock(&mymutex);
x = x-5;
cout << "Thread1: x = " << x << endl;
pthread_mutex_unlock(&mymutex);
}
}
void *second(void *){ // function for thread2
for(; i<=5; i++){
pthread_mutex_lock(&mymutex);
x = x/5;
cout << "Thread2: x = " << x << endl;
pthread_mutex_unlock(&mymutex);
}
}
Note I am brand new to the concept threads and mutex. And I'd prefer to stick to the way I have learned in class which I believe is called "the C way".
Thanks to rclgdr who commented above I was able to answer my own question the following code works as intended:
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
using namespace std;
void *first(void *);
void *second(void *);
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // thread1 mutex
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; // thread2 mutex
int x = 19530; // global var that is being manipulated
int main() {
cout << "x = " << x << endl << endl;
int t1, t2;
pthread_t thread1, thread2;
pthread_mutex_lock(&mutex2);
if((t1 = pthread_create( &thread1, NULL, first, NULL))) {
printf("Thread creation failed: %d\n", t2);
}
if((t2 = pthread_create( &thread2, NULL, second, NULL))) {
printf("Thread creation failed: %d\n", t2);
}
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return(0);
}
void *first(void *){ // function for thread1
for(int i = 1; i <=5; i++){
pthread_mutex_lock(&mutex1);
x = x-5;
cout << "Iteration " << i <<endl;
cout << "Thread1: x = " << x << endl;
pthread_mutex_unlock(&mutex2);
}
}
void *second(void *){ // function for thread2
for(int i = 1; i<=5; i++){
pthread_mutex_lock(&mutex2);
x = x/5;
cout << "Thread2: x = " << x << endl << endl;
pthread_mutex_unlock(&mutex1);
}
}

passing a single integer to a multithread function in C++

I want to call a function with multiple threads, and I only need to pass a single integer to that function (the thread id, so if it is accessible I need no value to pass).
How should I do this?
for example like:
for(int i=0; i < numberOfThread; i++ ){
pthread_create(&threads[i], NULL, multichaper, &td[i]);
}
in which multichaper is my function and threadID is an integer.
Update: I marked the answer from user3286661 as the right answer and that worked for me, if you want a more detailed answer you can check my own solution to this question in answers.
General approach to this is to make the function like this:
void* multichaper(void* arg) {
int tid = *(int*)arg;
...
}
And while calling pthread_create:
pthread_create(&threads[i], NULL, multichaper, &td[i])
where td[i] is an int.
You really should consider moving to C++11 threads:
#include <thread>
#include <iostream>
void show_id(int id) {
std::cout << id << std::endl;
}
int main()
{
std::thread t(show_id, 10);
t.join();
}
If you must use pthreads, though:
#include <iostream>
#include <pthread.h>
void *show_id(void *x_void_ptr)
{
const int id = *static_cast<int *>(x_void_ptr);
std::cout << id << std::endl;
return NULL;
}
int main()
{
pthread_t t;
int id = 10;
if(pthread_create(&t, NULL, show_id, &id)) {
std::cerr << "couldn't create" << std::endl;
return -1;
}
if(pthread_join(t, NULL)) {
std::cerr << "couldn't join" << std::endl;
return -2;
}
}
Note how much better the first version is:
No casts
Fewer explicit checks
No problem with the lifetime of the object you're passing - in the first version, you're passing a pointer to it, and thus must ensure it's "alive" while the thread is using it.
No unintuitive void * returns (with the same lifetime problems).
No. You can't do that. The function you pass to pthread_create must have the signature void *(*start_routine) (void *). That is, a function taking a non-const pointer to void and returning a non-const pointer to void.
The simplest way is something like:
int *arg = new int(threadID);
pthread_create(&threads[i], NULL, multichaper, threadID );
and then multichaper looks like:
void *multichaper(void *arg)
{
int *pint = static_cast<int*>(arg);
int threadID = *pint;
delete pint;
...
return nullptr;
}
Note that I have allocated the int on the heap to avoid having to worry about variable lifetimes. If you can guarantee that the variable threadID in the calling function will outlive the thread, then you can skip that bit.
I strongly recommend you use C+11 and the built-in threading library, or if you can't do that, use boost::threads. They both make this much easier!
As i want to pass numbers from 0 to NumberOfThreads to my function i finally used the code below, by passing an integer inside a struct and locking (lock_mutex) that when trying to retrieve the threadNum:
Calling function in multi threads in a member function of SVAnchor class:
pthread_t threads[this->numberOfThread];
pthread_attr_t attr;
params_t params;
pthread_mutex_init (&params.mutex , NULL);
pthread_cond_init (&params.done, NULL);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(int i=0; i < this->numberOfThread; i++ ){
params.id = i;
params.ptr = this;
rc = pthread_create(&threads[i], NULL, &(SVAnchor::multichaperWrapper), &params);
pthread_cond_wait (&params.done, &params.mutex);
}
pthread_attr_destroy(&attr);
void* status;
for(int i=0; i < this->numberOfThread; i++ )
rc = pthread_join(threads[i], &status);
pthread_mutex_destroy (&params.mutex);
pthread_cond_destroy (&params.done);
with params_t as follows:
struct params {
SVAnchor* ptr;
pthread_mutex_t mutex;
pthread_cond_t done;
int id;
};
typedef struct params params_t;
and then multichaperWrapper is as follows:
void* SVAnchor::multichaperWrapper(void* arg){
return (((params*)arg)->ptr)->multichaper( ((params*)arg));
}
and multichaper is as follows:
void* SVAnchor::multichaper( void *threadarg /*0 <= threadNum < numberofthreads*/ ){
int threadNum;
/* Lock. */
pthread_mutex_lock(&(*(params_t*)(threadarg)).mutex);
/* Work. */
threadNum = (*(params_t*)(threadarg)).id;
/* Unlock and signal completion. */
pthread_mutex_unlock(&(*(params_t*)(threadarg)).mutex);
pthread_cond_signal (&(*(params_t*)(threadarg)).done);
cout<<threadNum<<endl;
...
}
If all you want to do is pass an id to the calling thread you can do so by burying it in the void* parameter, and do so portably. Like,
pthread_create(&threads[i], NULL, multichaper, (void*)threadID );

Call join child pthread in main function

I have the test code:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
pthread_t th_worker, th_worker2;
void * worker2(void *data) {
for(int i = 0; i< 1000000; i++){
printf("thread for worker2----%d\n", i);
usleep(500);
}
}
void * worker(void *data){
pthread_create(&th_worker2, NULL, worker2, data);
for(int i = 0; i< 100; i++){
printf("thread for worker-----%d\n", i);
usleep(500);
}
}
void join(pthread_t _th){
pthread_join(_th, NULL);
}
In main() function, If I call join(the_worker2):
int main() {
char* str = "hello thread";
pthread_create(&th_worker, NULL, worker, (void*) str);
/* problem in here */
join(th_worker2);
return 1;
}
--> Segment Fault error
Else, i call:
join(the_worker);
join(th_worker2);
---> OK
Why have segment fault error in above case?
Thanks for help !!!
If you posted all your code, you have a race condition.
main is synchronized with the start of worker but not worker2.
That is, main is trying to join th_worker2 before worker has had a chance to invoke pthread_create and set up th_worker2 with a valid [non-null] value.
So, th_worker2 will be invalid until the second pthread_create completes, but that's already too late for main. It has already fetched th_worker2, which has a NULL value and main will segfault.
When you add the join for th_worker, it works because it guarantees synchronization and no race condition.
To achieve this guarantee without the join, have main do:
int
main()
{
char *str = "hello thread";
pthread_create(&th_worker, NULL, worker, (void *) str);
// give worker enough time to properly start worker2
while (! th_worker2)
usleep(100);
/* problem in here */
join(th_worker2);
return 1;
}
An even better way to do this is to add an extra variable. With this, the first loop is not needed [but I've left it in]:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
int worker_running;
pthread_t th_worker;
int worker2_running;
pthread_t th_worker2;
void *
worker2(void *data)
{
// tell main we're fully functional
worker2_running = 1;
for (int i = 0; i < 1000000; i++) {
printf("thread for worker2----%d\n", i);
usleep(500);
}
return NULL;
}
void *
worker(void *data)
{
// tell main we're fully functional
worker_running = 1;
pthread_create(&th_worker2, NULL, worker2, data);
for (int i = 0; i < 100; i++) {
printf("thread for worker-----%d\n", i);
usleep(500);
}
return NULL;
}
void
join(pthread_t _th)
{
pthread_join(_th, NULL);
}
int
main()
{
char *str = "hello thread";
pthread_create(&th_worker, NULL, worker, (void *) str);
// give worker enough time to properly start worker2
// NOTE: this not necessarily needed as loop below is better
while (! th_worker2)
usleep(100);
// give worker2 enough time to completely start
while (! worker2_running)
usleep(100);
/* problem in here (not anymore!) */
join(th_worker2);
return 1;
}

Embedding matplotlib in C++

I am reading a message from a socket with C++ code and am trying to plot it interactively with matplotlib, but it seems Python code will block the main thread, no matter I use show() or ion() and draw(). ion() and draw() won't block in Python.
Any idea how to plot interactively with matplotlib in C++ code?
An example would be really good.
Thanks a lot.
You may also try creating a new thread that does the call to the
blocking function, so that it does not block IO in your main program
loop. Use an array of thread objects and loop through to find an unused
one, create a thread to do the blocking calls, and have another thread
that joins them when they are completed.
This code is a quick slap-together I did to demonstrate what I mean about
using threads to get pseudo asynchronous behavior for blocking functions...
I have not compiled it or combed over it very well, it is simply to show
you how to accomplish this.
#include <pthread.h>
#include <sys/types.h>
#include <string>
#include <memory.h>
#include <malloc.h>
#define MAX_THREADS 256 // Make this as low as possible!
using namespace std;
pthread_t PTHREAD_NULL;
typedef string someTypeOrStruct;
class MyClass
{
typedef struct
{
int id;
MyClass *obj;
someTypeOrStruct input;
} thread_data;
void draw(); //Undefined in this example
bool getInput(someTypeOrStruct *); //Undefined in this example
int AsyncDraw(MyClass * obj, someTypeOrStruct &input);
static void * Joiner(MyClass * obj);
static void * DoDraw(thread_data *arg);
pthread_t thread[MAX_THREADS], JoinThread;
bool threadRunning[MAX_THREADS], StopJoinThread;
bool exitRequested;
public:
void Main();
};
bool MyClass::getInput(someTypeOrStruct *input)
{
}
void MyClass::Main()
{
exitRequested = false;
pthread_create( &JoinThread, NULL, (void *(*)(void *))MyClass::Joiner, this);
while(!exitRequested)
{
someTypeOrStruct tmpinput;
if(getInput(&tmpinput))
AsyncDraw(this, tmpinput);
}
if(JoinThread != PTHREAD_NULL)
{
StopJoinThread = true;
pthread_join(JoinThread, NULL);
}
}
void *MyClass::DoDraw(thread_data *arg)
{
if(arg == NULL) return NULL;
thread_data *data = (thread_data *) arg;
data->obj->threadRunning[data->id] = true;
// -> Do your draw here <- //
free(arg);
data->obj->threadRunning[data->id] = false; // Let the joinThread know we are done with this handle...
}
int MyClass::AsyncDraw(MyClass *obj, someTypeOrStruct &input)
{
int timeout = 10; // Adjust higher to make it try harder...
while(timeout)
{
for(int i = 0; i < MAX_THREADS; i++)
{
if(thread[i] == PTHREAD_NULL)
{
thread_data *data = (thread_data *)malloc(sizeof(thread_data));
if(data)
{
data->id = i;
data->obj = this;
data->input = input;
pthread_create( &(thread[i]), NULL,(void* (*)(void*))MyClass::DoDraw, (void *)&data);
return 1;
}
return 0;
}
}
timeout--;
}
}
void *MyClass::Joiner(MyClass * obj)
{
obj->StopJoinThread = false;
while(!obj->StopJoinThread)
{
for(int i = 0; i < MAX_THREADS; i++)
if(!obj->threadRunning[i] && obj->thread[i] != PTHREAD_NULL)
{
pthread_join(obj->thread[i], NULL);
obj->thread[i] = PTHREAD_NULL;
}
}
}
int main(int argc, char **argv)
{
MyClass base;
base.Main();
return 0;
}
This way you can continue accepting input while the draw is occurring.
~~Fixed so the above code actually compiles, make sure to add -lpthread