I know that this code is correct (except the delete not done) :
#include <thread>
#include <atomic>
#include <cassert>
#include <string>
std::atomic<std::string*> ptr;
int data;
void producer()
{
std::string* p = new std::string("Hello");
data = 42;
ptr.store(p, std::memory_order_release);
}
void consumer()
{
std::string* p2;
while (!(p2 = ptr.load(std::memory_order_acquire)))
;
assert(*p2 == "Hello"); // never fires
assert(data == 42); // never fires
}
int main()
{
std::thread t1(producer);
std::thread t2(consumer);
t1.join(); t2.join();
}
However, I wonder why in the consumer thread data can not be a stale data. Is it because of the acquire operation?
Assignment to data happens-before ptr.store call. Accessing data happens-after that call (yes, by way of synchronization with the atomic object). Therefore, the access is guaranteed to see the value previously assigned.
Related
I'm very confused with std::thread and std::async. Here is my code:
#include <vector>
#include <thread>
#include <mutex>
std::vector<int> myvector = {};
std::mutex mu;
int result;
void samplefunc(int a, int& result){
result = 2 * a;
}
void func1(){
while(true){
std::unique_lock locker(mu);
locker.lock();
std::vector<int> temp = myvector;
locker.unlock();
if(myvector.size() > 0){
samplefunc(myvector.at(0),result);
myvector.clear();
}else{
samplefunc(0,result);
}
}
}
void func2(){
while(true){
//do complex things, suppose after complex calculations we have result dummy which is type int
int dummy = 0;
std::unique_lock locker(mu);
locker.lock();
myvector.push_back(dummy);
locker.unlock();
}
}
int main(){
std::thread t1(func1);
std::thread t2(func2);
t1.join();
t2.join();
}
What I want to do is very simple. We have two threads which should run in parallel (i.e. they don't have to wait on each other). However, thread t1 is supposed to change its behaviour if thread t2 puts some integer in a shared vector. Does the code given above achieve this, or should we use std::async, std::future, etc?
If the shared data is properly synchronized, it is fine for modification from one thread on the shared data to affect computation using that data in separate thread.
But you haven't locked myvector sufficiently. The lock has to surround all reading steps on the shared data. In func1, you release the lock too soon.
I am fairly new to C++ and very new to using mutex. I am trying to implement a thread safe queue by #ChewOnThis_Trident from this answer.
Essentially I have different threads adding messages to a queue and I need to preserve the order they are being added. However the messages require some conditional modifications before being added. In the real code listeners on separate threads call unique "handleMessage" functions that modify the message before adding to them to the queue. A separate thread checks to see if messages are in the queue and handles them in order. In the full code, I know the listeners are receiving the messages in the correct order, but they are failing to add them to the queue in the correct order.
I think the problem is there is some time elapsing between when a message is received and if it is being modified, causing messages to fall out of order.
For practical reasons in the real code, I can't do these modifications inside of "Safequeue::enqueue".
In my example two threads can add to the queue. One thread reads from it. The "message" in this case is a random int. "UsesQ" handles adding to the queue, and message modification (Ex. makes all ints odd).
I think another mutex is needed when "UsesQ::addQ" is called, but it would need to be shared across all the threads and I'm not sure if I am not sure how to implement it.
In the example I am struggling of thinking of a way to test if the order is correct.
Here is the example:
#include <queue>
#include <mutex>
#include <condition_variable>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <assert.h>
#include <pthread.h>
#include <unistd.h>
class SafeQueue
{// A threadsafe-queue.
public:
SafeQueue(void)
: q()
, m()
, cv()
{}
~SafeQueue(void)
{}
// Add an element to the queue.
void enqueue(int i)
{
std::lock_guard<std::mutex> lock(m);
q.push(i);
cv.notify_one();
}
// Get the "front"-element.
// If the queue is empty, wait till a element is avaiable.
int dequeue(void)
{
std::unique_lock<std::mutex> lock(m);
while(q.empty())
{
// release lock as long as the wait and reaquire it afterwards.
cv.wait(lock);
}
int val = q.front();
q.pop();
return val;
}
private:
std::queue<int> q;
mutable std::mutex m;
std::condition_variable cv;
};
class UsesQ
{
private:
int readVal;
int lastReadVal = 1;
public:
SafeQueue & Q;
UsesQ(SafeQueue & Q): Q(Q){};
~UsesQ(){};
void addQ(int i)
{
if(i% 2 == 0)
{
i++;//some conditional modification to the initial "message"
}
Q.enqueue(i);
}
void removeQ()
{
readVal = Q.dequeue();
}
};
void* run_add(void* Ptr)
{
UsesQ * UsesQPtr = (UsesQ *)Ptr;
for(;;)
{
int i = rand();//simulate an incoming "message"
UsesQPtr->addQ(i);
}
pthread_exit (NULL);
return NULL;
}
void* run_remove(void* Ptr)
{
UsesQ * UsesQPtr = (UsesQ *)Ptr;
for(;;)
{
UsesQPtr->removeQ();
}
pthread_exit (NULL);
return NULL;
}
int main()
{
SafeQueue Q;
UsesQ * UsesQPtr = new UsesQ(std::ref(Q));
pthread_t thread1;
pthread_create(&thread1, NULL, run_add, UsesQPtr);
pthread_t thread2;
pthread_create(&thread2, NULL, run_add, UsesQPtr);
pthread_t thread3;
pthread_create(&thread3, NULL, run_remove, UsesQPtr);
while(1)
{
usleep(1);
printf(".\n");
}
};
Complied with the pthread tag
g++ main.cpp -pthread
Thank you for any help.
Is it good or bad to create a thread in function object constructor by passing the dereferenced this pointer by reference to the thread object?
Is there any problem in below code?
Any improvement can be made on it to reach below objective?
The objective is to gracefully end the thread when the class object is out of scope.
#include <iostream>
#include <chrono>
#include <future>
#include <thread>
class MyThread {
private:
std::atomic<bool> exit;
std::thread t;
public:
MyThread() : exit(false) {
t = std::thread(std::ref(*this));
}
~MyThread() {
exit.store(true, std::memory_order_relaxed);
if (t.joinable()) {
t.join();
}
}
void operator()() {
while (!exit.load(std::memory_order_relaxed)) {
std::cout << "."; // some more meaningful work here
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
};
int main() {
MyThread t;
std::cin.get();
return 0;
}
It might work occasionally, but it's unsafe. It potentially generates a race condition, because you start the thread on an object that hasn't finished its construction yet, resulting in undefined behavior.
I wrote this sample program to mimic what I'm trying to do in a larger program.
I have some data that will come from the user and be passed into a thread for some processing. I am using mutexes around the data the flags to signal when there is data.
Using the lambda expression, is a pointer to *this send to the thread? I seem to be getting the behavior I expect in the cout statement.
Are the mutexes used properly around the data?
Is putting the atomics and mutexes as a private member of the class a good move?
foo.h
#pragma once
#include <atomic>
#include <thread>
#include <vector>
#include <mutex>
class Foo
{
public:
Foo();
~Foo();
void StartThread();
void StopThread();
void SendData();
private:
std::atomic<bool> dataFlag;
std::atomic<bool> runBar;
void bar();
std::thread t1;
std::vector<int> data;
std::mutex mx;
};
foo.c
#include "FooClass.h"
#include <thread>
#include <string>
#include <iostream>
Foo::Foo()
{
dataFlag = false;
}
Foo::~Foo()
{
StopThread();
}
void Foo::StartThread()
{
runBar = true;
t1 = std::thread([=] {bar(); });
return;
}
void Foo::StopThread()
{
runBar = false;
if(t1.joinable())
t1.join();
return;
}
void Foo::SendData()
{
mx.lock();
for (int i = 0; i < 5; ++i) {
data.push_back(i);
}
mx.unlock();
dataFlag = true;
}
void Foo::bar()
{
while (runBar)
{
if(dataFlag)
{
mx.lock();
for(auto it = data.begin(); it < data.end(); ++it)
{
std::cout << *it << '\n';
}
mx.unlock();
dataFlag = false;
}
}
}
main.cpp
#include "FooClass.h"
#include <iostream>
#include <string>
int main()
{
Foo foo1;
std::cout << "Type anything to end thread" << std::endl;
foo1.StartThread();
foo1.SendData();
// type something to end threads
char a;
std::cin >> a;
foo1.StopThread();
return 0;
}
You ensure that the thread is joined using RAII techniques? Check.
All data access/modification is either protected through atomics or mutexs? Check.
Mutex locking uses std::lock_guard? Nope. Using std::lock_guard wraps your lock() and unlock() calls with RAII. This ensures that even if an exception occurs while within the lock, that the lock is released.
Is putting the atomics and mutexes as a private member of the class a good move?
Its neither good nor bad, but in this scenario, where Foo is a wrapper for a std::thread that does work and controls the synchronization, it makes sense.
Using the lambda expression, is a pointer to *this send to the thread?
Yes, you can also do t1 = std::thread([this]{bar();}); to make it more explicit.
As it stands, with your dataFlag assignments after the locks, you may encounter problems. If you call SendData twice such that bar processes the first one but is halted before setting dataFlag = false so that the second call adds the data, sets the flag to true only to have bar set it back to false. Then, you'll have data that has been "sent" but bar doesn't think there's anything to process.
There may be other tricky situations, but this was just one example; moving it into the lock clears up that problem.
for example, your SendData should look like:
void Foo::SendData()
{
std::lock_guard<std::mutex> guard(mx);
for (int i = 0; i < 5; ++i) {
data.push_back(i);
}
dataFlag = true;
}
I want print a permutation of {0, 1, 2, 3} set by a multithread program written in C++11.
The source code is this:
#include <iostream>
#include <stdio.h>
#include <thread>
#include <vector>
#include <chrono>
using namespace std;
void func(int index);
int main()
{
vector<thread> threads;
for (int i = 0; i < 4; i++)
{
auto var = [&]()
{
return func(i);
};
threads.push_back(thread(var));
}
for (auto& thread : threads)
thread.join();
}
void func(int index)
{
cout << index;
for (int i = 0; i < 10000; i++);
}
I expect in output a permutation of 0123, but I receive weird results, like these:
0223
0133
0124
I don't understand this weird behaviour, in particualar I cannot explain the presence of number 4.
Probably this is a beginner's mistake, I thanks anyway everybody will help me.
You are capturing i by reference:
auto var = [&]()
{
return func(i);
};
As such, when the thread that gets eventually started gets kicked off and going, it does not have an actual copy, the value of i that it has at this moment, but it has only a reference to i.
Which was probably incremented, once or twice now. If you consider that a reference is really just an ordinary pointer with a thin layer of makeup on top of it, you should be able to figure this out on your own. The thread gets a pointer to i, which, who knows how many times it could've been incremented, by the time the thread gets going.
And, technically, since i could've even gone out of scope here, if the for loop terminated before the thread started executing, this is undefined behavior.
You're invoking undefined behaviors in three ways:
Firstly, you're capturing the value of a stack variable by reference rather than by value, so when the thread starts it will call the lambda and use the then-current value of i rather than the value at the time of capture.
[edit: No-longer true as of C++11] Second is the thread-safety of cout.
The third is an assumption of the order in which the threads execute which is not guaranteed. [edit:] This includes not only the order in which they start but in which they access cout to write their output.
But do you need to solve the order of execution?
If you do, then instead of passing the values to the threads, put them into a queue and give the threads access to the queue.
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
#include <chrono>
#include <queue>
class LockedQueue {
std::queue<int> queue_;
mutable std::mutex mutex_;
public:
LockedQueue() = default;
// these don't have to be deleted, but you'd have to decide whether or
// not each operation needed to invoke a lock, and in the case of operator=
// you have two mutexes to contend with.
LockedQueue(const LockedQueue&) = delete;
LockedQueue(LockedQueue&&) = delete;
LockedQueue& operator=(const LockedQueue&) = delete;
LockedQueue& operator=(LockedQueue&&) = delete;
void push(int value) {
std::lock_guard<std::mutex> lock(mutex_);
queue_.push(value);
}
int pop() {
std::lock_guard<std::mutex> lock(mutex_);
int value = queue_.front();
queue_.pop();
return value;
}
bool empty() const {
std::lock_guard<std::mutex> lock(mutex_);
return queue_.empty();
}
};
void func(LockedQueue& work, LockedQueue& results);
int main()
{
LockedQueue work, results;
std::vector<std::thread> threads;
for (int i = 0; i < 4; i++)
{
work.push(i);
threads.emplace_back(func, std::ref(work), std::ref(results));
}
for (auto& thread : threads)
thread.join();
while (!results.empty()) {
int i = results.pop();
std::cout << i;
}
}
void func(LockedQueue& work, LockedQueue& results)
{
int index = work.pop();
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
results.push(index);
}
http://ideone.com/7G0JEO
We are still not guaranteed to get our results back in-order: it's quite possible that the thread that takes 0 off the queue is then pre-empted and doesn't execute again until 1, 2 and 3 have had their results pushed onto the result queue.
As Sam Varshavchik mentioned about the undefined behavior of the i when it when it goes out of scope, i suggest to join each thread created inside the loop where i exists by adding this :
threads[i].join();
And don't forget to remove the :
for (auto& thread : threads)
thread.join();
Your main function should be like this :
int main()
{
vector<thread> threads;
for (int i = 0; i < 4; i++)
{
auto var = [&]()
{
return func(i);
};
threads.push_back(thread(var));
threads[i].join(); // joining the thread after its creation.
}
system("pause");
return 0;
}
Amrane Abdelkader.