gpt4 book ai didi

c++ - 让我的程序保持活力的神奇cout

转载 作者:太空宇宙 更新时间:2023-11-04 04:49:30 25 4
gpt4 key购买 nike

我写了一段代码来实现自旋锁和互斥锁。
有一个有趣的但是。神奇的 cout 可以让我的程序保持活力。如果我删除 cout,我的程序将永远休眠。 (这只发生在 Linux 中。Windows 表现良好)
有人知道吗?

#include <pthread.h>
#include <iostream>
#include <queue>
#include <sys/time.h>
#include <stdexcept>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define Tcount 10
#define TheLock MutexLock

static inline int TAS(volatile int * ptr) {
unsigned long result;
asm volatile("lock;"
"xchgl %0, %1;"
: "=r"(result), "=m"(*ptr)
: "0"(1), "m"(*ptr)
: "memory");
return result;
}




class SpinLock {
private:
int lock;
pthread_t owner;
public:

SpinLock() {
lock = 0;
}

void getLock() {
while (TAS(&lock) == 1) {

}

owner = pthread_self();

}

void releaseLock() {
if (lock == 0) {
cout << "Spin no lock" << endl;
return;
} else if (owner == pthread_self()) {
owner = NULL;
lock = 0;
} else {
throw runtime_error("Spin can't release");
}
}


};

class MutexLock {
private:
int lock;
pthread_t owner;
queue<pthread_t> q;
SpinLock qLock;
public:

MutexLock() {
lock = 0;
}

void getLock(int id) {
pthread_t self = pthread_self();
cout<<"a"<<endl;// magic cout

if (TAS(&lock) == 0) {
owner = self;
return;
}
qLock.getLock();
q.push(self);
qLock.releaseLock();

while (owner != self) {
}

}

void releaseLock(int id) {
if (lock == 0) {
cout << "Mutex no lock" << endl;
return;
} else if (owner == pthread_self()) {
qLock.getLock();
if (q.empty()) {
owner = NULL;
lock = 0;
} else {
owner = q.front();
q.pop();
}
qLock.releaseLock();
} else {
throw runtime_error("Mutex can't release");
}
}
};

TheLock lock;
int g = 0;
void* run(void* pt) {

int id = (int) pt;
for (int i = 0; i < 10000; i++) {

lock.getLock(id);
//cout<<"Thread "<<id<<" get lock, g="<<g<<endl;
int next = g + 1;
g = next;
//cout<<"Thread "<<id<<" release lock, g="<<g<<endl;
lock.releaseLock(id);


}

return NULL;

}

int main() {

pthread_t th[Tcount];

long mtime, seconds, useconds;
struct timeval start, end;
gettimeofday(&start, NULL);

for (int i = 0; i < Tcount; i++) {
pthread_create(&th[i], NULL, run, (void*) (i+10));
}
for (int i = 0; i < Tcount; i++) {
pthread_join(th[i], 0);
}
gettimeofday(&end, NULL);

seconds = end.tv_sec - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;

mtime = ((seconds) * 1000000 + useconds);

cout << "g=" << g << endl;
cout << "time=" << mtime << endl;



return 0;
}

最佳答案

您无法使用 volatile 来实现互斥锁关键字,因为操作可能不是原子的。这意味着操作系统可能会在操作完成之前切换到不同的线程。

对于互斥体,您必须使用操作系统。这是唯一知道线程何时切换的东西。

关于c++ - 让我的程序保持活力的神奇cout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9448059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com