4
Vote

No Thread Safe for CompressStream method (v0.64)

description

Hi,
 
I found still no Thread Safe for CompressStream method for version 0.64, below is my code:
 
using (MemoryStream outStream = new MemoryStream())
{
            SevenZip.SevenZipCompressor szc = new SevenZip.SevenZipCompressor();
            szc.CompressionLevel = SevenZip.CompressionLevel.High;
            szc.CompressStream(inStream, outStream, password);
 
            return outStream.ToArray();
}
 
Please help!
 
Thank you very much.

comments

mproepst wrote May 20, 2011 at 1:37 PM

Hi,

as I am not able to use CompressStream in my multithreaded scenario, I can't use SevenZipSharp at all.

sanme98 wrote May 24, 2011 at 3:52 PM

Hi mproepst,

If u don't mind run single thread only during compression period, u can using lock to solve this issue.

private object locker = new object();

public byte[] Compress(Stream inStream)
{
using (MemoryStream outStream = new MemoryStream())
{
SevenZip.SevenZipCompressor szc = new SevenZip.SevenZipCompressor();
szc.CompressionLevel = SevenZip.CompressionLevel.High;
szc.CompressStream(inStream, outStream, password);

return outStream.ToArray();
}
}

sanme98 wrote May 24, 2011 at 3:56 PM

sorry, mistake on above code, it should be


private object locker = new object();

public byte[] Compress(Stream inStream)
{
using (MemoryStream outStream = new MemoryStream())
{
lock(locker)
{
SevenZip.SevenZipCompressor szc = new SevenZip.SevenZipCompressor();
szc.CompressionLevel = SevenZip.CompressionLevel.High;
szc.CompressStream(inStream, outStream, password);
}
return outStream.ToArray();
}
}

mproepst wrote May 25, 2011 at 12:37 PM

Hi sanme98,

my code runs on a server and I am concerned about scalability, I don't want to syncronize sevenzip on a global level.
Is there a serious reason why this can't be fixed, for example with my proposed patch 9498?

sanme98 wrote May 25, 2011 at 2:54 PM

Hi mproepst,

Sorry, I didn't aware ur patch u upload. It should be can solve, but u need wait for author to release it.

wrote Jun 28, 2011 at 9:15 AM

PatrikOscarsson wrote Jun 28, 2011 at 9:19 AM

I added locking to LibraryManager's methods LoadLibrary and FreeLibrary using lock (_libraryFileName), that seems to have solved it. But official support for it should of course be really appreciated!

jeffreypry wrote Nov 16, 2011 at 6:11 PM

How about using a global mutex?

JohnBonano wrote Dec 15, 2011 at 9:40 PM

Anyone figure out this issue? Is wrapping the compression in a lock the answer?

wrote Dec 21, 2011 at 6:19 PM

scottlerch wrote Dec 21, 2011 at 10:44 PM

I can recreate this problem every time using several threads compressing various files at once on a machine with 8 hardware threads. Same issue as 9786, 6080. See my comments in 9786.

wrote Feb 22, 2013 at 1:16 AM