由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教如何保证函数时thread safe的?
相关主题
求教一个今天被面到的多线程的问题career cup 如何测context switch花费时间?
怎么才是 精简,准确呢? spinlock VS semaphore 的 区别??c++ thread 求助 (转载)
请问C++ threading w/ lock free algorithms请问如果用C++实现Thread Safe Queue
failed bloomberg phone interview昨天onsite被问到的 multithreading 题目
问个multi threading code 题,同时请问高手mutil threading 编程有什么好书,网站和教程推荐?上周的几道电面题
two functons and two threadsmulti-threading guru们
谁给讲讲test-and-set怎么实现mutex?又tmd的面砸了一个,还是贴贴面经
攒人品。面试经历(2)thread safe hash table
相关话题的讨论汇总
话题: boost话题: thread话题: lock话题: mutex
进入JobHunting版参与讨论
1 (共1页)
c**z
发帖数: 669
1
如果在里面放一个 member variable, set it to true when the thread starts to
execute, and flip it when the thread returns.
请问这样可以吗
s***0
发帖数: 117
2

to
I don't know how a boolean member variable would help.
The answer is mutexes and locks, you create a member variable mutex.
Here's some code, take out the lock and see what happens. I have a simple
cout here, but imagine you are doing some complex data operations within the
object.
#include
#include
class ThreadSafeClass
{
public:
ThreadSafeClass(int id): mThreadId(id){};
void operator()()
{
// This function will be altering the state, so grab the lock here
boost::lock_guard lock(mMutex);
std::cout << "Try this with and without the lock" << i<< std::endl;
} // Leave the function, pass out of scope and release the lock,
private:
boost::mutex mMutex;
int mThreadId;
};
int main()
{
std::vector vectorThreads;
std::vector::iterator it;
for (int i = 0; i < 10; ++i)
{
ThreadSafeClass tsEx(i);
boost::thread exThread(tsEx); // This launches the threads.
vectorThreads.push_back(exThread);
}
for (it =vectorThreads.begin(); it!=vectorThreads.end(); ++it)
{
(*it)->join();
}
return 0;
}

【在 c**z 的大作中提到】
: 如果在里面放一个 member variable, set it to true when the thread starts to
: execute, and flip it when the thread returns.
: 请问这样可以吗

x****5
发帖数: 293
3
yes if the set and flip operation is atomic.
but that is no difference from using a spin lock - which is implemented with
atomic operation inside
c**z
发帖数: 669
4
I copy and paste the code into and got the following error message. Does
anyone know what's going on?
1> c:\users\lyn\downloads\boost_1_51_0\boost_1_51_0\boost\thread\
win32\mutex.hpp(26) : see declaration of 'boost::mutex::mutex'
1> c:\users\lyn\downloads\boost_1_51_0\boost_1_51_0\boost\thread\
win32\mutex.hpp(22) : see declaration of 'boost::mutex'
1> This diagnostic occurred in the compiler generated function '
ThreadSafeClass::ThreadSafeClass(ThreadSafeClass &)'
1 (共1页)
进入JobHunting版参与讨论
相关主题
thread safe hash table问个multi threading code 题,同时请问高手mutil threading 编程有什么好书,网站和教程推荐?
发个n个月前的面经 - ms bing 悲剧two functons and two threads
贡献T家新鲜面经,求个bless谁给讲讲test-and-set怎么实现mutex?
一个multithread的小问题攒人品。面试经历(2)
求教一个今天被面到的多线程的问题career cup 如何测context switch花费时间?
怎么才是 精简,准确呢? spinlock VS semaphore 的 区别??c++ thread 求助 (转载)
请问C++ threading w/ lock free algorithms请问如果用C++实现Thread Safe Queue
failed bloomberg phone interview昨天onsite被问到的 multithreading 题目
相关话题的讨论汇总
话题: boost话题: thread话题: lock话题: mutex