提升线程问题,当一个线程与另一个线程没有相同的副本时如何共享变量?
Boost thread issue, how to share variable when one thread doesn't have the same copy as another?
这可能是
一个愚蠢的问题,但我确实在互联网上搜索了变量,并找到了与互斥体和赛车条件、锁等相关的所有内容;但似乎没有什么能解决这个简单的问题。
基本上下面的代码创建两个线程,在每个线程中更改变量shared_int
以表示使用它的线程。线程单独运行,类本身似乎在两个不同的线程中shared_int
具有同一变量的两个实例?我遇到的问题是我希望这个变量在任一线程中被更改并且也是可读的,但我也希望从一个线程中看到的shared_int
值在第二个线程中相同。这是代码
#include <boost/thread.hpp>
template <typename I>
class threaded
{
private:
I volatile shared_int;
public:
threaded();
virtual ~threaded();
bool inputAvailable();
void thread_1();
void thread_2();
};
template <typename I>
threaded<I>::threaded(){}
template <typename I>
threaded<I>::~threaded(){}
template <typename I>
bool threaded<I>::inputAvailable()
{
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv);
return (FD_ISSET(0, &fds));
}
template <typename I>
void threaded<I>::thread_1()
{
shared_int = 1;
while(!inputAvailable())
{
std::cout<<"threaded::thread_1 shared_int "<<this->shared_int<<std::endl;
boost::this_thread::sleep_for( boost::chrono::milliseconds{ 9000});
};
}
template <typename I>
void threaded<I>::thread_2()
{
shared_int = 2;
while(!inputAvailable())
{
std::cout<<"threaded::thread_2 shared_int "<<this->shared_int<<std::endl;
boost::this_thread::sleep_for( boost::chrono::milliseconds{ 10000});
};
}
int main()
{
boost::thread_group thread;
threaded< int> threads;
thread.add_thread( new boost::thread( boost::bind( &threaded<int>::thread_1, threads)));
thread.add_thread( new boost::thread( boost::bind( &threaded<int>::thread_2, threads)));
thread.join_all();
return 0;
}
下面的代码似乎确实解决了这个问题。它使用boost::shared_ptr
但也适用于常规指针;无论哪种方式!我仍然想要一个按值而不是指针传递对象的解决方案,但指针现在有效。
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
template <typename I>
class threaded
{
private:
I volatile shared_int;
public:
threaded();
virtual ~threaded();
bool inputAvailable();
void thread_1();
void thread_2();
};
template <typename I>
threaded<I>::threaded(){}
template <typename I>
threaded<I>::~threaded(){}
template <typename I>
bool threaded<I>::inputAvailable()
{
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv);
return (FD_ISSET(0, &fds));
}
template <typename I>
void threaded<I>::thread_1()
{
shared_int = 1;
while(!inputAvailable())
{
std::cout<<"threaded::thread_1 shared_int "<<this->shared_int<<std::endl;
boost::this_thread::sleep_for( boost::chrono::milliseconds{ 9000});
};
}
template <typename I>
void threaded<I>::thread_2()
{
shared_int = 2;
while(!inputAvailable())
{
std::cout<<"threaded::thread_2 shared_int "<<this->shared_int<<std::endl;
boost::this_thread::sleep_for( boost::chrono::milliseconds{ 10000});
};
}
int main()
{
boost::thread_group thread;
boost::shared_ptr< threaded <int> > threads{ new threaded <int>};
thread.add_thread( new boost::thread( boost::bind( &threaded<int>::thread_1, threads)));
thread.add_thread( new boost::thread( boost::bind( &threaded<int>::thread_2, threads)));
thread.join_all();
threads.reset();
return 0;
}
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- 是否可以在并行区域中为共享 2D 数组创建选定元素的线程本地副本?(共享,私有,障碍:OPenMP)
- 使用 lambda 表达式创建线程时,如何为每个线程提供自己的 lambda 表达式副本
- 提升线程问题,当一个线程与另一个线程没有相同的副本时如何共享变量?
- 免疫表中副本的设置是否安全,避免线程损坏
- C++-初始化由不同线程拥有的对象的两个副本
- 复制/修改STL容器的副本是线程安全的