这是一个面试问题,面试已经完成.
如何在不使用互斥锁,半变量,spinLock和futex的情况下进行线程同步?
给定5个线程,如何使4个线程在同一点等待来自左线程的信号?这意味着当所有线程(1,2,3,4)在其线程函数中的某个点执行时,它们会停止并等待来自线程5的信号发送信号,否则它们将不会继续.
我的想法:
使用全局bool变量作为标志,如果线程5未将其设置为true,则所有其他线程在一个点等待,并将其标志变量设置为true.在线程5发现所有线程的标志变量都为真之后,它将设置标志var true.
这是一个忙碌的等待.
有更好的想法吗?
谢谢
the pseudo code:
bool globalflag = false;
bool a[10] = {false} ;
int main()
{
for (int i = 0 ; i < 10; i++)
pthread_create( threadfunc, i ) ;
while(1)
{
bool b = true;
for (int i = 0 ; i < 10 ; i++)
{
b = a[i] & b ;
}
if (b) break;
}
}
void threadfunc(i)
{
a[i] = true;
while(!globalflag);
}
Run Code Online (Sandbox Code Playgroud) 是否可以使用互斥锁来锁定向量中的元素而不是整个向量?
例如,给定一个向量 myVec;将 10 个元素推回 myVec
for (int i = 0; i < 10; ++i)
{
buffer myBuf = i; // actually myBuf is not necessarily int.
myVec.push_back(myBuf);
}
Run Code Online (Sandbox Code Playgroud)
向量的每个元素将被多个线程异步更改。如何使用互斥锁仅锁定 myVec 中的一个缓冲区,以便一个线程可以写入或读取元素;另一个可以同时读写另一个元素吗?
谢谢
这个功能的目的是什么?
bool whatIsIt(double n)
{
return n == n;
}
Run Code Online (Sandbox Code Playgroud)
它可以用来检查n中的每一位?
我不信 .
任何评论都表示赞赏.
我试图在Linux上比较boost :: atomic和pthread mutex的性能:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER ;
int g = 0 ;
void f()
{
pthread_mutex_lock(&mutex);
++g;
pthread_mutex_unlock(&mutex);
return ;
}
const int threadnum = 100;
int main()
{
boost::threadpool::fifo_pool tp(threadnum);
for (int j = 0 ; j < 100 ; ++j)
{
for (int i = 0 ; i < threadnum ; ++i)
tp.schedule(boost::bind(f));
tp.wait();
}
std::cout << g << std::endl ;
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
是时候了:
real 0m0.308s
user 0m0.176s
sys 0m0.324s
Run Code Online (Sandbox Code Playgroud)
我也试过boost :: atomic:
boost::atomic<int> …
Run Code Online (Sandbox Code Playgroud) 我正在研究按位操作.
但是,我不明白为什么他们声称代码是并行运行的.
并行计算奇偶校验
unsigned int v; // word value to compute the parity of
v ^= v >> 16;
v ^= v >> 8;
v ^= v >> 4;
v &= 0xf;
return (0x6996 >> v) & 1;
Run Code Online (Sandbox Code Playgroud)
我从http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel复制了代码
任何帮助将不胜感激.
该函数无法初始化数组,因为sizeof()
返回的字节数int pointer
不是内存所指向的大小myArray
.
void assignArray(int *myArray)
{
for(int k = 0; k < sizeof(myArray); ++k)
{
myArray[k] = k;
}
}
Run Code Online (Sandbox Code Playgroud)
还有其他问题吗?
谢谢
我的C++程序有一个错误:
#include<iostream>
using namespace std;
char* foo()
{
char * mystr = new char[6];
mystr = "Hello";
return mystr ;
}
int main()
{
char* myString =foo();
printf("%s \n", myString);
delete [] myString ;
}
==27472== Invalid free() / delete / delete[]
==27472== at 0x4A07A12: operator delete[](void*) (vg_replace_malloc.c:409)
==27472== by 0x4007EB: main (printHello.cpp:16)
==27472== Address 0x4008f8 is not stack'd, malloc'd or (recently) free'd
Run Code Online (Sandbox Code Playgroud)
如果我删除delete [] myString ;
它运作良好.
我应该释放堆上分配的内存,对吧?
但是,如果我删除它,为什么我会收到错误.
谢谢
这是一个面试问题.
参考示例代码,需要覆盖哪个运算符才能使用
std::set<Value>
#include<iostream>
class Value
{
std::string s_val;
int i_val;
public:
Value(std::string s, int i): s_val(s) , i_val(i){}
};
// EOF
/*
a operator !=
b operator >
c operator <=
d operator >=
e operator <
*/
Run Code Online (Sandbox Code Playgroud)
实际上,我不明白为什么需要在这里覆盖运营商."set"不允许重复的元素,也许运算符!=需要被覆盖?