我了解到信号量可以充当原子锁,可以执行两个功能:down和up。
有没有办法以value原子方式交换两个指针,避免竞争条件和死锁。
我首先想出了“解决方案”,假设两个指针都有:
Item a = { value = "A", lock = Semaphore(1) }
Item b = { value = "B", lock = Semaphore(1) }
void atomic_swap(Item* a, Item* b) {
a->lock.down(); // acquire
b->lock.down(); // acquire
non_atomic_swap(&a.value, &b.value);
b->lock.up(); // release
a->lock.up(); // release
}
Run Code Online (Sandbox Code Playgroud)
atomic_swap但如果我没记错的话,如果使用相同的指针调用两个,则会导致死锁:例如。
Item a = ...;
Item b = ...;
thread_start(atomic_swap, {&a, &b}); // start a thread running atomic_swap(&a, &b);
thread_start(atomic_swap, {&b, &a}); // start a thread …Run Code Online (Sandbox Code Playgroud) 有没有像国际象棋计时器一样工作的电话机,意思是;
线程A完成其任务,循环回到顶部并调用信号量
这会触发线程2继续执行其代码,循环回到顶部并调用信号量
这会触发线程A ......
所以信号量既阻塞又信令.
我知道我可以使用两个事件和WaitForSingleObject,但我想知道是否有一个信号量专门做这个?
在这个主题上,一个事件的"昂贵"程度如何,在内存和CPU方面,WaitForSingleObject()的"代价"是多么"昂贵"?
作为求职面试的问题,使用C#实现信号量的最佳和最简单的方法是什么?
请添加您的代码
谢谢!
*不使用带有Win32 API的semphores,我的意思是自己实现它.
我需要解析类似于XML的大文本.因为它不在内存中的文本(我有一个StreamReader对象)将该流放在内存中是我花费最多时间的地方.所以在一个线程上我将该流放入一个数组(内存).我有另一个处理该数组的线程.但我有很奇怪的行为.例如,看一下这个图像:

请注意,listToProcess[counter] = buffer 现在应该listToProcess[10] = buffer注意调试器说明listToProcess[10]=null为什么!?.另一个线程只读取它不修改它们的项目.起初我以为可能另一个线程正在使该item = null但事实并非如此.为什么我会遇到这种行为?
如果你想在这里看到我的代码,它是:
Semaphore sem = new Semaphore(0, 1000000);
bool w;
bool done = false;
// this task is responsible for parsing text created by main thread. Main thread
// reads text from the stream and places chunks in listToProces[]
var task1 = Task.Factory.StartNew(() =>
{
sem.WaitOne(); // wait so there are items on list (listToProcess) to work with
// counter to identify which chunk of char[] in …Run Code Online (Sandbox Code Playgroud) 你知道为什么这个C代码以"分段错误"结束吗?
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define NAMESEM "/mysem"
int main(int argc, char* argv) {
sem_t* sem;
int fd = shm_open(NAMESEM, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0);
ftruncate(fd, sizeof(sem_t));
sem = mmap(NULL, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
sem_init(sem, 1, 0);
sem_wait(sem);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已经关注了这里发现的所有相关内容,但似乎sem_init()生成了一个分段错误,我不知道为什么.我用指针犯了一些错误吗?
在我的应用程序中,我通过选择器打开相机,拍完照片后,我想通过以下方法保护资产库.调用writeImageToSavedPhotosAlbum后,该方法会冻结.
没有信号量,方法可以完美地工作.但是我没想到会收到assetURL.
+ (NSURL*)safeImageToAssetsLibrary:(UIImage *)image metadata:(NSDictionary *)metadata
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block NSURL *retAssestURL = nil;
dispatch_semaphore_t semaWaitingForSafeImage = dispatch_semaphore_create(0);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// safe the image to the assests library
NSLog(@"Safe image to asssets library...");
dispatch_async(queue, ^{
[library writeImageToSavedPhotosAlbum:image.CGImage metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"Image could not be safed to the assets library: %@", error);
retAssestURL = nil;
}
else {
NSLog( @"Image safed successfully to assetURL: %@", assetURL);
retAssestURL …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个线程池.我在线找到了几个例子,但他们在SyncObjs库中使用了TSemaphore.
我正在使用Delphi 6,而我的SyncObjs不包括TSemaphore.我环顾网络,找不到任何源代码.
是否有适用于包含TSemaphore的Delphi 6的库?
我试图使用二进制信号量实现一个多线程程序.这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
int g = 0;
sem_t *semaphore;
void *myThreadFun(void *vargp)
{
int myid = (int)vargp;
static int s = 0;
sem_wait(semaphore);
++s; ++g;
printf("Thread ID: %d, Static: %d, Global: %d\n", myid, s, g);
fflush(stdout);
sem_post(semaphore);
pthread_exit(0);
}
int main()
{
int i;
pthread_t tid;
if ((semaphore = sem_open("/semaphore", O_CREAT, 0644, 3))==SEM_FAILED) {
printf("semaphore initialization failed\n");
}
for (i = 0; i < 3; i++) {
pthread_create(&tid, NULL, myThreadFun, (void *)i); …Run Code Online (Sandbox Code Playgroud) 我正在使用函数处理文件,并启动我的线程,如下所示:
for my $file (@files){
$threads[$k] = threads->create('function', $file);
$k++;
}
Run Code Online (Sandbox Code Playgroud)
我想限制并行进程的数量.怎么做的?我查看了很多Semaphore/Queue示例,但找不到任何简单的东西.
有什么想法我可以简单地限制线程数吗?
我的书说的答案是x。
但是那怎么可能呢?我只是从计数信号量和二进制信号量之间的差异中学到的,计数信号量具有正值,因此多个进程可以访问关键部分。因此,在那种情况下-怎么可以说x个进程正在等待,因为到达0时,下一个等待信号将忙于等待一个进程,并且信号量永远不能小于0。
现在,我认为可能还有第二种情况。就像将计数信号量初始化为1一样。现在,当一个进程访问它时,它变为0。
while(s <= 0);
Run Code Online (Sandbox Code Playgroud)
然后下一个过程将其设为-1。因此,单个进程等待使信号量值为-1。
因此,我可以得出结论,对于-x,x个进程正在等待!
有人可以澄清我是对还是错?任何帮助表示赞赏。提前致谢。